From 021c219e6e7bc9bacd286a3f287c5c3ff4518f4b Mon Sep 17 00:00:00 2001 From: Kaushal Rohit Date: Wed, 7 Oct 2020 06:18:40 +0530 Subject: [PATCH] concurrent.futures.wait: use _Collection (#4618) --- stdlib/3/concurrent/futures/_base.pyi | 14 ++++++++++++-- third_party/2/concurrent/futures/_base.pyi | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/stdlib/3/concurrent/futures/_base.pyi b/stdlib/3/concurrent/futures/_base.pyi index ba17658c8..e84042f12 100644 --- a/stdlib/3/concurrent/futures/_base.pyi +++ b/stdlib/3/concurrent/futures/_base.pyi @@ -1,8 +1,9 @@ import sys import threading +from abc import abstractmethod from logging import Logger from types import TracebackType -from typing import Any, Callable, Generic, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, TypeVar +from typing import Any, Callable, Container, Generic, Iterable, Iterator, List, Optional, Protocol, Set, Tuple, TypeVar FIRST_COMPLETED: str FIRST_EXCEPTION: str @@ -26,6 +27,15 @@ if sys.version_info >= (3, 7): _T = TypeVar("_T") +_T_co = TypeVar("_T_co", covariant=True) + +# Copied over Collection implementation as it does not exist in Python 2 and <3.6. +# Also to solve pytype issues with _Collection. +class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + class Future(Generic[_T]): def __init__(self) -> None: ... def cancel(self) -> bool: ... @@ -65,7 +75,7 @@ class Executor: def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ... def wait( - fs: Sequence[Future[_T]], timeout: Optional[float] = ..., return_when: str = ... + fs: _Collection[Future[_T]], timeout: Optional[float] = ..., return_when: str = ... ) -> Tuple[Set[Future[_T]], Set[Future[_T]]]: ... class _Waiter: diff --git a/third_party/2/concurrent/futures/_base.pyi b/third_party/2/concurrent/futures/_base.pyi index ba17658c8..e84042f12 100644 --- a/third_party/2/concurrent/futures/_base.pyi +++ b/third_party/2/concurrent/futures/_base.pyi @@ -1,8 +1,9 @@ import sys import threading +from abc import abstractmethod from logging import Logger from types import TracebackType -from typing import Any, Callable, Generic, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, TypeVar +from typing import Any, Callable, Container, Generic, Iterable, Iterator, List, Optional, Protocol, Set, Tuple, TypeVar FIRST_COMPLETED: str FIRST_EXCEPTION: str @@ -26,6 +27,15 @@ if sys.version_info >= (3, 7): _T = TypeVar("_T") +_T_co = TypeVar("_T_co", covariant=True) + +# Copied over Collection implementation as it does not exist in Python 2 and <3.6. +# Also to solve pytype issues with _Collection. +class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + class Future(Generic[_T]): def __init__(self) -> None: ... def cancel(self) -> bool: ... @@ -65,7 +75,7 @@ class Executor: def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ... def wait( - fs: Sequence[Future[_T]], timeout: Optional[float] = ..., return_when: str = ... + fs: _Collection[Future[_T]], timeout: Optional[float] = ..., return_when: str = ... ) -> Tuple[Set[Future[_T]], Set[Future[_T]]]: ... class _Waiter: