-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-17792: more accurate error message for UnboundLocalError #24976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This PR is stale because it has been open for 30 days with no activity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do I think the error message is fine as is, mentioning the delete aspect doesn't sit so well with me, I mean the actual state is that the variable is unbound, which is standard enough. I don't know what @vstinner thinks of this?
Well, the current message doesn't just say the variable is unbound. It says " local variable 'x' referenced before assignment", which is incorrect in my example. |
I see what you mean but my reasoning is, "before assignment" is kind of a synonym for unbound, which sort of makes sense to me, no? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I'm used used to "using a variable" than "referencing a variable", but I'm fine with "local variable could have been referenced ...".
cc @pablogsal
We could do something like ‘using an unassigned variable’ if that sound better. |
English is not my first language, so I let the others decide. I approved the PR since it fix an issue and it's correct ;-) |
I would prefer to use something generic that doesn't refer to any specific operation as there are other ways to trigger this that are not technically assignments or deletions. FOr example:
|
I agree, how about one of these? UnboundLocalError: local variable 'lel' is not associated with a value UnboundLocalError: reading undefined local variable 'lel' |
I personally think that these are still quite confusing. For example:
I think people will still be confused with the message given that in:
they think the variable is defined.
Same as before, people can still think that the variable is associated with a value later. Maybe we could modify this slightly to:
Maybe is too verbose.... |
Shorter: UnboundLocalError: accessed local variable 'lel' where it is not associated with a value |
Looks great! |
I noticed another message just below for free variables - I think the same applied there -- see this unit test cpython/Lib/test/test_scope.py Line 270 in bb3e0c2
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest "cannot" :-)
%.200s
is a ghost of the past, it can be removed.
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but again, English is not my first language, so you should better rely on others opinion than mine :-D
@pablogsal @nanjekyejoannah Are we all good here? |
Ok from me 👍 |
Currently:
With this patch:
UnboundLocalError: local variable 'x' referenced before assignment or after deletion
https://bugs.python.org/issue17792