Skip to content

Commit

Permalink
Fix crash when deleting from a dict returned from a call (#8678)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored May 12, 2023
1 parent 0d878dd commit aed3c08
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8598.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix crash for ``modified-while-iterating`` checker when deleting
members of a dict returned from a call.

Closes #8598
6 changes: 1 addition & 5 deletions pylint/checkers/modified_iterating_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
7 changes: 7 additions & 0 deletions tests/functional/m/modified_iterating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/functional/m/modified_iterating.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aed3c08

Please sign in to comment.