Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading