-
-
Notifications
You must be signed in to change notification settings - Fork 30.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
Merge typing.Union
and types.UnionType
#105499
Comments
I put up a first implementation at #105511. Some thoughts on the details:
|
This broken an AST walker I had written for Python 3.9, after I upgraded all my |
Will this also fix the issue of i.e. class Foo:
def get_self(self) -> "Foo" | None:
pass
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "<stdin>", line 2, in Foo
# TypeError: unsupported operand type(s) for |: 'str' and 'NoneType'
from typing import Union
class Foo:
def get_self(self) -> Union["Foo", None]:
pass
# no issues |
@MarsCapone, no, but other changes in Python 3.14 (PEP-649) will fix that case. |
See also https://mypy.readthedocs.io/en/stable/runtime_troubles.html and |
Currently, unions created through
typing.Union[A, B]
and through the PEP-604 syntaxA | B
are at runtime instances of completely different types, and they differ in exactly what elements they accept. This is confusing and makes it harder for users to detect unions at runtime.I propose to proceed in two steps:
typing.Union
an alias fortypes.UnionType
and make it sotypes.UnionType[A, B]
works, accepting the same typesUnion
accepts now.|
operator accepts to accept more types that are commonly used in unions.Linked PRs
The text was updated successfully, but these errors were encountered: