Skip to content

Commit 80285a9

Browse files
Fix #5504: Fix crash if the output of items() is assigned to a 1-tuple (#5505)
* Fix #5504: Fix crash if the output of items() is assigned to a 1-tuple
1 parent bd55b27 commit 80285a9

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Release date: TBA
2525

2626
Closes #4716
2727

28+
* Fix crash in ``unnecessary-dict-index-lookup`` checker if the output of
29+
``items()`` is assigned to a 1-tuple.
30+
31+
Closes #5504
32+
2833
* The ``PyLinter`` class will now be initialiazed with a ``TextReporter``
2934
as its reporter if none is provided.
3035

doc/whatsnew/2.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Other Changes
2626

2727
Closes #4716
2828

29+
* Fix crash in ``unnecessary-dict-index-lookup`` checker if the output of
30+
``items()`` is assigned to a 1-tuple.
31+
32+
Closes #5504
33+
2934
* Fix false negative for ``consider-iterating-dictionary`` during membership checks encapsulated in iterables
3035
or ``not in`` checks
3136

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,8 @@ def _check_unnecessary_dict_index_lookup(
18821882
if isinstance(value, nodes.Name):
18831883
if (
18841884
not isinstance(node.target, nodes.Tuple)
1885+
# Ignore 1-tuples: for k, in d.items()
1886+
or len(node.target.elts) < 2
18851887
or value.name != node.target.elts[0].name
18861888
or iterating_object_name != subscript.value.as_string()
18871889
):

tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,10 @@ class Foo:
100100
for key_two, val_two in val.items():
101101
del outer_dict[key][key_two] # [unnecessary-dict-index-lookup]
102102
break
103+
104+
# Test partial unpacking of items
105+
# https://github.com/PyCQA/pylint/issues/5504
106+
107+
d = {}
108+
for key, in d.items():
109+
print(d[key])

0 commit comments

Comments
 (0)