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 when for loop has function call that returns NoReturn #7311

Closed
DetachHead opened this issue Aug 16, 2022 · 4 comments · Fixed by #7476
Labels
C: undefined-variable Issues related to 'undefined-variable' check 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

@DetachHead
Copy link
Contributor

DetachHead commented Aug 16, 2022

Bug description

from typing import NoReturn


def fail() -> NoReturn:
    ...

def foo(l):
    while True:
        for i in l:
            break
        else:
            continue
            # or break
            # or fail()
        print(i)

this is the same issue as #5971, though it seems to have only been fixed for return statements but not break, continue statements or function calls that return NoReturn or Never

Configuration

No response

Command used

pylint test.py

Pylint output

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

Expected behavior

no error

Pylint version

pylint 2.14.5
astroid 2.11.7
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

@DetachHead DetachHead added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 16, 2022
@DetachHead DetachHead changed the title W0631:undefined-loop-variable false positive when for loop has else branch with break or `continue W0631:undefined-loop-variable false positive when for loop has else branch with break or continue Aug 16, 2022
@DetachHead DetachHead changed the title W0631:undefined-loop-variable false positive when for loop has else branch with break or continue W0631:undefined-loop-variable false positive when for loop has else branch with break, continue or function call that returns NoReturn Aug 16, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code C: undefined-variable Issues related to 'undefined-variable' check Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 16, 2022
@jacobtylerwalls
Copy link
Member

I'd be in favor of adding break and continue to the list of nodes checked in the PR for #6480.

Would you be interested to prepare a PR?

@DetachHead
Copy link
Contributor Author

@jacobtylerwalls i've opened #7318, however i would like to also try to fix the NoReturn example but i'm not sure how

@DetachHead DetachHead changed the title W0631:undefined-loop-variable false positive when for loop has else branch with break, continue or function call that returns NoReturn W0631:undefined-loop-variable false positive when for loop has function call that returns NoReturn Aug 19, 2022
@DanielNoord
Copy link
Collaborator

@DetachHead Do you want to work on the NoReturn example? I can guide you through it if you want.

If you get the FunctionDef node of fail by inferring the call to it you can use the type_comment_returns attribute to get the return type annotation. With an inference on that annotation + a check for .qualname() == "typing.NoReturn I think we should be able to add this case as well.

@DetachHead
Copy link
Contributor Author

@DanielNoord i gave it a go but i can't figure out how to get the FunctionDef from the function call. #7419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: undefined-variable Issues related to 'undefined-variable' check 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
4 participants