From fec65b3c802a10961b7d980c71fdb92261328eab Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Thu, 18 Sep 2025 02:04:59 +0300 Subject: [PATCH 1/3] fix: handle for-loop variable shadowing correctly Signed-off-by: Emmanuel Ferdman --- pylint/checkers/variables.py | 7 ++++++- tests/functional/f/for_loop_variable_shadowing.py | 10 ++++++++++ tests/functional/f/for_loop_variable_shadowing.txt | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/functional/f/for_loop_variable_shadowing.py create mode 100644 tests/functional/f/for_loop_variable_shadowing.txt diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index e18b535bf3..e73593e9fc 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -585,7 +585,12 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None: and parent_node.iter == node and parent_node.target in found_nodes ): - found_nodes = None + # Only filter out the for-loop target if there are other definitions besides the target + other_definitions = [fn for fn in found_nodes if fn != parent_node.target] + if other_definitions: + found_nodes = other_definitions + else: + found_nodes = None # Before filtering, check that this node's name is not a nonlocal if _is_nonlocal_name(node, node.frame()): diff --git a/tests/functional/f/for_loop_variable_shadowing.py b/tests/functional/f/for_loop_variable_shadowing.py new file mode 100644 index 0000000000..3372605049 --- /dev/null +++ b/tests/functional/f/for_loop_variable_shadowing.py @@ -0,0 +1,10 @@ +# pylint: disable=missing-module-docstring + +# Should not trigger undefined-variable +item = (1, 2, 3) +for item in item: + print(item) + +# Should trigger undefined-variable +for iteree in iteree: # [undefined-variable] + print(iteree) diff --git a/tests/functional/f/for_loop_variable_shadowing.txt b/tests/functional/f/for_loop_variable_shadowing.txt new file mode 100644 index 0000000000..44fa545209 --- /dev/null +++ b/tests/functional/f/for_loop_variable_shadowing.txt @@ -0,0 +1 @@ +undefined-variable:9:14:9:20::Undefined variable 'iteree':UNDEFINED From 95e3a792975b123da33f84963f60b9e72ce4b9d3 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Thu, 18 Sep 2025 14:42:46 +0300 Subject: [PATCH 2/3] fix: handle for-loop variable shadowing correctly Signed-off-by: Emmanuel Ferdman --- doc/whatsnew/fragments/10562.bugfix.rst | 3 +++ tests/functional/f/for_loop_variable_shadowing.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/10562.bugfix.rst diff --git a/doc/whatsnew/fragments/10562.bugfix.rst b/doc/whatsnew/fragments/10562.bugfix.rst new file mode 100644 index 0000000000..001dc72a5a --- /dev/null +++ b/doc/whatsnew/fragments/10562.bugfix.rst @@ -0,0 +1,3 @@ +Fix false positive ``undefined-variable`` (E0602) for for-loop variable shadowing patterns like ``for item in item:`` when the variable was previously defined. + +Closes #10562 diff --git a/tests/functional/f/for_loop_variable_shadowing.py b/tests/functional/f/for_loop_variable_shadowing.py index 3372605049..85941ff128 100644 --- a/tests/functional/f/for_loop_variable_shadowing.py +++ b/tests/functional/f/for_loop_variable_shadowing.py @@ -1,6 +1,6 @@ # pylint: disable=missing-module-docstring -# Should not trigger undefined-variable +# Should not trigger undefined-variable (#10562) item = (1, 2, 3) for item in item: print(item) From fd8fc82e76646c32abc42e058971030b53c372b6 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Fri, 19 Sep 2025 00:09:34 +0300 Subject: [PATCH 3/3] fix: handle for-loop variable shadowing correctly Signed-off-by: Emmanuel Ferdman --- doc/whatsnew/fragments/{10562.bugfix.rst => 10562.bugfix} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/whatsnew/fragments/{10562.bugfix.rst => 10562.bugfix} (100%) diff --git a/doc/whatsnew/fragments/10562.bugfix.rst b/doc/whatsnew/fragments/10562.bugfix similarity index 100% rename from doc/whatsnew/fragments/10562.bugfix.rst rename to doc/whatsnew/fragments/10562.bugfix