Skip to content

PEP 604 and aliases #9945

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

Closed
asottile opened this issue Jan 23, 2021 · 3 comments
Closed

PEP 604 and aliases #9945

asottile opened this issue Jan 23, 2021 · 3 comments
Labels

Comments

@asottile
Copy link
Contributor

Feature

PEP 604 | operator should be sufficient to make an alias

Pitch

This is accepted:

from typing import Union
Alias = Union[int, str]
def f(x: Alias) -> None: ...
f(1)
f('2')

so the equivalent post-PEP-604 I believe should be allowed as well?

Alias = int | str
def f(x: Alias) -> None: ...
f(1)
f('2')

currently it produces this output:

$ mypy t.py --python-version 3.10
t.py:1: error: Unsupported left operand type for | ("Type[int]")
t.py:2: error: Variable "t.Alias" is not valid as a type
t.py:2: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 2 errors in 1 file (checked 1 source file)
$ cat t.py 
Alias = int | str
def f(x: Alias) -> None: ...
f(1)
f('2')
$ mypy --version
mypy 0.800
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 23, 2021

Yeah, the PEP 604 support in mypy (and typeshed) doesn't yet cover anything that would require runtime support. This is the same as #9880

@gvanrossum
Copy link
Member

Probably we should first support PEP 613 (TypeAlias) and then you can write

A: TypeAlias = int | str

@asottile
Copy link
Contributor Author

cool -- will follow #9880 instead 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants