Skip to content

Commit 1496340

Browse files
Fix a false positive for unbalanced-tuple-unpacking. (#10724)
* 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 Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 0426f8b commit 1496340

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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.
2+
3+
Closes #10721

pylint/checkers/variables.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,11 +2162,10 @@ def visit_assign(self, node: nodes.Assign) -> None:
21622162
# Check if we have starred nodes.
21632163
if any(isinstance(target, nodes.Starred) for target in targets):
21642164
return
2165-
21662165
try:
2167-
inferred = utils.safe_infer(node.value)
2168-
if inferred is not None:
2169-
self._check_unpacking(inferred, node, targets)
2166+
inferred = node.value.inferred()
2167+
if inferred is not None and len(inferred) == 1:
2168+
self._check_unpacking(inferred[0], node, targets)
21702169
except astroid.InferenceError:
21712170
return
21722171

tests/functional/u/unbalanced/unbalanced_tuple_unpacking.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,14 @@ def my_function(mystring):
167167
# Using a lot of args, so we have a high probability to still trigger the problem if
168168
# we add arguments to our unittest command later
169169
(p, q, r, s, t, u, v, w, x, y, z) = sys.argv # pylint: disable=invalid-name
170+
171+
172+
# https://github.com/pylint-dev/pylint/issues/10721
173+
def fruit(apple: int, pear: int, kiwi: int | None = None) -> tuple[int, int] | tuple[int, int, int]:
174+
return (apple, pear) if kiwi is None else (apple, pear, kiwi)
175+
176+
def main():
177+
_, _ = fruit(1, 2)
178+
_, _ = fruit(1, 2, 3) # known false negative, requires better None comprehension in astroid
179+
_, _, _ = fruit(1, 2) # known false negative, requires better None comprehension in astroid
180+
_, _, _ = fruit(1, 2, 3)

0 commit comments

Comments
 (0)