bpo-17792: more accurate error message for UnboundLocalError#24976
bpo-17792: more accurate error message for UnboundLocalError#24976iritkatriel merged 7 commits intopython:mainfrom
Conversation
|
This PR is stale because it has been open for 30 days with no activity. |
nanjekyejoannah
left a comment
There was a problem hiding this comment.
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? |
vstinner
left a comment
There was a problem hiding this comment.
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 |
vstinner
left a comment
There was a problem hiding this comment.
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>
vstinner
left a comment
There was a problem hiding this comment.
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