Skip to content

Commit 33f3fcf

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Fix a crash in unnecessary-dict-index-lookup when subscripting an attribute (#6579)
1 parent aecadc2 commit 33f3fcf

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Release date: TBA
2929

3030
Relates to #6531
3131

32+
* Fix a crash in ``unnecessary-dict-index-lookup`` when subscripting an attribute.
33+
34+
Closes #6557
35+
3236
* Fix a crash when accessing ``__code__`` and assigning it to a variable.
3337

3438
Closes #6539

doc/whatsnew/2.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,7 @@ Other Changes
653653
* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method.
654654

655655
Relates to #6531
656+
657+
* Fix a crash in ``unnecessary-dict-index-lookup`` when subscripting an attribute.
658+
659+
Closes #6557

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,7 @@ def _check_unnecessary_dict_index_lookup(
19381938
elif isinstance(value, nodes.Subscript):
19391939
if (
19401940
not isinstance(node.target, nodes.AssignName)
1941+
or not isinstance(value.value, nodes.Name)
19411942
or node.target.name != value.value.name
19421943
or iterating_object_name != subscript.value.as_string()
19431944
):

tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,10 @@ class Foo:
112112
d = {}
113113
for key, in d.items():
114114
print(d[key])
115+
116+
# Test subscripting an attribute
117+
# https://github.com/PyCQA/pylint/issues/6557
118+
f = Foo()
119+
for input_output in d.items():
120+
f.input_output = input_output # pylint: disable=attribute-defined-outside-init
121+
print(d[f.input_output[0]])

0 commit comments

Comments
 (0)