Skip to content

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

Open
@nikita-leontiev

Description

@nikita-leontiev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: undefined-variableIssues related to 'undefined-variable' checkC: used-before-assignmentIssues related to 'used-before-assignment' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions