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 commented on May 18, 2015
I'm working on a solution for this issue in my 'scope' branch.
JukkaL commented on 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 commented on 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 commented on 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 commented on Mar 21, 2022
another example that from __future__ import annotations doesn't fix:
PeterJCLaw commented on Nov 3, 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:
Obviously this code actually produces NameError rather than UnboundLocalError but the idea is the same.
ilevkivskyi commented on Nov 26, 2022
@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 commented on Nov 27, 2022
#14203 should fix this for classes and functions.
[use before def] handle class and function definitions (#14203)
PeterJCLaw commented on Nov 29, 2022
Connecting the dots: looks like #4019 tracks the local variable case.