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

undefined-variable/possibly-used-before-assignment/used-before-assignment false positives for global #9633

Open
nikita-leontiev opened this issue May 16, 2024 · 0 comments
Labels
C: undefined-variable Issues related to 'undefined-variable' check C: used-before-assignment Issues related to 'used-before-assignment' 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

Comments

@nikita-leontiev
Copy link

nikita-leontiev commented May 16, 2024

Bug description

  1. Code below leads to undefined-variable false positive:
if __name__ == "__main__":
    global g_object
    if "g_object" not in globals():
        g_object = 1
    g_object += 1
  1. Changing code a little bit: undefined-variable is gone, now error is possibly-used-before-assignment:
if __name__ == "__main__":
    global g_object
    if "g_object" not in globals():
        g_object = 1
    obj = g_object
  1. One code line added: possibly-used-before-assignment is gone, now error is used-before-assignment on the same line:
if __name__ == "__main__":
    global g_object
    if "g_object" not in globals():
        g_object = 1
    obj = g_object
    g_object += 1

Configuration

Without pylintrc

Command used

pylint -sn --disable=C0114,C0103,W0604 test.py

Pylint output

************* Module test
test.py:5:4: E0602: Undefined variable 'g_object' (undefined-variable)
************* Module test
test.py:5:10: E0606: Possibly using variable 'g_object' before assignment (possibly-used-before-assignment)
************* Module test
test.py:5:10: E0601: Using variable 'g_object' before assignment (used-before-assignment)

Expected behavior

No E0602, E0606, E0601 error for this case as it was on pylint 3.1.0.

Pylint version

pylint 3.2.0
astroid 3.2.0
Python 3.8.10 (default, Nov 22 2023, 10:22:35)
[GCC 9.4.0]

OS / Environment

Ubuntu 20.04

Additional dependencies

No response

@nikita-leontiev nikita-leontiev added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 16, 2024
@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 May 16, 2024
@jacobtylerwalls jacobtylerwalls changed the title undefined-variable/possibly-used-before-assignment/used-before-assignment false positives undefined-variable/possibly-used-before-assignment/used-before-assignment false positives for global May 17, 2024
@jacobtylerwalls jacobtylerwalls added the C: used-before-assignment Issues related to 'used-before-assignment' check label May 17, 2024
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 C: used-before-assignment Issues related to 'used-before-assignment' 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
Development

No branches or pull requests

3 participants