-
Notifications
You must be signed in to change notification settings - Fork 234
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
Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit TypeAlias
es inside if TYPE_CHECKING
blocks as stringized annotations.
#1562
Comments
I'd support this given that type checkers already happily accept this union syntax in type annotations if I don't think the stipulation with |
I'm strongly opposed to this proposal. The whole intention behind I recommend against using If you want to define a type alias using the pre-3.10 syntax, you can do so safely without the use of A: TypeAlias = "int | str" |
This is a good solution, thank you. My only (relatively small) complaint about it is that it is not commonly auto-completed in editors. I'd like to still proceed with the proposal if possible.
While it is true that this would require special-casing and i am not totally "happy" about it too, i don't see this as "lying" to the type-checker. This is what, in both practice and theory, should and does happen at runtime: syntax is valid, but the "semantics" are essentially ignored because
In most (all, actually, but i don't want to be rude) projects that i have seen, "lying" to the type-checker is not what |
While mypy, pyright and others may support many Python versions at the same time, the same cannot be always said about the project being type-checked. This is not actually the case, but imagine if |
You could've used PEP695 as an example, in that case as soon as you use it your project can only support Python 3.12+. I understand what you mean, but it's not really related to your proposal, since it specifically mentions valid syntax with new semantics, i.e. stuff that would throw exceptions at runtime but parses just fine, so I don't see how respecting the Python version in If a type expression is evaluated at runtime it's also part of what the type checker evaluates, so if it sees operations that aren't valid for that version of Python it will (or at least should) complain. But there is the existing exception for forward references (explicit via wrapping in string, or implicit via I can see why unmarked type aliases can be a bit problematic for this proposal, since you essentially need to defer raising an error about invalid operations until you're absolutely certain that the expression is not a type alias. But in an explicit type alias using |
if TYPE_CHECKING
blocks if the minimum Python version a given project claims to support can successfully parse feature's syntax.TypeAlias
es inside if TYPE_CHECKING
blocks as stringized annotations.
@Daverball Appreciate the explanation, this indeed does make sense! I edited the original message to mention these, let me know if i got something wrong! |
The Problem
At runtime,
TYPE_CHECKING
is alwaysFalse
, only type checkers assume it isTrue
. Therefore,if TYPE_CHECKING
blocks are only parsed, but never evaluated by the Python interpreter. SinceA = int | str
is a valid syntax for all still-supported Python versions at the moment (all versions ever sincetyping
was introduced, actually), it would make sense if type checkers were allowed to accept usage of "new-semantics-old-syntax" features (like the alternative union syntax) in explicitly-annotatedTypeAlias
es inside ofif TYPE_CHECKING
blocks as long as the Python version specified[project.requires-python]
(or it's equivalent in tools) can successfully parse that syntax. This is essentially the same as treating explicitly-annotatedTypeAlias
es as stringized annotations by-default.Examples
pyright-play
mypy-play
The text was updated successfully, but these errors were encountered: