-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Support PEP 604 - union types as X | Y #9610
Comments
We should definitely add support, and it's great there's already an implementation by @pprados. Looks like this feature will be available only in 3.10, so there's no great hurry. I'm guessing we'll also need typeshed changes to add |
@JelleZijlstra That's not entirely correct. Although from __future__ import annotations
t1: str | None = "Hello World"
t2 = "Hello World" # type: str | None What doesn't work are type aliases. The following would cause a type error: from __future__ import annotations
T1 = str | None # TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
d: T1 = "Hello World" |
@cdce8p it occurred to me that |
@hauntsaninja Thanks for letting me know. To be honest, I just wanted to replace |
Would you be up for making the needed changes to typeshed / adding a test to mypy with an |
I can try. No promises :) |
are type aliases supposed to work? ISTM from reading PEP0604 that they are, but mypy is still failing on them |
@matejcik yes, they should work (always in stubs and at runtime in Python >=3.10). If they don't, that's a bug in mypy. |
Runtime uses of the PEP 604 aren't supported by |
Feature
Add support for PEP 604 to mypy. Since it has been officially accepted, should mypy add support for
X | Y
now? The syntax won't change anymore, is backwards compatible and there is an existing implementation https://github.com/pprados/mypy/tree/PEP604Pitch
Adding this now would allow projects to already take advantage of the new syntax for type checking when using
from __future__ import annotations
.The text was updated successfully, but these errors were encountered: