|
| 1 | +import sys |
| 2 | +from asyncio.events import AbstractEventLoop |
| 3 | +from collections.abc import Awaitable, Callable, Coroutine, Generator, Iterable |
| 4 | +from contextvars import Context |
| 5 | +from types import FrameType |
| 6 | +from typing import Any, Literal, TextIO, TypeVar |
| 7 | +from typing_extensions import Self, TypeAlias |
| 8 | + |
| 9 | +if sys.version_info >= (3, 9): |
| 10 | + from types import GenericAlias |
| 11 | + |
| 12 | +_T = TypeVar("_T") |
| 13 | +_T_co = TypeVar("_T_co", covariant=True) |
| 14 | +_TaskYieldType: TypeAlias = Future[object] | None |
| 15 | + |
| 16 | +class Future(Awaitable[_T], Iterable[_T]): |
| 17 | + _state: str |
| 18 | + @property |
| 19 | + def _exception(self) -> BaseException | None: ... |
| 20 | + _blocking: bool |
| 21 | + @property |
| 22 | + def _log_traceback(self) -> bool: ... |
| 23 | + @_log_traceback.setter |
| 24 | + def _log_traceback(self, val: Literal[False]) -> None: ... |
| 25 | + _asyncio_future_blocking: bool # is a part of duck-typing contract for `Future` |
| 26 | + def __init__(self, *, loop: AbstractEventLoop | None = ...) -> None: ... |
| 27 | + def __del__(self) -> None: ... |
| 28 | + def get_loop(self) -> AbstractEventLoop: ... |
| 29 | + @property |
| 30 | + def _callbacks(self) -> list[tuple[Callable[[Self], Any], Context]]: ... |
| 31 | + def add_done_callback(self, fn: Callable[[Self], object], /, *, context: Context | None = None) -> None: ... |
| 32 | + if sys.version_info >= (3, 9): |
| 33 | + def cancel(self, msg: Any | None = None) -> bool: ... |
| 34 | + else: |
| 35 | + def cancel(self) -> bool: ... |
| 36 | + |
| 37 | + def cancelled(self) -> bool: ... |
| 38 | + def done(self) -> bool: ... |
| 39 | + def result(self) -> _T: ... |
| 40 | + def exception(self) -> BaseException | None: ... |
| 41 | + def remove_done_callback(self, fn: Callable[[Self], object], /) -> int: ... |
| 42 | + def set_result(self, result: _T, /) -> None: ... |
| 43 | + def set_exception(self, exception: type | BaseException, /) -> None: ... |
| 44 | + def __iter__(self) -> Generator[Any, None, _T]: ... |
| 45 | + def __await__(self) -> Generator[Any, None, _T]: ... |
| 46 | + @property |
| 47 | + def _loop(self) -> AbstractEventLoop: ... |
| 48 | + if sys.version_info >= (3, 9): |
| 49 | + def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... |
| 50 | + |
| 51 | +if sys.version_info >= (3, 12): |
| 52 | + _TaskCompatibleCoro: TypeAlias = Coroutine[Any, Any, _T_co] |
| 53 | +elif sys.version_info >= (3, 9): |
| 54 | + _TaskCompatibleCoro: TypeAlias = Generator[_TaskYieldType, None, _T_co] | Coroutine[Any, Any, _T_co] |
| 55 | +else: |
| 56 | + _TaskCompatibleCoro: TypeAlias = Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co] |
| 57 | + |
| 58 | +# mypy and pyright complain that a subclass of an invariant class shouldn't be covariant. |
| 59 | +# While this is true in general, here it's sort-of okay to have a covariant subclass, |
| 60 | +# since the only reason why `asyncio.Future` is invariant is the `set_result()` method, |
| 61 | +# and `asyncio.Task.set_result()` always raises. |
| 62 | +class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportInvalidTypeArguments] |
| 63 | + if sys.version_info >= (3, 12): |
| 64 | + def __init__( |
| 65 | + self, |
| 66 | + coro: _TaskCompatibleCoro[_T_co], |
| 67 | + *, |
| 68 | + loop: AbstractEventLoop = ..., |
| 69 | + name: str | None = ..., |
| 70 | + context: Context | None = None, |
| 71 | + eager_start: bool = False, |
| 72 | + ) -> None: ... |
| 73 | + elif sys.version_info >= (3, 11): |
| 74 | + def __init__( |
| 75 | + self, |
| 76 | + coro: _TaskCompatibleCoro[_T_co], |
| 77 | + *, |
| 78 | + loop: AbstractEventLoop = ..., |
| 79 | + name: str | None = ..., |
| 80 | + context: Context | None = None, |
| 81 | + ) -> None: ... |
| 82 | + else: |
| 83 | + def __init__( |
| 84 | + self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop = ..., name: str | None = ... |
| 85 | + ) -> None: ... |
| 86 | + |
| 87 | + if sys.version_info >= (3, 12): |
| 88 | + def get_coro(self) -> _TaskCompatibleCoro[_T_co] | None: ... |
| 89 | + else: |
| 90 | + def get_coro(self) -> _TaskCompatibleCoro[_T_co]: ... |
| 91 | + |
| 92 | + def get_name(self) -> str: ... |
| 93 | + def set_name(self, value: object, /) -> None: ... |
| 94 | + if sys.version_info >= (3, 12): |
| 95 | + def get_context(self) -> Context: ... |
| 96 | + |
| 97 | + def get_stack(self, *, limit: int | None = None) -> list[FrameType]: ... |
| 98 | + def print_stack(self, *, limit: int | None = None, file: TextIO | None = None) -> None: ... |
| 99 | + if sys.version_info >= (3, 11): |
| 100 | + def cancelling(self) -> int: ... |
| 101 | + def uncancel(self) -> int: ... |
| 102 | + if sys.version_info < (3, 9): |
| 103 | + @classmethod |
| 104 | + def current_task(cls, loop: AbstractEventLoop | None = None) -> Task[Any] | None: ... |
| 105 | + @classmethod |
| 106 | + def all_tasks(cls, loop: AbstractEventLoop | None = None) -> set[Task[Any]]: ... |
| 107 | + if sys.version_info >= (3, 9): |
| 108 | + def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... |
| 109 | + |
| 110 | +def get_event_loop() -> AbstractEventLoop: ... |
| 111 | +def get_running_loop() -> AbstractEventLoop: ... |
| 112 | +def _set_running_loop(loop: AbstractEventLoop | None, /) -> None: ... |
| 113 | +def _get_running_loop() -> AbstractEventLoop: ... |
| 114 | +def _register_task(task: Task[Any]) -> None: ... |
| 115 | +def _unregister_task(task: Task[Any]) -> None: ... |
| 116 | +def _enter_task(loop: AbstractEventLoop, task: Task[Any]) -> None: ... |
| 117 | +def _leave_task(loop: AbstractEventLoop, task: Task[Any]) -> None: ... |
| 118 | + |
| 119 | +if sys.version_info >= (3, 12): |
| 120 | + def current_task(loop: AbstractEventLoop | None = None) -> Task[Any] | None: ... |
0 commit comments