Skip to content

Commit 215d00d

Browse files
jacobtylerwallsDanielNoord
authored andcommitted
Fix crash on unbalanced tuple unpacking
1 parent f137871 commit 215d00d

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ What's New in Pylint 2.13.3?
3636
============================
3737
Release date: TBA
3838

39+
* Fix crash involving unbalanced tuple unpacking.
40+
41+
Closes #5998
3942

4043

4144
What's New in Pylint 2.13.2?

pylint/checkers/base/name_checker/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ def visit_assignname(self, node: nodes.AssignName) -> None:
386386
elif (
387387
isinstance(node.parent, nodes.Tuple)
388388
and isinstance(assign_type.value, nodes.Tuple)
389+
# protect against unbalanced tuple unpacking
390+
and node.parent.elts.index(node) < len(assign_type.value.elts)
389391
and self._assigns_typevar(
390392
assign_type.value.elts[node.parent.elts.index(node)]
391393
)

tests/functional/u/unbalanced_tuple_unpacking.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,6 @@ def my_function(mystring):
157157
a, b = my_function("12") # [unbalanced-tuple-unpacking]
158158
c = my_function("12")
159159
d, *_ = my_function("12")
160+
161+
# https://github.com/PyCQA/pylint/issues/5998
162+
x, y, z = (1, 2) # [unbalanced-tuple-unpacking]

tests/functional/u/unbalanced_tuple_unpacking.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ unbalanced-tuple-unpacking:96:8:96:33:UnbalancedUnpacking.test:"Possible unbalan
66
unbalanced-tuple-unpacking:140:8:140:43:MyClass.sum_unpack_3_into_4:"Possible unbalanced tuple unpacking with sequence defined at line 128: left side has 4 label(s), right side has 3 value(s)":UNDEFINED
77
unbalanced-tuple-unpacking:145:8:145:28:MyClass.sum_unpack_3_into_2:"Possible unbalanced tuple unpacking with sequence defined at line 128: left side has 2 label(s), right side has 3 value(s)":UNDEFINED
88
unbalanced-tuple-unpacking:157:0:157:24::"Possible unbalanced tuple unpacking with sequence defined at line 151: left side has 2 label(s), right side has 0 value(s)":UNDEFINED
9+
unbalanced-tuple-unpacking:162:0:162:16::"Possible unbalanced tuple unpacking with sequence (1, 2): left side has 3 label(s), right side has 2 value(s)":UNDEFINED

0 commit comments

Comments
 (0)