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

W0631 undefined-loop-variable false positive for lambda statements inside first of two loops #6419

Closed
Mnmhead opened this issue Apr 21, 2022 · 1 comment · Fixed by #6479
Closed
Assignees
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@Mnmhead
Copy link

Mnmhead commented Apr 21, 2022

Bug description

def test():
   l = []
   for thing in l:
      x = lambda: print( thing )
      x()

   for thing in l:
      print( thing )

If I initialize the list, l, to anything besides the empty list, then the W0631 warning goes away. Or instead if I delete the second for-loop, the W0631 warning goes away as well.

Seems similar to this bug: #202

Configuration

No response

Command used

pylint test.py

Pylint output

% pylint test.py
************* Module test
test.py:16:25: W0640: Cell variable thing defined in loop (cell-var-from-loop)
test.py:16:25: W0631: Using possibly undefined loop variable 'thing' (undefined-loop-variable)

-----------------------------------
Your code has been rated at 8.46/10

Expected behavior

I don't expect to see W0631 (undefined-loop-variable). The (cell-var-from-loop) warning is warranted.

Pylint version

% pylint --version
pylint 2.11.1
astroid 2.8.5
Python 3.8.12 (default, Aug 31 2021, 12:46:19)
[GCC 8.4.0 20200304 (Red Hat 8.4.0-4)]

OS / Environment

No response

Additional dependencies

No response

@Mnmhead Mnmhead added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 21, 2022
@jacobtylerwalls jacobtylerwalls added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 21, 2022
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Apr 21, 2022

Thanks, I agree that's unexpected. This diff seems to fix it. If this looks correct to you, would you perhaps be interested in submitting a PR?

collapsed bc this wasn't correct, entirely ```diff diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 1a84b739..16c93c41 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2222,8 +2222,8 @@ class VariablesChecker(BaseChecker): ): _astmts = [] else: - _astmts = astmts[:1] - for i, stmt in enumerate(astmts[1:]): + _astmts = astmts[:] + for i, stmt in enumerate(astmts): if astmts[i].statement(future=True).parent_of( stmt ) and not in_for_else_branch(astmts[i].statement(future=True), stmt): ```

@jacobtylerwalls jacobtylerwalls changed the title W0631 undefined-loop-variable false positive W0631 undefined-loop-variable false positive for lambda statements inside first of two loops Apr 21, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.8 milestone Apr 21, 2022
@jacobtylerwalls jacobtylerwalls self-assigned this Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
3 participants