You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
'local variable referenced before assignment' false positive #315
I've vastly simplified this bit of code to make it easier to look at.
I understand this code isn't necessarily best practice, but I ran across this in a project at work, and it took me a while to hunt down what the error message was referring to.
frommoduleimporterrorsdeftest_f823():
try:
errors.blah('some_value') # this line should not produce a pyflakes errorexceptExceptionaserror:
errors= [error]
returnerrors
output: /tmp/tester.py:6: local variable 'errors' (defined in enclosing scope on line 1) referenced before assignment
I don't have time to look into the details right now, but this seems somewhat reminiscent of previous conditional-related issues we've seen this in the past (like #252).
(Though, in this particular example, there would an error in the code even if weren't for the errors.blah() false positive. Pyflakes would rightfully complain about the redefinition of errors.)
But it is an error. Try this variation, which is a program we should expect to print hello world!:
frompprintimportpprintdeftest_f823():
try:
pprint('hello world!') # this line should not produce a pyflakes errorexceptExceptionaserror:
pprint= [error]
returnpprintif__name__=='__main__':
print(test_f823())
$ python3 test.py[UnboundLocalError("local variable 'pprint' referenced before assignment",)]
$ python2 test.py[UnboundLocalError("local variable 'pprint' referenced before assignment",)]
Activity
myint commentedon Nov 28, 2017
I don't have time to look into the details right now, but this seems somewhat reminiscent of previous conditional-related issues we've seen this in the past (like #252).
(Though, in this particular example, there would an error in the code even if weren't for the
errors.blah()false positive. Pyflakes would rightfully complain about the redefinition oferrors.)bitglue commentedon Nov 28, 2017
But it is an error. Try this variation, which is a program we should expect to print
hello world!:sigmavirus24 commentedon Nov 28, 2017
Right. This isn't a matter of pyflakes being nitpicky, this is totally a language issue.
bitglue commentedon Nov 28, 2017
I'm closing this as it appears this is a true positive. Please reopen if there's something I'm overlooking.