diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index 0c903d28d808..fdc18c919360 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -2,21 +2,15 @@ import enum import sys from _typeshed import Self from collections import OrderedDict -from collections.abc import Awaitable, Callable, Generator, Mapping, Sequence, Set -from types import ( - AsyncGeneratorType, - BuiltinFunctionType, - CodeType, - CoroutineType, - FrameType, - FunctionType, - GeneratorType, - MethodType, - ModuleType, - TracebackType, -) -from typing import Any, ClassVar, NamedTuple, Tuple, Type, Union -from typing_extensions import Literal, TypeGuard +from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator, Mapping, Sequence, Set +from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType +from typing import Any, ClassVar, NamedTuple, Tuple, Type, TypeVar, Union +from typing_extensions import Literal, ParamSpec, TypeGuard + +_T = TypeVar("_T") +_P = ParamSpec("_P") +_SendType = TypeVar("_SendType") +_YieldType = TypeVar("_YieldType") # # Types and members @@ -48,29 +42,41 @@ TPFLAGS_IS_ABSTRACT: int def getmembers(object: object, predicate: Callable[[Any], bool] | None = ...) -> list[Tuple[str, Any]]: ... def getmodulename(path: str) -> str | None: ... def ismodule(object: object) -> TypeGuard[ModuleType]: ... -def isclass(object: object) -> TypeGuard[Type[Any]]: ... +def isclass(object: Type[_T] | object) -> TypeGuard[Type[_T]]: ... def ismethod(object: object) -> TypeGuard[MethodType]: ... def isfunction(object: object) -> TypeGuard[FunctionType]: ... if sys.version_info >= (3, 8): - def isgeneratorfunction(obj: object) -> bool: ... - def iscoroutinefunction(obj: object) -> bool: ... + def isgeneratorfunction( + obj: Callable[_P, Generator[_YieldType, _SendType, _T]] | object + ) -> TypeGuard[Callable[_P, Generator[_YieldType, _SendType, _T]]]: ... # type: ignore + def iscoroutinefunction( + obj: Callable[_P, Coroutine[_YieldType, _SendType, _T]] | object + ) -> TypeGuard[Callable[_P, Coroutine[_YieldType, _SendType, _T]]]: ... # type: ignore else: - def isgeneratorfunction(object: object) -> bool: ... - def iscoroutinefunction(object: object) -> bool: ... + def isgeneratorfunction( + object: Callable[_P, Generator[_YieldType, _SendType, _T]] | object + ) -> TypeGuard[Callable[_P, Generator[_YieldType, _SendType, _T]]]: ... # type: ignore + def iscoroutinefunction( + object: Callable[_P, Coroutine[_YieldType, _SendType, _T]] | object + ) -> TypeGuard[Callable[_P, Coroutine[_YieldType, _SendType, _T]]]: ... # type: ignore -def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ... -def iscoroutine(object: object) -> TypeGuard[CoroutineType]: ... -def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ... +def isgenerator(object: Generator[_YieldType, _SendType, _T] | object) -> TypeGuard[Generator[_YieldType, _SendType, _T]]: ... +def iscoroutine(object: Coroutine[_YieldType, _SendType, _T] | object) -> TypeGuard[Coroutine[_YieldType, _SendType, _T]]: ... +def isawaitable(object: Awaitable[_T] | object) -> TypeGuard[Awaitable[_T]]: ... if sys.version_info >= (3, 8): - def isasyncgenfunction(obj: object) -> bool: ... + def isasyncgenfunction( + obj: Callable[_P, AsyncGenerator[_YieldType, _SendType]] | object + ) -> TypeGuard[Callable[_P, AsyncGenerator[_YieldType, _SendType]]]: ... # type: ignore else: - def isasyncgenfunction(object: object) -> bool: ... + def isasyncgenfunction( + object: Callable[_P, AsyncGenerator[_YieldType, _SendType]] | object + ) -> TypeGuard[Callable[_P, AsyncGenerator[_YieldType, _SendType]]]: ... # type: ignore -def isasyncgen(object: object) -> TypeGuard[AsyncGeneratorType[Any, Any]]: ... +def isasyncgen(object: AsyncGenerator[_YieldType, _SendType] | object) -> TypeGuard[AsyncGenerator[_YieldType, _SendType]]: ... def istraceback(object: object) -> TypeGuard[TracebackType]: ... def isframe(object: object) -> TypeGuard[FrameType]: ... def iscode(object: object) -> TypeGuard[CodeType]: ...