-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Bug description
"""
Demonstrate an overlooked possibly-used-before-assignment error
"""
def false_negative1():
"""False negative possibly-used-before-assignment"""
if object() is None:
data={"cat": "harf"}
# The following line should raise a possibly-used-before-assignment error, but does not
token: str = data.get("cat")
print(token)
def false_negative2():
"""False negative possibly-used-before-assignment"""
for _ in range(3):
if object() is None:
data={"cat": "harf"}
# The following line should raise a possibly-used-before-assignment error, but does not
token: str = data.get("cat")
print(token)
def true_positive1():
"""pylint (correctly) identifies the possibly-used-before-assignment error in this function"""
if object() is None:
data={"cat": "harf"}
# The following line is correctly flagged as possibly-used-before-assignment
token = data.get("cat")
print(token)
def true_positive2():
"""pylint (correctly) identifies the possibly-used-before-assignment error in this function"""
for _ in range(3):
if object() is None:
data={"cat": "harf"}
# The following line is correctly flagged as possibly-used-before-assignment
token = data.get("cat")
print(token)
def true_positive3():
"""pylint (correctly) identifies the undefined-variable error in this function"""
# The following line is correctly flagged as undefined-variable
token: str = data.get("cat")
print(token)
From what I can tell, if a variable is assigned inside of a conditional or a loop, such that later use would ordinarily provoke a possibly-used-before-assignment
error, this error is NOT given if the use in question is on the right side of a type-annotated assignment expression.
true_positive1
and true_positive2
show that omitting the type annotation allows the error to correctly be reported.
true_positive3
shows that while the type annotation does suppress the possibly-used-before-assignment
error, it will not suppress an undefined-variable
error.
Command used
pylint repro.py
Pylint output
************* Module repro
repro.py:27:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:36:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:42:17: E0602: Undefined variable 'data' (undefined-variable)
Expected behavior
************* Module repro
repro.py:10:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:19:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:27:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:36:12: E0606: Possibly using variable 'data' before assignment (possibly-used-before-assignment)
repro.py:42:17: E0602: Undefined variable 'data' (undefined-variable)
Pylint version
pylint 3.3.7
astroid 3.3.10
Python 3.13.3 (main, May 9 2025, 23:49:05) [GCC 12.2.0]
I tested this on older Python/pylint versions and this bug existed on all of them. (On some of them, it also missed some of the errors caught by the current version, but none of the older versions found the ones that are currently missed).
OS / Environment
docker.io/python:3.13