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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

(馃悶) false error: undefined-loop-variable when for loop has terminating else branch #5971

Closed
KotlinIsland opened this issue Mar 25, 2022 · 2 comments 路 Fixed by #6480
Closed
Labels
Control flow Requires control flow understanding False Positive 馃 A message is emitted but nothing is wrong with the code
Milestone

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 25, 2022

Bug description

def foo(l):
    for i in l:
        break
    else:
        return
    print(i)

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:6:10: W0631: Using possibly undefined loop variable 'i' (undefined-loop-variable)

Expected behavior

no error

Pylint version

pylint 2.13.0
astroid 2.11.1
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

@KotlinIsland KotlinIsland added the Needs triage 馃摜 Just created, needs acknowledgment, triage, and proper labelling label Mar 25, 2022
@KotlinIsland KotlinIsland changed the title false error: undefined-loop-variable when for loop has terminating else branch (馃悶) false error: undefined-loop-variable when for loop has terminating else branch Mar 25, 2022
@Pierre-Sassoulas Pierre-Sassoulas added 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 Mar 25, 2022
@DanielNoord
Copy link
Collaborator

Related are #5608, #5146.

@jacobtylerwalls Please help me remember, what was our conclusion about this message. Were you going to take a look at the larger problem with this message and these patterns for 2.14?

@jacobtylerwalls
Copy link
Member

Were you going to take a look at the larger problem with this message and these patterns for 2.14?

I was just waiting for input on #5649 -- I'll update the PR itself, looks like most discussion happened on the issue.

But that said, I think this issue is a completely isolated control-flow issue. We just lack any logic to know that else: return means you can depend on i being defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding False Positive 馃 A message is emitted but nothing is wrong with the code
Projects
None yet
4 participants