Skip to content

Commit 8e05ff6

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Fix a crash in the modified-iterating-dict checker involving instance attributes (#7472)
1 parent 9b359ad commit 8e05ff6

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

doc/whatsnew/fragments/7461.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in the ``modified-iterating-dict`` checker involving instance attributes.
2+
3+
Closes #7461

pylint/checkers/modified_iterating_checker.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _is_node_assigns_subscript_name(node: nodes.NodeNG) -> bool:
128128
)
129129

130130
def _modified_iterating_list_cond(
131-
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
131+
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
132132
) -> bool:
133133
if not self._is_node_expr_that_calls_attribute_name(node):
134134
return False
@@ -141,7 +141,7 @@ def _modified_iterating_list_cond(
141141
)
142142

143143
def _modified_iterating_dict_cond(
144-
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
144+
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
145145
) -> bool:
146146
if not self._is_node_assigns_subscript_name(node):
147147
return False
@@ -159,10 +159,14 @@ def _modified_iterating_dict_cond(
159159
return False
160160
if infer_val != utils.safe_infer(iter_obj):
161161
return False
162-
return node.targets[0].value.name == iter_obj.name
162+
if isinstance(iter_obj, nodes.Attribute):
163+
iter_obj_name = iter_obj.attrname
164+
else:
165+
iter_obj_name = iter_obj.name
166+
return node.targets[0].value.name == iter_obj_name
163167

164168
def _modified_iterating_set_cond(
165-
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
169+
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
166170
) -> bool:
167171
if not self._is_node_expr_that_calls_attribute_name(node):
168172
return False

tests/functional/m/modified_iterating.py

+12
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ def my_method(self):
105105
"""This should raise as we are deleting."""
106106
for var in self.attribute:
107107
del var # [modified-iterating-list]
108+
109+
110+
class MyClass2:
111+
"""Regression test for https://github.com/PyCQA/pylint/issues/7461"""
112+
def __init__(self) -> None:
113+
self.attribute = {}
114+
115+
def my_method(self):
116+
"""This should not raise, as a copy was made."""
117+
for key in self.attribute:
118+
tmp = self.attribute.copy()
119+
tmp[key] = None

0 commit comments

Comments
 (0)