add TypeGuard to coroutines.iscoroutine (#6105)

make CoroutineType extend Coroutine
This commit is contained in:
KotlinIsland
2021-10-10 01:01:36 +10:00
committed by GitHub
parent e018ad66dc
commit b7d1d099d9
3 changed files with 21 additions and 9 deletions

View File

@@ -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]]: ...

View File

@@ -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):

View File

@@ -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