From aed3c080388a8dc1d44c1a14a5ed243233f77c1c Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 12 May 2023 14:56:14 -0400 Subject: [PATCH] Fix crash when deleting from a dict returned from a call (#8678) --- doc/whatsnew/fragments/8598.bugfix | 4 ++++ pylint/checkers/modified_iterating_checker.py | 6 +----- tests/functional/m/modified_iterating.py | 7 +++++++ tests/functional/m/modified_iterating.txt | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 doc/whatsnew/fragments/8598.bugfix diff --git a/doc/whatsnew/fragments/8598.bugfix b/doc/whatsnew/fragments/8598.bugfix new file mode 100644 index 0000000000..e1961f709c --- /dev/null +++ b/doc/whatsnew/fragments/8598.bugfix @@ -0,0 +1,4 @@ +Fix crash for ``modified-while-iterating`` checker when deleting +members of a dict returned from a call. + +Closes #8598 diff --git a/pylint/checkers/modified_iterating_checker.py b/pylint/checkers/modified_iterating_checker.py index 9d89f7255f..61471b9ee5 100644 --- a/pylint/checkers/modified_iterating_checker.py +++ b/pylint/checkers/modified_iterating_checker.py @@ -90,14 +90,10 @@ def _modified_iterating_check( elif self._modified_iterating_set_cond(node, iter_obj): msg_id = "modified-iterating-set" if msg_id: - if isinstance(iter_obj, nodes.Attribute): - obj_name = iter_obj.attrname - else: - obj_name = iter_obj.name self.add_message( msg_id, node=node, - args=(obj_name,), + args=(iter_obj._repr_name(),), confidence=interfaces.INFERENCE, ) diff --git a/tests/functional/m/modified_iterating.py b/tests/functional/m/modified_iterating.py index 2dae6c1069..bd2d0dd13d 100644 --- a/tests/functional/m/modified_iterating.py +++ b/tests/functional/m/modified_iterating.py @@ -119,6 +119,13 @@ def my_method(self): tmp = self.attribute.copy() tmp[key] = None + +def my_call(): + """Regression test for https://github.com/pylint-dev/pylint/issues/7461""" + for var in {}.copy(): + del var # [modified-iterating-dict] + + class MyEnum(Enum): FOO = 1 BAR = 2 diff --git a/tests/functional/m/modified_iterating.txt b/tests/functional/m/modified_iterating.txt index e5b57ca328..66544da386 100644 --- a/tests/functional/m/modified_iterating.txt +++ b/tests/functional/m/modified_iterating.txt @@ -14,3 +14,4 @@ modified-iterating-list:68:12:68:31::Iterated list 'item_list' is being modified modified-iterating-list:70:16:70:35::Iterated list 'item_list' is being modified inside for loop body, consider iterating through a copy of it instead.:INFERENCE modified-iterating-dict:96:8:96:28:update_existing_key:Iterated dict 'my_dict' is being modified inside for loop body, iterate through a copy of it instead.:INFERENCE modified-iterating-list:108:12:108:19:MyClass.my_method:Iterated list 'attribute' is being modified inside for loop body, consider iterating through a copy of it instead.:INFERENCE +modified-iterating-dict:126:8:126:15:my_call:Iterated dict '' is being modified inside for loop body, iterate through a copy of it instead.:INFERENCE