Skip to content

Commit f58a7b6

Browse files
fix: avoid false positive unbalanced-tuple-unpacking for cross-module function calls
Signed-off-by: Emmanuel Ferdman <[email protected]>
1 parent 42c0131 commit f58a7b6

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive ``unbalanced-tuple-unpacking`` warning for cross-module function calls.
2+
3+
Closes #10317

pylint/checkers/variables.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,6 +3107,16 @@ def _check_unpacking(
31073107
# Variable-length argument, we can't determine the length.
31083108
return
31093109

3110+
# Skip check for function calls with empty inference from external modules.
3111+
# This avoids false positives when static analysis fails on library functions.
3112+
if (isinstance(node.value, nodes.Call) and
3113+
isinstance(inferred, (nodes.List, nodes.Tuple)) and
3114+
len(inferred.elts) == 0 and
3115+
hasattr(inferred, 'parent') and
3116+
isinstance(inferred.parent, nodes.Assign) and
3117+
node.root().name != inferred.root().name):
3118+
return
3119+
31103120
# Attempt to check unpacking is properly balanced
31113121
values = self._nodes_to_unpack(inferred)
31123122
details = _get_unpacking_extra_info(node, inferred)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Test for unbalanced tuple unpacking false positives with function calls."""
2+
3+
import statistics
4+
5+
6+
def test_statistics_quantiles():
7+
"""Test that statistics.quantiles doesn't trigger false positive."""
8+
q1, q2, q3 = statistics.quantiles(list(range(100)), n=4)
9+
return q1, q2, q3
10+
11+
12+
def test_statistics_quantiles_different_n():
13+
"""Test statistics.quantiles with different n values."""
14+
d1, d2, d3, d4, d5, d6, d7, d8, d9 = statistics.quantiles(list(range(100)), n=10)
15+
return d1, d2, d3, d4, d5, d6, d7, d8, d9
16+
17+
18+
def empty_list_function():
19+
"""Function that returns an empty list."""
20+
return []
21+
22+
23+
def test_legitimate_unbalanced_unpacking():
24+
"""Test that legitimate unbalanced unpacking still gets flagged."""
25+
a, b, c = [1, 2] # [unbalanced-tuple-unpacking]
26+
x, y = empty_list_function() # [unbalanced-tuple-unpacking]
27+
return a, b, c, x, y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
unbalanced-tuple-unpacking:25:4:25:20:test_legitimate_unbalanced_unpacking:"Possible unbalanced tuple unpacking with sequence '[1, 2]': left side has 3 labels, right side has 2 values":INFERENCE
2+
unbalanced-tuple-unpacking:26:4:26:32:test_legitimate_unbalanced_unpacking:"Possible unbalanced tuple unpacking with sequence defined at line 20: left side has 2 labels, right side has 0 values":INFERENCE

0 commit comments

Comments
 (0)