Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined-loop-variable in walrus in comprehension test #7222

Closed
abe-winter opened this issue Jul 23, 2022 · 2 comments · Fixed by #7324
Closed

undefined-loop-variable in walrus in comprehension test #7222

abe-winter opened this issue Jul 23, 2022 · 2 comments · Fixed by #7324
Assignees
Labels
Assignment expression Related to the walrus operator / assignment expression False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@abe-winter
Copy link

Bug description

I think I'm getting a spurious undefined-loop-variable warning when:

  • walrus expression in 'if' block of list comprehension
  • loop variable in list comprehension (x, here) is the same as a previous loop
# pylint: disable=C0114,C0116,C0103

def example(arr):
    for x in arr:
        pass
    print([y for x in arr if (y := x)]) # this warns undefined-loop-variable
    print([y for x in arr if x]) # this is fine

def example2(arr):
    print([y for x in arr if (y := x)]) # this is fine

Configuration

No response

Command used

pylint example.py

Pylint output

************* Module example
example.py:6:35: W0631: Using possibly undefined loop variable 'x' (undefined-loop-variable)

------------------------------------------------------------------
Your code has been rated at 8.57/10 (previous run: 8.00/10, +0.57)

Expected behavior

Shouldn't warn.

Pylint version

pylint 2.14.5
astroid 2.11.7
Python 3.9.7 (default, Jun 22 2022, 20:11:26) 
[GCC 11.2.0]

OS / Environment

ubuntu 21.10

Additional dependencies

No response

@abe-winter abe-winter added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 23, 2022
@jacobtylerwalls jacobtylerwalls added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation Assignment expression Related to the walrus operator / assignment expression and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 23, 2022
@jacobtylerwalls jacobtylerwalls changed the title undefined-loop-variable in walrus undefined-loop-variable in walrus in comprehension test Jul 23, 2022
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Aug 4, 2022

Edited: thought I had a smaller reproducer, but it missed the point...

@jacobtylerwalls jacobtylerwalls added Assignment expression Related to the walrus operator / assignment expression and removed Assignment expression Related to the walrus operator / assignment expression labels Aug 4, 2022
@jacobtylerwalls
Copy link
Member

Draft diff, needs tests:

diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 0cdd37c5e..dc3fbc821 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2247,6 +2247,14 @@ class VariablesChecker(BaseChecker):
         ):
             return
 
+        maybe_walrus = utils.get_node_first_ancestor_of_type(node, nodes.NamedExpr)
+        if maybe_walrus:
+            maybe_comprehension = utils.get_node_first_ancestor_of_type(maybe_walrus, nodes.Comprehension)
+            if maybe_comprehension:
+                comprehension_scope = utils.get_node_first_ancestor_of_type(maybe_comprehension, nodes.ComprehensionScope)
+                if comprehension_scope.parent.scope() is scope and node.name in comprehension_scope.locals:
+                    return
+
         # For functions we can do more by inferring the length of the itered object
         try:
             inferred = next(assign.iter.infer())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Assignment expression Related to the walrus operator / assignment expression False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants