Skip to content

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

Merged
merged 7 commits into from
Jun 2, 2021

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Mar 22, 2021

Currently:

>>> def g():
...    x = 42
...    del x
...    print(x)
...
>>> g()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in g
UnboundLocalError: local variable 'x' referenced before assignment
>>>

With this patch:

UnboundLocalError: local variable 'x' referenced before assignment or after deletion

https://bugs.python.org/issue17792

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 22, 2021
@iritkatriel iritkatriel requested a review from warsaw May 14, 2021 11:13
Copy link
Contributor

@nanjekyejoannah nanjekyejoannah left a 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?

@iritkatriel
Copy link
Member Author

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.

@nanjekyejoannah
Copy link
Contributor

nanjekyejoannah commented May 29, 2021

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?

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label May 30, 2021
Copy link
Member

@vstinner vstinner left a 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

@iritkatriel
Copy link
Member Author

iritkatriel commented May 31, 2021

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?

We could do something like ‘using an unassigned variable’ if that sound better.

@iritkatriel iritkatriel reopened this May 31, 2021
@vstinner
Copy link
Member

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 ;-)

@pablogsal
Copy link
Member

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:

>>> def blech(x):
...    print(lel)
...    import lel
...
>>> blech(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in blech
UnboundLocalError: local variable 'lel' referenced before assignment

@iritkatriel
Copy link
Member Author

I agree, how about one of these?

UnboundLocalError: local variable 'lel' is not associated with a value

UnboundLocalError: reading undefined local variable 'lel'

@pablogsal
Copy link
Member

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:

UnboundLocalError: reading undefined local variable 'lel'

I think people will still be confused with the message given that in:

x = 4
def foo():
    print(x)
    x = 3

they think the variable is defined.

UnboundLocalError: local variable 'lel' is not associated with a value

Same as before, people can still think that the variable is associated with a value later.

Maybe we could modify this slightly to:

UnboundLocalError: local variable 'lel' is not associated with a value at the time it was accessed

Maybe is too verbose....

@iritkatriel
Copy link
Member Author

iritkatriel commented May 31, 2021

UnboundLocalError: local variable 'lel' is not associated with a value at the time it was accessed

Shorter:

UnboundLocalError: accessed local variable 'lel' where it is not associated with a value

@pablogsal
Copy link
Member

UnboundLocalError: local variable 'lel' is not associated with a value at the time it was accessed

Shorter:

UnboundLocalError: accessed local variable 'lel' where it is not associated with a value

Looks great!

@iritkatriel
Copy link
Member Author

I noticed another message just below for free variables - I think the same applied there -- see this unit test

def testUnboundLocal_AfterDel(self):

Copy link
Member

@vstinner vstinner left a 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.

iritkatriel and others added 2 commits June 1, 2021 12:46
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a 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

@iritkatriel
Copy link
Member Author

@pablogsal @nanjekyejoannah Are we all good here?

@pablogsal
Copy link
Member

Are we all good here?

Ok from me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants