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

possibly-used-before-assigment fails to understand test with immutable bool #10095

Closed
lucascolley opened this issue Nov 23, 2024 · 2 comments
Closed
Labels
Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@lucascolley
Copy link

lucascolley commented Nov 23, 2024

Bug description

As per the docs, this works:

def func(x: bool) -> None:
    if not x:
        msg = 'hello world'
    print("hi")
    if not x:
        print(msg)
    return

but only because we are using the same test, not x, twice.

If we instead use the test x, pylint fails:

def func(x: bool) -> None:
    if not x:
        msg = 'hello world'
    print("hi")
    if x:
        return
    print(msg)

Command used

pylint

Pylint output

E0606: Possibly using variable 'msg' before assignment (possibly-used-before-assignment)

Expected behavior

It is impossible for print("hi") (or any intervening code that doesn't assign to x) to change the value of x since it is an immutable bool. If not x is False at the top of the function, x will always be True at the bottom of the function. So it is impossible for msg to be used before assignment - this is a false positive.

Pylint version

pylint 3.3.1
@lucascolley lucascolley added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 23, 2024
@zenlyj zenlyj added Control flow Requires control flow understanding 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 Nov 23, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report, this is an even simpler case than the one discussed in #9662, for which I would expect any fix to also cover this case. 👍

@jacobtylerwalls jacobtylerwalls added the Duplicate 🐫 Duplicate of an already existing issue label Nov 23, 2024
@jacobtylerwalls
Copy link
Member

So it is impossible for msg to be used before assignment - this is a false positive.

Part of the premise that I want to make explicit here is that there are no other reassignments to x in the middle of the function. x = not x before the second test at the bottom of the function would violate your example.

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 Duplicate 🐫 Duplicate of an already existing issue False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests

3 participants