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
{{ message }}
Repository navigation
References to names before they're bound should generate an error (UnboundLocalError) #686
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.
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.
changed the title [-]References to names before they're bound should generate an error[/-][+]References to names before they're bound should generate an error (UnboundLocalError)[/+]on Aug 25, 2022
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:
@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.
Activity
spkersten commentedon May 18, 2015
I'm working on a solution for this issue in my 'scope' branch.
JukkaL commentedon 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:
carljm commentedon 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 annotationsis supposed to become the default behavior over time, perhaps implementing this form of strictness is not a high priority.JukkaL commentedon May 17, 2018
This is confusing for new users and they might not know about
from __future__ import annotationsso it would still be nice to generate better error messages.DetachHead commentedon Mar 21, 2022
another example that
from __future__ import annotationsdoesn't fix:[-]References to names before they're bound should generate an error[/-][+]References to names before they're bound should generate an error (UnboundLocalError)[/+]PeterJCLaw commentedon Nov 3, 2022
Should we expect
mypyto catch cases where the name could have been defined, such as in atry...except? IMO it would be great if it could, though I realise it's a bit more involved than the other cases described here:Obviously this code actually produces
NameErrorrather thanUnboundLocalErrorbut the idea is the same.ilevkivskyi commentedon Nov 26, 2022
@ilinum I think the original example in this issue should result in
use-before-deffor they = 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 commentedon Nov 27, 2022
#14203 should fix this for classes and functions.
[use before def] handle class and function definitions (#14203)
PeterJCLaw commentedon Nov 29, 2022
Connecting the dots: looks like #4019 tracks the local variable case.