diff --git a/stdlib/types.pyi b/stdlib/types.pyi index b9771ffc72da..abe0bc6599f8 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -17,7 +17,7 @@ from collections.abc import ( ValuesView, ) from importlib.machinery import ModuleSpec -from typing import Any, ClassVar, Literal, ParamSpec, TypeVar, final, overload +from typing import Any, ClassVar, Literal, ParamSpec, TypeVar, Union, final, overload # noqa: Y037 from typing_extensions import Self, TypeAliasType, TypeVarTuple, deprecated, disjoint_base if sys.version_info >= (3, 14): @@ -719,25 +719,28 @@ class EllipsisType: ... @final class NotImplementedType(Any): ... -@final -class UnionType: - @property - def __args__(self) -> tuple[Any, ...]: ... - @property - def __parameters__(self) -> tuple[Any, ...]: ... - # `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`). - # Normally we'd express this using the return type of `_SpecialForm.__ror__`, - # but because `UnionType.__or__` accepts `Any`, type checkers will use - # the return type of `UnionType.__or__` to infer the result of this operation - # rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any` - # in the return type of `UnionType.__(r)or__`. - def __or__(self, value: Any, /) -> UnionType | Any: ... - def __ror__(self, value: Any, /) -> UnionType | Any: ... - def __eq__(self, value: object, /) -> bool: ... - def __hash__(self) -> int: ... - # you can only subscript a `UnionType` instance if at least one of the elements - # in the union is a generic alias instance that has a non-empty `__parameters__` - def __getitem__(self, parameters: Any, /) -> object: ... +if sys.version_info >= (3, 14): + UnionType = Union +else: + @final + class UnionType: + @property + def __args__(self) -> tuple[Any, ...]: ... + @property + def __parameters__(self) -> tuple[Any, ...]: ... + # `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`). + # Normally we'd express this using the return type of `_SpecialForm.__ror__`, + # but because `UnionType.__or__` accepts `Any`, type checkers will use + # the return type of `UnionType.__or__` to infer the result of this operation + # rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any` + # in the return type of `UnionType.__(r)or__`. + def __or__(self, value: Any, /) -> UnionType | Any: ... + def __ror__(self, value: Any, /) -> UnionType | Any: ... + def __eq__(self, value: object, /) -> bool: ... + def __hash__(self) -> int: ... + # you can only subscript a `UnionType` instance if at least one of the elements + # in the union is a generic alias instance that has a non-empty `__parameters__` + def __getitem__(self, parameters: Any, /) -> object: ... if sys.version_info >= (3, 13): @final diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 2018a835c632..245ef0c6e24a 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -237,7 +237,31 @@ class _SpecialForm(_Final): def __or__(self, other: Any) -> _SpecialForm: ... def __ror__(self, other: Any) -> _SpecialForm: ... -Union: _SpecialForm +if sys.version_info >= (3, 14): + @final + class Union: + @property + def __args__(self) -> tuple[Any, ...]: ... + @property + def __parameters__(self) -> tuple[Any, ...]: ... + # `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`). + # Normally we'd express this using the return type of `_SpecialForm.__ror__`, + # but because `UnionType.__or__` accepts `Any`, type checkers will use + # the return type of `UnionType.__or__` to infer the result of this operation + # rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any` + # in the return type of `UnionType.__(r)or__`. + def __or__(self, value: Any, /) -> UnionType | Any: ... + def __ror__(self, value: Any, /) -> UnionType | Any: ... + def __eq__(self, value: object, /) -> bool: ... + def __hash__(self) -> int: ... + def __class_getitem__(cls, item: Any) -> Self: ... + # you can only subscript a `Union` instance if at least one of the elements + # in the union is TypeVar or a generic alias instance that has a non-empty `__parameters__` + def __getitem__(self, parameters: Any, /) -> object: ... + +else: + Union: _SpecialForm + Protocol: _SpecialForm Callable: _SpecialForm Type: _SpecialForm