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 used-before-assignment when usage and assignment guarded by the same test in different statements #8167

Closed
tvandeveenzepp opened this issue Feb 2, 2023 · 5 comments · Fixed by #8581 or #9093
Assignees
Labels
Blocker 🙅 Blocks the next release C: used-before-assignment Issues related to 'used-before-assignment' check Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code High priority Issue with more than 10 reactions Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@tvandeveenzepp
Copy link

Bug description

The used-before-assignment check reports false positive for using the same variable inside multiple if __name__ == '__main__' statements.

# pylint: disable=missing-docstring

if __name__ == '__main__':
    X = 1

if __name__ == '__main__':
    print(X)

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:7:10: E0601: Using variable 'X' before assignment (used-before-assignment)

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

Expected behavior

No error.

Pylint version

pylint 2.16.0
astroid 2.14.1
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

@tvandeveenzepp tvandeveenzepp added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 2, 2023
@clavedeluna
Copy link
Collaborator

It would appear if __name__ == '__main__': is causing different behavior because if you do:

# pylint: disable=missing-docstring

if True:
    X = 1

if True:
    print(X)

the msg is not emitted. My hunch is this is happening because pylint isn't loading it as a script which is what if __name__ == '__main__': requires.

@clavedeluna clavedeluna added 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 C: used-before-assignment Issues related to 'used-before-assignment' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 2, 2023
@Pierre-Sassoulas Pierre-Sassoulas added the Control flow Requires control flow understanding label Feb 2, 2023
@jacobtylerwalls
Copy link
Member

Likely met the "always false" test in #6677.

@jacobtylerwalls
Copy link
Member

We may need to check if the variable is guarded again by the same(ish) test.

@saranti
Copy link
Contributor

saranti commented Feb 6, 2023

This bug wasn't present in v2.15.10. I'll take this up if no one objects

@jacobtylerwalls jacobtylerwalls changed the title False positive used-before-assignment multiple if __name__ == '__main__' statements False positive used-before-assignment when usage and assignment guarded by the same test in different statements Apr 9, 2023
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Apr 9, 2023

This also affects if TYPE_CHECKING: e.g.:

if TYPE_CHECKING:
    import math

if TYPE_CHECKING:
    assert math

This is because TYPE_CHECKING, like __name__ == '__main__', is false at runtime except under special circumstances, see #6677.

@jacobtylerwalls jacobtylerwalls added this to the 3.0.0 milestone Apr 9, 2023
@jacobtylerwalls jacobtylerwalls added the High priority Issue with more than 10 reactions label Apr 9, 2023
@Pierre-Sassoulas Pierre-Sassoulas added the Blocker 🙅 Blocks the next release label Apr 15, 2023
@jacobtylerwalls jacobtylerwalls modified the milestones: 3.0.0, 2.17.3 Apr 16, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.17.3, 3.0.0b1 Apr 18, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.0.0b1, 3.0.0a7 May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker 🙅 Blocks the next release C: used-before-assignment Issues related to 'used-before-assignment' check Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code High priority Issue with more than 10 reactions Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
5 participants