summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2022-05-11 10:20:17 -0400
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-05-13 17:30:49 +0200
commit33f3fcf047227ddd7d5816184ac4bf01f60863bb (patch)
tree147f921052291a73aa19278c05369bdf2a3f4400
parentaecadc276022fe5d445b26aa98e94d3b8df8bc98 (diff)
downloadpylint-git-33f3fcf047227ddd7d5816184ac4bf01f60863bb.tar.gz
Fix a crash in `unnecessary-dict-index-lookup` when subscripting an attribute (#6579)
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/2.13.rst4
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py1
-rw-r--r--tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py7
4 files changed, 16 insertions, 0 deletions
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]])