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

Support PEP 604 - union types as X | Y #9610

Closed
cdce8p opened this issue Oct 17, 2020 · 9 comments · Fixed by #9647
Closed

Support PEP 604 - union types as X | Y #9610

cdce8p opened this issue Oct 17, 2020 · 9 comments · Fixed by #9647
Labels

Comments

@cdce8p
Copy link
Collaborator

cdce8p commented Oct 17, 2020

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/PEP604

Pitch
Adding this now would allow projects to already take advantage of the new syntax for type checking when using
from __future__ import annotations.

@cdce8p cdce8p added the feature label Oct 17, 2020
@JelleZijlstra
Copy link
Member

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 type.__or__.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Oct 18, 2020

@JelleZijlstra That's not entirely correct. Although type.__or__ will only be added in 3.10, X | Y for typing is backwards compatible. The AST parser already knows how to handle it. I did some tests with the original implementation. Type comments work without issue. For type annotations from __future__ import annotations is necessary PEP 563.

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"

@hauntsaninja
Copy link
Collaborator

@cdce8p it occurred to me that isinstance(x, int | str) won't yet work. As Jelle mentioned, that'll probably require typeshed changes, but thought I'd flag in case you want to try getting that working :-)

@cdce8p
Copy link
Collaborator Author

cdce8p commented Dec 2, 2020

@hauntsaninja Thanks for letting me know. To be honest, I just wanted to replace Optional 😅
Although with the PR now finished, the Typeshed changes should be all to fully support PEP604 once 3.10 is released (if I didn't miss anything)

@hauntsaninja
Copy link
Collaborator

Would you be up for making the needed changes to typeshed / adding a test to mypy with an isinstance check (with --python-version 3.10, of course)? (no rush, just at some point in the next year 😊)

@cdce8p
Copy link
Collaborator Author

cdce8p commented Dec 3, 2020

I can try. No promises :)
At the moment I'm still waiting for a review on open Typeshed PR. Would you mind taking a look? I don't seem to get any new responses. python/typeshed#4752

@matejcik
Copy link

matejcik commented Apr 1, 2021

What doesn't work are type aliases.

are type aliases supposed to work? ISTM from reading PEP0604 that they are, but mypy is still failing on them

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Apr 1, 2021

@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.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Apr 1, 2021

Runtime uses of the PEP 604 aren't supported by mypy yet, see: #9880

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.

4 participants