Skip to content
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

References to names before they're bound should generate an error (UnboundLocalError) #686

Closed
spkersten opened this issue May 18, 2015 · 9 comments · Fixed by #14203
Closed
Labels

Comments

@spkersten
Copy link
Contributor

This code raises an error when executed but passes type checking:

def f(x: A): pass

y = A()

class A: pass
@spkersten
Copy link
Contributor Author

I'm working on a solution for this issue in my 'scope' branch.

This was referenced May 18, 2015
@JukkaL JukkaL added the feature label May 19, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented May 19, 2015

Yup, mypy is currently too lenient, and catching these errors would be useful.

This should still be fine and we shouldn't complain about this:

def f(x: 'A'):
    y = A()

class A: pass

@carljm
Copy link
Member

carljm commented Mar 8, 2018

In Python 3.7, with from __future__ import annotations, the original example here is fine at runtime and should not raise an error in mypy either. See https://www.python.org/dev/peps/pep-0563/

Given that from __future__ import annotations is supposed to become the default behavior over time, perhaps implementing this form of strictness is not a high priority.

@JukkaL
Copy link
Collaborator

JukkaL commented May 17, 2018

This is confusing for new users and they might not know about from __future__ import annotations so it would still be nice to generate better error messages.

@DetachHead
Copy link
Contributor

another example that from __future__ import annotations doesn't fix:

foo() # runtime NameError: name 'foo' is not defined

def foo():
    ...

@PeterJCLaw
Copy link
Contributor

Should we expect mypy to catch cases where the name could have been defined, such as in a try...except? IMO it would be great if it could, though I realise it's a bit more involved than the other cases described here:

try:
   raise ValueError
   bar = 3
except Exception:
   print(bar)

Obviously this code actually produces NameError rather than UnboundLocalError but the idea is the same.

@ilevkivskyi
Copy link
Member

@ilinum I think the original example in this issue should result in use-before-def for the y = A() line, but it looks like it doesn't. Are you specifically excluding references to types? I think we should only allow forward references to types in type context (e.g. rvalue of a type alias definition), but not in runtime context.

@ilinum
Copy link
Collaborator

ilinum commented Nov 27, 2022

#14203 should fix this for classes and functions.

ilinum added a commit that referenced this issue Nov 29, 2022
Previously, we would ignore any class definitions and would fail to
detect undefined classes and functions. This updates the logic to handle
them.

Closes #686
@PeterJCLaw
Copy link
Contributor

Connecting the dots: looks like #4019 tracks the local variable case.

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

Successfully merging a pull request may close this issue.

9 participants