Skip to content

Commit

Permalink
Fix a crash in the modified-iterating-dict checker involving instan…
Browse files Browse the repository at this point in the history
…ce attributes (#7472)
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed Sep 19, 2022
1 parent 9b359ad commit 8e05ff6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7461.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in the ``modified-iterating-dict`` checker involving instance attributes.

Closes #7461
12 changes: 8 additions & 4 deletions pylint/checkers/modified_iterating_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _is_node_assigns_subscript_name(node: nodes.NodeNG) -> bool:
)

def _modified_iterating_list_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return False
Expand All @@ -141,7 +141,7 @@ def _modified_iterating_list_cond(
)

def _modified_iterating_dict_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return False
Expand All @@ -159,10 +159,14 @@ def _modified_iterating_dict_cond(
return False
if infer_val != utils.safe_infer(iter_obj):
return False
return node.targets[0].value.name == iter_obj.name
if isinstance(iter_obj, nodes.Attribute):
iter_obj_name = iter_obj.attrname
else:
iter_obj_name = iter_obj.name
return node.targets[0].value.name == iter_obj_name

def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return False
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/m/modified_iterating.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ def my_method(self):
"""This should raise as we are deleting."""
for var in self.attribute:
del var # [modified-iterating-list]


class MyClass2:
"""Regression test for https://github.com/PyCQA/pylint/issues/7461"""
def __init__(self) -> None:
self.attribute = {}

def my_method(self):
"""This should not raise, as a copy was made."""
for key in self.attribute:
tmp = self.attribute.copy()
tmp[key] = None

0 comments on commit 8e05ff6

Please sign in to comment.