From 505446574695b1612b3be9e6a0e0ac811ee53d18 Mon Sep 17 00:00:00 2001 From: Daniel Li Date: Fri, 15 Jun 2018 13:03:19 -0400 Subject: [PATCH] Make concurrent.futures stubs identical (#2237) Make the Python 2 and 3 concurrent.futures stubs identical so fixes get applied to both. For example, #1305 and #2233 fixed the same problem at different times, as did #1078 and #1911. By making the stubs identical, we apply the fix from #1711 to Python 2. Fixes #2234. --- stdlib/3/concurrent/futures/_base.pyi | 19 +++++-- stdlib/3/concurrent/futures/process.pyi | 4 +- tests/check_consistent.py | 5 ++ third_party/2/concurrent/futures/__init__.pyi | 49 ++--------------- third_party/2/concurrent/futures/_base.pyi | 54 +++++++++++++++++++ third_party/2/concurrent/futures/process.pyi | 11 ++++ third_party/2/concurrent/futures/thread.pyi | 10 ++++ 7 files changed, 102 insertions(+), 50 deletions(-) create mode 100644 third_party/2/concurrent/futures/_base.pyi create mode 100644 third_party/2/concurrent/futures/process.pyi create mode 100644 third_party/2/concurrent/futures/thread.pyi diff --git a/stdlib/3/concurrent/futures/_base.pyi b/stdlib/3/concurrent/futures/_base.pyi index 352913b32..ff38f8689 100644 --- a/stdlib/3/concurrent/futures/_base.pyi +++ b/stdlib/3/concurrent/futures/_base.pyi @@ -1,4 +1,6 @@ from typing import TypeVar, Generic, Any, Iterable, Iterator, Callable, Tuple, Optional, Set, NamedTuple +from types import TracebackType +import sys FIRST_COMPLETED = ... # type: str FIRST_EXCEPTION = ... # type: str @@ -24,14 +26,25 @@ class Future(Generic[_T]): def done(self) -> bool: ... def add_done_callback(self, fn: Callable[[Future[_T]], Any]) -> None: ... def result(self, timeout: Optional[float] = ...) -> _T: ... - def exception(self, timeout: Optional[float] = ...) -> Optional[BaseException]: ... def set_running_or_notify_cancel(self) -> bool: ... def set_result(self, result: _T) -> None: ... - def set_exception(self, exception: Optional[BaseException]) -> None: ... + + if sys.version_info >= (3,): + def exception(self, timeout: Optional[float] = ...) -> Optional[BaseException]: ... + def set_exception(self, exception: Optional[BaseException]) -> None: ... + else: + def exception(self, timeout: Optional[float] = ...) -> Any: ... + def exception_info(self, timeout: Optional[float] = ...) -> Tuple[Any, Optional[TracebackType]]: ... + def set_exception(self, exception: Any) -> None: ... + def set_exception_info(self, exception: Any, traceback: Optional[TracebackType]) -> None: ... + class Executor: def submit(self, fn: Callable[..., _T], *args: Any, **kwargs: Any) -> Future[_T]: ... - def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ..., chunksize: int = ...) -> Iterator[_T]: ... + if sys.version_info >= (3, 5): + def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ..., chunksize: int = ...) -> Iterator[_T]: ... + else: + def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ...,) -> Iterator[_T]: ... def shutdown(self, wait: bool = ...) -> None: ... def __enter__(self: _T) -> _T: ... def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: ... diff --git a/stdlib/3/concurrent/futures/process.pyi b/stdlib/3/concurrent/futures/process.pyi index 8076c2d05..c36180ff8 100644 --- a/stdlib/3/concurrent/futures/process.pyi +++ b/stdlib/3/concurrent/futures/process.pyi @@ -1,9 +1,11 @@ from typing import Optional, Any from ._base import Future, Executor +import sys EXTRA_QUEUED_CALLS = ... # type: Any -class BrokenProcessPool(RuntimeError): ... +if sys.version_info >= (3,): + class BrokenProcessPool(RuntimeError): ... class ProcessPoolExecutor(Executor): def __init__(self, max_workers: Optional[int] = ...) -> None: ... diff --git a/tests/check_consistent.py b/tests/check_consistent.py index 673c1c044..fcb05f75b 100755 --- a/tests/check_consistent.py +++ b/tests/check_consistent.py @@ -14,6 +14,11 @@ consistent_files = [ {'stdlib/3.4/enum.pyi', 'third_party/3/enum.pyi'}, {'stdlib/2/os/path.pyi', 'stdlib/3/os/path.pyi'}, {'stdlib/3/unittest/mock.pyi', 'third_party/2and3/mock.pyi'}, + {'stdlib/3/concurrent/__init__.pyi', 'third_party/2/concurrent/__init__.pyi'}, + {'stdlib/3/concurrent/futures/__init__.pyi', 'third_party/2/concurrent/futures/__init__.pyi'}, + {'stdlib/3/concurrent/futures/_base.pyi', 'third_party/2/concurrent/futures/_base.pyi'}, + {'stdlib/3/concurrent/futures/thread.pyi', 'third_party/2/concurrent/futures/thread.pyi'}, + {'stdlib/3/concurrent/futures/process.pyi', 'third_party/2/concurrent/futures/process.pyi'}, ] def main(): diff --git a/third_party/2/concurrent/futures/__init__.pyi b/third_party/2/concurrent/futures/__init__.pyi index dfae50b23..4439dcadb 100644 --- a/third_party/2/concurrent/futures/__init__.pyi +++ b/third_party/2/concurrent/futures/__init__.pyi @@ -1,46 +1,3 @@ -# Stubs for concurrent.futures (Python 2) - -from typing import TypeVar, Generic, Any, Iterable, Iterator, Callable, Optional, Set, Tuple, Union -from types import TracebackType - -_T = TypeVar('_T') - -class Error(Exception): ... -class CancelledError(Error): ... -class TimeoutError(Error): ... - -class Future(Generic[_T]): - def cancel(self) -> bool: ... - def cancelled(self) -> bool: ... - def running(self) -> bool: ... - def done(self) -> bool: ... - def result(self, timeout: Optional[float] = ...) -> _T: ... - def exception(self, timeout: Optional[float] = ...) -> Any: ... - def exception_info(self, timeout: Optional[float] = ...) -> Tuple[Any, Optional[TracebackType]]: ... - def add_done_callback(self, fn: Callable[[Future[_T]], Any]) -> None: ... - - def set_running_or_notify_cancel(self) -> bool: ... - def set_result(self, result: _T) -> None: ... - def set_exception(self, exception: Any) -> None: ... - def set_exception_info(self, exception: Any, traceback: Optional[TracebackType]) -> None: ... - -class Executor: - def submit(self, fn: Callable[..., _T], *args: Any, **kwargs: Any) -> Future[_T]: ... - def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ...) -> Iterator[_T]: ... - def shutdown(self, wait: bool = ...) -> None: ... - def __enter__(self) -> Executor: ... - def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: ... - -class ThreadPoolExecutor(Executor): - def __init__(self, max_workers: Optional[int] = ...) -> None: ... - -class ProcessPoolExecutor(Executor): - def __init__(self, max_workers: Optional[int] = ...) -> None: ... - -def wait(fs: Iterable[Future[_T]], timeout: Optional[float] = ..., return_when: str = ...) -> Tuple[Set[Future[_T]], Set[Future[_T]]]: ... - -FIRST_COMPLETED = ... # type: str -FIRST_EXCEPTION = ... # type: str -ALL_COMPLETED = ... # type: str - -def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ... +from ._base import * # noqa: F403 +from .thread import * # noqa: F403 +from .process import * # noqa: F403 diff --git a/third_party/2/concurrent/futures/_base.pyi b/third_party/2/concurrent/futures/_base.pyi new file mode 100644 index 000000000..ff38f8689 --- /dev/null +++ b/third_party/2/concurrent/futures/_base.pyi @@ -0,0 +1,54 @@ +from typing import TypeVar, Generic, Any, Iterable, Iterator, Callable, Tuple, Optional, Set, NamedTuple +from types import TracebackType +import sys + +FIRST_COMPLETED = ... # type: str +FIRST_EXCEPTION = ... # type: str +ALL_COMPLETED = ... # type: str +PENDING = ... # type: Any +RUNNING = ... # type: Any +CANCELLED = ... # type: Any +CANCELLED_AND_NOTIFIED = ... # type: Any +FINISHED = ... # type: Any +LOGGER = ... # type: Any + +class Error(Exception): ... +class CancelledError(Error): ... +class TimeoutError(Error): ... + +_T = TypeVar('_T') + +class Future(Generic[_T]): + def __init__(self) -> None: ... + def cancel(self) -> bool: ... + def cancelled(self) -> bool: ... + def running(self) -> bool: ... + def done(self) -> bool: ... + def add_done_callback(self, fn: Callable[[Future[_T]], Any]) -> None: ... + def result(self, timeout: Optional[float] = ...) -> _T: ... + def set_running_or_notify_cancel(self) -> bool: ... + def set_result(self, result: _T) -> None: ... + + if sys.version_info >= (3,): + def exception(self, timeout: Optional[float] = ...) -> Optional[BaseException]: ... + def set_exception(self, exception: Optional[BaseException]) -> None: ... + else: + def exception(self, timeout: Optional[float] = ...) -> Any: ... + def exception_info(self, timeout: Optional[float] = ...) -> Tuple[Any, Optional[TracebackType]]: ... + def set_exception(self, exception: Any) -> None: ... + def set_exception_info(self, exception: Any, traceback: Optional[TracebackType]) -> None: ... + + +class Executor: + def submit(self, fn: Callable[..., _T], *args: Any, **kwargs: Any) -> Future[_T]: ... + if sys.version_info >= (3, 5): + def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ..., chunksize: int = ...) -> Iterator[_T]: ... + else: + def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ...,) -> Iterator[_T]: ... + def shutdown(self, wait: bool = ...) -> None: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: ... + +def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ... + +def wait(fs: Iterable[Future[_T]], timeout: Optional[float] = ..., return_when: str = ...) -> Tuple[Set[Future[_T]], Set[Future[_T]]]: ... diff --git a/third_party/2/concurrent/futures/process.pyi b/third_party/2/concurrent/futures/process.pyi new file mode 100644 index 000000000..c36180ff8 --- /dev/null +++ b/third_party/2/concurrent/futures/process.pyi @@ -0,0 +1,11 @@ +from typing import Optional, Any +from ._base import Future, Executor +import sys + +EXTRA_QUEUED_CALLS = ... # type: Any + +if sys.version_info >= (3,): + class BrokenProcessPool(RuntimeError): ... + +class ProcessPoolExecutor(Executor): + def __init__(self, max_workers: Optional[int] = ...) -> None: ... diff --git a/third_party/2/concurrent/futures/thread.pyi b/third_party/2/concurrent/futures/thread.pyi new file mode 100644 index 000000000..88a45a3e0 --- /dev/null +++ b/third_party/2/concurrent/futures/thread.pyi @@ -0,0 +1,10 @@ +from typing import Optional +from ._base import Executor, Future +import sys + +class ThreadPoolExecutor(Executor): + if sys.version_info >= (3, 6): + def __init__(self, max_workers: Optional[int] = ..., + thread_name_prefix: str = ...) -> None: ... + else: + def __init__(self, max_workers: Optional[int] = ...) -> None: ...