Use recursive type aliases in builtins and _typeshed (#9134)

This commit is contained in:
Alex Waygood
2022-11-08 17:04:09 +00:00
committed by GitHub
parent efa1048d42
commit 739460291b
2 changed files with 5 additions and 12 deletions

View File

@@ -276,5 +276,4 @@ StrOrLiteralStr = TypeVar("StrOrLiteralStr", LiteralString, str) # noqa: Y001
ProfileFunction: TypeAlias = Callable[[FrameType, str, Any], object]
# Objects suitable to be passed to sys.settrace, threading.settrace, and similar
# TODO: Ideally this would be a recursive type alias
TraceFunction: TypeAlias = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]
TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None]

View File

@@ -1325,19 +1325,13 @@ def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]:
@overload
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...
# We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any`
if sys.version_info >= (3, 10):
def isinstance(
__obj: object, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...]
) -> bool: ...
def issubclass(
__cls: type, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...]
) -> bool: ...
_ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
else:
def isinstance(__obj: object, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ...
def issubclass(__cls: type, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ...
_ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]
def isinstance(__obj: object, __class_or_tuple: _ClassInfo) -> bool: ...
def issubclass(__cls: type, __class_or_tuple: _ClassInfo) -> bool: ...
def len(__obj: Sized) -> int: ...
def license() -> None: ...
def locals() -> dict[str, Any]: ...