pylint
pylint copied to clipboard
E0601 false positive when nested try block exhaustively defines name, raises, or returns
Bug description
This produces a using 'x' before assignment error:
"""pylint_false_positive.py"""
def test():
try:
try:
x = None
except:
x = None
raise
finally:
print("x", x)
Configuration
No response
Command used
pylint -E pylint_false_positive.py
Pylint output
************* Module pylint_false_positive
pylint_false_positive.py:10: [E0601(used-before-assignment), test] Using variable 'x' before assignment
Expected behavior
Expected no error, x is assigned in all branches.
Pylint version
pylint 2.14.5
astroid 2.11.7
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
OS / Environment
No response
Additional dependencies
No response
I think this is correct? Pinging @jacobtylerwalls for an opinion.
If you change this to:
"""pylint_false_positive.py"""
def test():
try:
try:
x = 1 / 0
except:
x = 1 / 0
raise
finally:
print("x", x)
test()
This won't run and an UnboundLocalError will be raised. Is that we are guarding against here @jacobtylerwalls?
Yes, or even:
"""pylint_false_positive.py"""
def test():
try:
1 / 0
try:
x = None
except:
x = None
raise
finally:
print("x", x)
test()
We just currently lack a "comprehensivity" check that "nothing else is being done in the outer try". Theoretically we could, so I'm happy to leave the issue open for now, but it could be that ultimately we determine it doesn't bring enough value for the effort involved.