Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10721.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a false positive for ``unbalanced-tuple-unpacking`` when a tuple is assigned to a function call and the structure of the function's return value is ambiguous.

Closes #10721
7 changes: 3 additions & 4 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2162,11 +2162,10 @@ def visit_assign(self, node: nodes.Assign) -> None:
# Check if we have starred nodes.
if any(isinstance(target, nodes.Starred) for target in targets):
return

try:
inferred = utils.safe_infer(node.value)
if inferred is not None:
self._check_unpacking(inferred, node, targets)
inferred = node.value.inferred()
if inferred is not None and len(inferred) == 1:
self._check_unpacking(inferred[0], node, targets)
except astroid.InferenceError:
return

Expand Down
11 changes: 11 additions & 0 deletions tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,14 @@ def my_function(mystring):
# Using a lot of args, so we have a high probability to still trigger the problem if
# we add arguments to our unittest command later
(p, q, r, s, t, u, v, w, x, y, z) = sys.argv # pylint: disable=invalid-name


# https://github.com/pylint-dev/pylint/issues/10721
def fruit(apple: int, pear: int, kiwi: int | None = None) -> tuple[int, int] | tuple[int, int, int]:
return (apple, pear) if kiwi is None else (apple, pear, kiwi)

def main():
_, _ = fruit(1, 2)
_, _ = fruit(1, 2, 3) # known false negative, requires better None comprehension in astroid
_, _, _ = fruit(1, 2) # known false negative, requires better None comprehension in astroid
_, _, _ = fruit(1, 2, 3)