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

False positive E0601: used-before-assignment when assignment of variables is inside if __name__ == "__main__": #8543

Closed
shatakshiiii opened this issue Apr 6, 2023 · 2 comments
Labels
Invalid Not a bug, already exists or already fixed

Comments

@shatakshiiii
Copy link

Bug description

Pylint version 3.0.0a6 seems to produce an used-before-assignment error when variables are assigned within
if __name__ == "__main__": function and later used in different functions.

This is coming from latest pylint v3.0.0a6 (since there is no such error in previous pylint versions)

Example code to reproduce this error:

def foo():
    print(x-y)

def main():
    print(x+y)
    foo()

if __name__ == "__main__":
    x = 20
    y = 10
    main()

Logging this issue here to get more context around this, as this is just a false-positive with v3.0.0a6 or something that pylint is aiming to trigger in its newer versions (As the above code format is supported in python).

Configuration

No response

Command used

pylint example.py

Pylint output

************* Module example
example.py:2:10: E0601: Using variable 'x' before assignment (used-before-assignment)
example.py:2:12: E0601: Using variable 'y' before assignment (used-before-assignment)

Expected behavior

Pylint should not trigger used-before-asssignment error.

Pylint version

pylint 3.0.0a6
astroid 2.15.2
Python 3.9.0 (default, Oct  6 2022, 15:58:26)


### OS / Environment

Fedora 37

### Additional dependencies

_No response_
@shatakshiiii shatakshiiii added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 6, 2023
@shatakshiiii shatakshiiii changed the title False positive used-before-assignment when assignment of variables is inside if __name__ == "__main__": False positive E0601: used-before-assignment when assignment of variables is inside if __name__ == "__main__": Apr 6, 2023
@jacobtylerwalls
Copy link
Member

Thanks for the question. This is intentional as of #6677. You're correct that this executes just fine when run from the CLI, but any other uses will raise NameError:

>>> import a
>>> a.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jacobwalls/pylint/a.py", line 2, in foo
    print(x-y)
          ^
NameError: name 'x' is not defined

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2023
@jacobtylerwalls jacobtylerwalls added Invalid Not a bug, already exists or already fixed and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 6, 2023
@shatakshiiii
Copy link
Author

Thanks for the explaination @jacobtylerwalls !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, already exists or already fixed
Projects
None yet
Development

No branches or pull requests

2 participants