From 33f3fcf047227ddd7d5816184ac4bf01f60863bb Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 11 May 2022 10:20:17 -0400 Subject: Fix a crash in `unnecessary-dict-index-lookup` when subscripting an attribute (#6579) --- ChangeLog | 4 ++++ doc/whatsnew/2.13.rst | 4 ++++ pylint/checkers/refactoring/refactoring_checker.py | 1 + tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py | 7 +++++++ 4 files changed, 16 insertions(+) diff --git a/ChangeLog b/ChangeLog index f48f42c2e..ccce125a1 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 5c1da5a0f..1ae1ca7f9 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 8dc65f129..730f1ae57 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1938,6 +1938,7 @@ class RefactoringChecker(checkers.BaseTokenChecker): 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 f594d9e0f..7ae5488ce 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 @@ for key, val in outer_dict.items(): 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]]) -- cgit v1.2.1