diff --git a/stdlib/asyncio/coroutines.pyi b/stdlib/asyncio/coroutines.pyi index e514b884c..df94d5ba1 100644 --- a/stdlib/asyncio/coroutines.pyi +++ b/stdlib/asyncio/coroutines.pyi @@ -1,7 +1,16 @@ -from typing import Any, Callable, TypeVar +import sys +import types +from collections.abc import Callable, Coroutine +from typing import Any, TypeVar +from typing_extensions import TypeGuard _F = TypeVar("_F", bound=Callable[..., Any]) def coroutine(func: _F) -> _F: ... def iscoroutinefunction(func: object) -> bool: ... -def iscoroutine(obj: object) -> bool: ... + +if sys.version_info < (3, 8): + def iscoroutine(obj: object) -> TypeGuard[types.GeneratorType[Any, Any, Any] | Coroutine[Any, Any, Any]]: ... + +else: + def iscoroutine(obj: object) -> TypeGuard[Coroutine[Any, Any, Any]]: ... diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index e14718777..b868e644f 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -62,7 +62,7 @@ else: def iscoroutinefunction(object: object) -> bool: ... def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ... -def iscoroutine(object: object) -> TypeGuard[CoroutineType]: ... +def iscoroutine(object: object) -> TypeGuard[CoroutineType[Any, Any, Any]]: ... def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ... if sys.version_info >= (3, 8): diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 26d8263ea..0df2c35a4 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -6,6 +6,7 @@ from typing import ( AsyncGenerator, Awaitable, Callable, + Coroutine, Generator, Generic, ItemsView, @@ -211,7 +212,7 @@ class AsyncGeneratorType(AsyncGenerator[_T_co, _T_contra]): def aclose(self) -> Awaitable[None]: ... @final -class CoroutineType: +class CoroutineType(Coroutine[_T_co, _T_contra, _V_co]): __name__: str __qualname__: str cr_await: Any | None @@ -219,12 +220,14 @@ class CoroutineType: cr_frame: FrameType cr_running: bool def close(self) -> None: ... - def __await__(self) -> Generator[Any, None, Any]: ... - def send(self, __arg: Any) -> Any: ... + def __await__(self) -> Generator[Any, None, _V_co]: ... + def send(self, __arg: _T_contra) -> _T_co: ... @overload - def throw(self, __typ: Type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ...) -> Any: ... + def throw( + self, __typ: Type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ... + ) -> _T_co: ... @overload - def throw(self, __typ: BaseException, __val: None = ..., __tb: TracebackType | None = ...) -> Any: ... + def throw(self, __typ: BaseException, __val: None = ..., __tb: TracebackType | None = ...) -> _T_co: ... class _StaticFunctionType: """Fictional type to correct the type of MethodType.__func__. @@ -365,7 +368,7 @@ def prepare_class( # Actually a different type, but `property` is special and we want that too. DynamicClassAttribute = property -def coroutine(func: Callable[..., Any]) -> CoroutineType: ... +def coroutine(func: Callable[..., Any]) -> CoroutineType[Any, Any, Any]: ... if sys.version_info >= (3, 8): CellType = _Cell