From 80285a9304f3f8e3ed339525cac95f6da6b6ce58 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 12 Dec 2021 11:25:35 -0500 Subject: 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 --- ChangeLog | 5 +++++ doc/whatsnew/2.13.rst | 5 +++++ pylint/checkers/refactoring/refactoring_checker.py | 2 ++ tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/ChangeLog b/ChangeLog index 22c918048..9bc848b59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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]) -- cgit v1.2.1