pylint icon indicating copy to clipboard operation
pylint copied to clipboard

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

Open nikita-leontiev opened this issue 1 month ago • 0 comments

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 avatar May 16 '24 02:05 nikita-leontiev