diff --git a/ChangeLog b/ChangeLog index f48f42c2e8..ccce125a1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -29,6 +29,10 @@ Release date: TBA Relates to #6531 +* Fix a crash in ``unnecessary-dict-index-lookup`` when subscripting an attribute. + + Closes #6557 + * Fix a crash when accessing ``__code__`` and assigning it to a variable. Closes #6539 diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 5c1da5a0f5..1ae1ca7f9a 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -653,3 +653,7 @@ Other Changes * Fix ``IndexError`` crash in ``uninferable_final_decorators`` method. Relates to #6531 + +* Fix a crash in ``unnecessary-dict-index-lookup`` when subscripting an attribute. + + Closes #6557 diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 8dc65f129d..730f1ae578 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1938,6 +1938,7 @@ def _check_unnecessary_dict_index_lookup( elif isinstance(value, nodes.Subscript): if ( not isinstance(node.target, nodes.AssignName) + or not isinstance(value.value, nodes.Name) or node.target.name != value.value.name or iterating_object_name != subscript.value.as_string() ): diff --git a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py index f594d9e0f8..7ae5488ce7 100644 --- a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py +++ b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py @@ -112,3 +112,10 @@ class Foo: d = {} for key, in d.items(): print(d[key]) + +# Test subscripting an attribute +# https://github.com/PyCQA/pylint/issues/6557 +f = Foo() +for input_output in d.items(): + f.input_output = input_output # pylint: disable=attribute-defined-outside-init + print(d[f.input_output[0]])