Skip to content

Commit 11b79a8

Browse files
committed
Merge remote-tracking branch 'Viicos/union-314' into union-314
2 parents d928f9e + 0069252 commit 11b79a8

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

stdlib/types.pyi

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from collections.abc import (
1717
ValuesView,
1818
)
1919
from importlib.machinery import ModuleSpec
20-
from typing import Any, ClassVar, Literal, ParamSpec, TypeVar, final, overload
20+
from typing import Any, ClassVar, Literal, ParamSpec, TypeVar, Union, final, overload
2121
from typing_extensions import Self, TypeAliasType, TypeVarTuple, deprecated, disjoint_base
2222

2323
if sys.version_info >= (3, 14):
@@ -719,25 +719,28 @@ class EllipsisType: ...
719719
@final
720720
class NotImplementedType(Any): ...
721721

722-
@final
723-
class UnionType:
724-
@property
725-
def __args__(self) -> tuple[Any, ...]: ...
726-
@property
727-
def __parameters__(self) -> tuple[Any, ...]: ...
728-
# `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`).
729-
# Normally we'd express this using the return type of `_SpecialForm.__ror__`,
730-
# but because `UnionType.__or__` accepts `Any`, type checkers will use
731-
# the return type of `UnionType.__or__` to infer the result of this operation
732-
# rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any`
733-
# in the return type of `UnionType.__(r)or__`.
734-
def __or__(self, value: Any, /) -> UnionType | Any: ...
735-
def __ror__(self, value: Any, /) -> UnionType | Any: ...
736-
def __eq__(self, value: object, /) -> bool: ...
737-
def __hash__(self) -> int: ...
738-
# you can only subscript a `UnionType` instance if at least one of the elements
739-
# in the union is a generic alias instance that has a non-empty `__parameters__`
740-
def __getitem__(self, parameters: Any, /) -> object: ...
722+
if sys.version_info >= (3, 14):
723+
UnionType = Union
724+
else:
725+
@final
726+
class UnionType:
727+
@property
728+
def __args__(self) -> tuple[Any, ...]: ...
729+
@property
730+
def __parameters__(self) -> tuple[Any, ...]: ...
731+
# `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`).
732+
# Normally we'd express this using the return type of `_SpecialForm.__ror__`,
733+
# but because `UnionType.__or__` accepts `Any`, type checkers will use
734+
# the return type of `UnionType.__or__` to infer the result of this operation
735+
# rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any`
736+
# in the return type of `UnionType.__(r)or__`.
737+
def __or__(self, value: Any, /) -> UnionType | Any: ...
738+
def __ror__(self, value: Any, /) -> UnionType | Any: ...
739+
def __eq__(self, value: object, /) -> bool: ...
740+
def __hash__(self) -> int: ...
741+
# you can only subscript a `UnionType` instance if at least one of the elements
742+
# in the union is a generic alias instance that has a non-empty `__parameters__`
743+
def __getitem__(self, parameters: Any, /) -> object: ...
741744

742745
if sys.version_info >= (3, 13):
743746
@final

stdlib/typing.pyi

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,21 @@ class _SpecialForm(_Final):
237237
def __or__(self, other: Any) -> _SpecialForm: ...
238238
def __ror__(self, other: Any) -> _SpecialForm: ...
239239

240-
Union: _SpecialForm
240+
if sys.version_info >= (3, 14):
241+
@final
242+
class Union:
243+
@property
244+
def __args__(self) -> tuple[Any, ...]: ...
245+
@property
246+
def __parameters__(self) -> tuple[Any, ...]: ...
247+
def __or__(self, value: Any, /) -> UnionType: ...
248+
def __ror__(self, value: Any, /) -> UnionType: ...
249+
def __eq__(self, value: object, /) -> bool: ...
250+
def __hash__(self) -> int: ...
251+
def __class_getitem__(cls, item: Any, /) -> Self: ...
252+
else:
253+
Union: _SpecialForm
254+
241255
Protocol: _SpecialForm
242256
Callable: _SpecialForm
243257
Type: _SpecialForm

0 commit comments

Comments
 (0)