Skip to content

Commit

Permalink
Don't emit modified-iterating-dict when updating existing keys (#7037)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
jacobtylerwalls and Pierre-Sassoulas committed Jun 29, 2022
1 parent bee24cd commit f8f05f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ Release date: TBA

Closes #7006

* Fixed a false positive for ``modified-iterating-dict`` when updating an existing key.

Closes #6179

* Fixed an issue where many-core Windows machines (>~60 logical processors) would hang when
using the default jobs count.

Closes #6965


What's New in Pylint 2.14.3?
----------------------------
Release date: 2022-06-18
Expand Down
9 changes: 9 additions & 0 deletions pylint/checkers/modified_iterating_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ def _modified_iterating_dict_cond(
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return False
# Do not emit when merely updating the same key being iterated
if (
isinstance(iter_obj, nodes.Name)
and iter_obj.name == node.targets[0].value.name
and isinstance(iter_obj.parent.target, nodes.AssignName)
and isinstance(node.targets[0].slice, nodes.Name)
and iter_obj.parent.target.name == node.targets[0].slice.name
):
return False
infer_val = utils.safe_infer(node.targets[0].value)
if not isinstance(infer_val, nodes.Dict):
return False
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/m/modified_iterating.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ def format_manifest_serializer_errors(errors):
dict2 = {"2": 2}
for item in dict1:
dict2[item] = 1


def update_existing_key():
"""No message when updating existing keys"""
for key in my_dict:
my_dict[key] = 1

for key in my_dict:
new_key = key.lower()
my_dict[new_key] = 1 # [modified-iterating-dict]
1 change: 1 addition & 0 deletions tests/functional/m/modified_iterating.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ modified-iterating-list:48:4:48:23::Iterated list 'item_list' is being modified
modified-iterating-list:52:4:52:23::Iterated list 'item_list' is being modified inside for loop body, consider iterating through a copy of it instead.:INFERENCE
modified-iterating-list:55:12:55:31::Iterated list 'item_list' is being modified inside for loop body, consider iterating through a copy of it instead.:INFERENCE
modified-iterating-list:57:16:57:35::Iterated list 'item_list' is being modified inside for loop body, consider iterating through a copy of it instead.:INFERENCE
modified-iterating-dict:83:8:83:28:update_existing_key:Iterated dict 'my_dict' is being modified inside for loop body, iterate through a copy of it instead.:INFERENCE

0 comments on commit f8f05f1

Please sign in to comment.