diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2021-12-12 11:25:35 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-12 17:25:35 +0100 |
commit | 80285a9304f3f8e3ed339525cac95f6da6b6ce58 (patch) | |
tree | 0c2231151edf49d2e77b8d13a020010e148a9b5c | |
parent | bd55b27d41542e3ca1f031f986b6151f6cac457f (diff) | |
download | pylint-git-80285a9304f3f8e3ed339525cac95f6da6b6ce58.tar.gz |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 5 | ||||
-rw-r--r-- | pylint/checkers/refactoring/refactoring_checker.py | 2 | ||||
-rw-r--r-- | tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py | 7 |
4 files changed, 19 insertions, 0 deletions
@@ -25,6 +25,11 @@ Release date: TBA Closes #4716 +* Fix crash in ``unnecessary-dict-index-lookup`` checker if the output of + ``items()`` is assigned to a 1-tuple. + + Closes #5504 + * The ``PyLinter`` class will now be initialiazed with a ``TextReporter`` as its reporter if none is provided. diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 55535638e..ec1ea0af5 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -26,6 +26,11 @@ Other Changes Closes #4716 +* Fix crash in ``unnecessary-dict-index-lookup`` checker if the output of + ``items()`` is assigned to a 1-tuple. + + Closes #5504 + * Fix false negative for ``consider-iterating-dictionary`` during membership checks encapsulated in iterables or ``not in`` checks diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 7bd82d1f9..2c687d418 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1882,6 +1882,8 @@ class RefactoringChecker(checkers.BaseTokenChecker): if isinstance(value, nodes.Name): if ( not isinstance(node.target, nodes.Tuple) + # Ignore 1-tuples: for k, in d.items() + or len(node.target.elts) < 2 or value.name != node.target.elts[0].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 423360c82..56e65e598 100644 --- a/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py +++ b/tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py @@ -100,3 +100,10 @@ for key, val in outer_dict.items(): for key_two, val_two in val.items(): del outer_dict[key][key_two] # [unnecessary-dict-index-lookup] break + +# Test partial unpacking of items +# https://github.com/PyCQA/pylint/issues/5504 + +d = {} +for key, in d.items(): + print(d[key]) |