From 4a057941a0a30e2fa6d485416ca4167cda6bf6fb Mon Sep 17 00:00:00 2001 From: orSolocate <38433858+orSolocate@users.noreply.github.com> Date: Sat, 26 Mar 2022 15:47:26 +0300 Subject: [PATCH] Fix issue #5969 for `modified_iterating-list` (#5986) When the list/dict/set being iterated through is a function call. Co-authored-by: Pierre Sassoulas --- ChangeLog | 5 +++++ pylint/checkers/modified_iterating_checker.py | 5 +++-- tests/functional/m/modified_iterating.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ea6f7701ef..be25eceb2b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -39,6 +39,11 @@ Release date: TBA Closes #5187 +* Fix program crash for ``modified_iterating-list/set/dict`` when the list/dict/set + being iterated through is a function call. + + Closes #5969 + * Don't emit ``broken-noreturn`` and ``broken-collections-callable`` errors inside ``if TYPE_CHECKING`` blocks. diff --git a/pylint/checkers/modified_iterating_checker.py b/pylint/checkers/modified_iterating_checker.py index d1fe08d40c..ca2a0c9000 100644 --- a/pylint/checkers/modified_iterating_checker.py +++ b/pylint/checkers/modified_iterating_checker.py @@ -57,8 +57,9 @@ class ModifiedIterationChecker(checkers.BaseChecker): ) def visit_for(self, node: nodes.For) -> None: iter_obj = node.iter - for body_node in node.body: - self._modified_iterating_check_on_node_and_children(body_node, iter_obj) + if isinstance(iter_obj, nodes.Name): + for body_node in node.body: + self._modified_iterating_check_on_node_and_children(body_node, iter_obj) def _modified_iterating_check_on_node_and_children( self, body_node: nodes.NodeNG, iter_obj: nodes.NodeNG diff --git a/tests/functional/m/modified_iterating.py b/tests/functional/m/modified_iterating.py index 32155fbc80..8817c1db0e 100644 --- a/tests/functional/m/modified_iterating.py +++ b/tests/functional/m/modified_iterating.py @@ -55,3 +55,19 @@ item_list.remove(1) # [modified-iterating-list] for _ in []: item_list.append(1) # [modified-iterating-list] + + +def format_manifest_serializer_errors(errors): + """Regression test for issue #5969 - iter_obj is a function call.""" + errors_messages = [] + for key, value in errors.items(): + for message in format_manifest_serializer_errors(value): + error_message = f"{key}: {message}" + errors_messages.append(error_message) + return errors_messages + + +dict1 = {"1": 1} +dict2 = {"2": 2} +for item in dict1: + dict2[item] = 1