Skip to content

add TypeGuard to coroutines.iscoroutine #6105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2021
Merged
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
13 changes: 11 additions & 2 deletions stdlib/asyncio/coroutines.pyi
Original file line number Diff line number Diff line change
@@ -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]]: ...
2 changes: 1 addition & 1 deletion stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 9 additions & 6 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from typing import (
AsyncGenerator,
Awaitable,
Callable,
Coroutine,
Generator,
Generic,
ItemsView,
Expand Down Expand Up @@ -211,20 +212,22 @@ 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
cr_code: CodeType
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__.
Expand Down Expand Up @@ -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
Expand Down