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
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2017-03-07.19:24:04.815>created_at=<Date2012-11-07.14:59:26.405>labels= ['type-feature']
title='Emit SyntaxWarning for code that risks UnboundLocalError'updated_at=<Date2017-03-07.19:24:04.814>user='https://github.com/ncoghlan'
The compiler is actually in a fairly good position to tell when code is at risk of triggering UnboundLocalError at runtime: specifically, in the section of the code that checks for duplicated parameter names [1]
Now, we can't emit SyntaxError here for backwards compatibility reasons (if you have an early reference that is never executed, your code is dodgy but will still run OK).
However, we should be able to emit a Syntax *Warning* when we detect an existing symbol at function scope having DEF_LOCAL applied for the first time *after* it has already been referenced in a way which doesn't create a local variable.
Something like:
SyntaxWarning: Local variable NAME bound after earlier reference (risks UnboundLocalError when function is called)
I seem to recall that this was rejected into the realm of linters in a python-dev discussion.
If there is a chance of false positives, having Python emit the warning would be annoying because there is no convenient way of telling it to shut up about it. At least people can choose not to run linters, or configure them to their taste.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: