3 from _typeshed import Self
4 from abc import abstractmethod
5 from collections.abc import Container, Iterable, Iterator, Sequence
6 from logging import Logger
7 from typing import Any, Callable, Generic, Protocol, Set, TypeVar, overload
9 if sys.version_info >= (3, 9):
10 from types import GenericAlias
18 CANCELLED_AND_NOTIFIED: str
20 _FUTURE_STATES: list[str]
21 _STATE_TO_DESCRIPTION_MAP: dict[str, str]
24 class Error(Exception): ...
25 class CancelledError(Error): ...
26 class TimeoutError(Error): ...
28 if sys.version_info >= (3, 8):
29 class InvalidStateError(Error): ...
31 if sys.version_info >= (3, 7):
32 class BrokenExecutor(RuntimeError): ...
36 _T_co = TypeVar("_T_co", covariant=True)
38 # Copied over Collection implementation as it does not exist in Python 2 and <3.6.
39 # Also to solve pytype issues with _Collection.
40 class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
41 # Implement Sized (but don't have it as a base class).
43 def __len__(self) -> int: ...
45 class Future(Generic[_T]):
46 def __init__(self) -> None: ...
47 def cancel(self) -> bool: ...
48 def cancelled(self) -> bool: ...
49 def running(self) -> bool: ...
50 def done(self) -> bool: ...
51 def add_done_callback(self, fn: Callable[[Future[_T]], Any]) -> None: ...
52 def result(self, timeout: float | None = ...) -> _T: ...
53 def set_running_or_notify_cancel(self) -> bool: ...
54 def set_result(self, result: _T) -> None: ...
55 def exception(self, timeout: float | None = ...) -> BaseException | None: ...
56 def set_exception(self, exception: BaseException | None) -> None: ...
57 if sys.version_info >= (3, 9):
58 def __class_getitem__(cls, item: Any) -> GenericAlias: ...
61 if sys.version_info >= (3, 9):
62 def submit(self, __fn: Callable[..., _T], *args: Any, **kwargs: Any) -> Future[_T]: ...
64 def submit(self, fn: Callable[..., _T], *args: Any, **kwargs: Any) -> Future[_T]: ...
66 self, fn: Callable[..., _T], *iterables: Iterable[Any], timeout: float | None = ..., chunksize: int = ...
67 ) -> Iterator[_T]: ...
68 if sys.version_info >= (3, 9):
69 def shutdown(self, wait: bool = ..., *, cancel_futures: bool = ...) -> None: ...
71 def shutdown(self, wait: bool = ...) -> None: ...
72 def __enter__(self: Self) -> Self: ...
73 def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool | None: ...
75 def as_completed(fs: Iterable[Future[_T]], timeout: float | None = ...) -> Iterator[Future[_T]]: ...
77 # Ideally this would be a namedtuple, but mypy doesn't support generic tuple types. See #1976
78 class DoneAndNotDoneFutures(Sequence[Set[Future[_T]]]):
80 not_done: set[Future[_T]]
81 def __new__(_cls, done: set[Future[_T]], not_done: set[Future[_T]]) -> DoneAndNotDoneFutures[_T]: ...
82 def __len__(self) -> int: ...
84 def __getitem__(self, i: int) -> set[Future[_T]]: ...
86 def __getitem__(self, s: slice) -> DoneAndNotDoneFutures[_T]: ...
88 def wait(fs: Iterable[Future[_T]], timeout: float | None = ..., return_when: str = ...) -> DoneAndNotDoneFutures[_T]: ...
91 event: threading.Event
92 finished_futures: list[Future[Any]]
93 def __init__(self) -> None: ...
94 def add_result(self, future: Future[Any]) -> None: ...
95 def add_exception(self, future: Future[Any]) -> None: ...
96 def add_cancelled(self, future: Future[Any]) -> None: ...
98 class _AsCompletedWaiter(_Waiter):
100 def __init__(self) -> None: ...
101 def add_result(self, future: Future[Any]) -> None: ...
102 def add_exception(self, future: Future[Any]) -> None: ...
103 def add_cancelled(self, future: Future[Any]) -> None: ...
105 class _FirstCompletedWaiter(_Waiter):
106 def add_result(self, future: Future[Any]) -> None: ...
107 def add_exception(self, future: Future[Any]) -> None: ...
108 def add_cancelled(self, future: Future[Any]) -> None: ...
110 class _AllCompletedWaiter(_Waiter):
111 num_pending_calls: int
112 stop_on_exception: bool
114 def __init__(self, num_pending_calls: int, stop_on_exception: bool) -> None: ...
115 def add_result(self, future: Future[Any]) -> None: ...
116 def add_exception(self, future: Future[Any]) -> None: ...
117 def add_cancelled(self, future: Future[Any]) -> None: ...
119 class _AcquireFutures:
120 futures: Iterable[Future[Any]]
121 def __init__(self, futures: Iterable[Future[Any]]) -> None: ...
122 def __enter__(self) -> None: ...
123 def __exit__(self, *args: Any) -> None: ...