From d0b673ec33d03e18bcd952a1ddce9469913765f1 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 22 May 2017 23:15:16 -0700 Subject: [PATCH] various fixes to asyncio stubs --- stdlib/3.4/asyncio/__init__.pyi | 16 ++++++++++------ stdlib/3.4/asyncio/coroutines.pyi | 4 ++-- stdlib/3.4/asyncio/events.pyi | 15 +++++++++------ stdlib/3.4/asyncio/futures.pyi | 14 ++++++++------ stdlib/3.4/asyncio/locks.pyi | 4 ++-- stdlib/3.4/asyncio/protocols.pyi | 4 ++-- stdlib/3.4/asyncio/queues.pyi | 4 ++-- stdlib/3.4/asyncio/streams.pyi | 16 ++++++++-------- stdlib/3.4/asyncio/subprocess.pyi | 4 ++-- stdlib/3.4/asyncio/tasks.pyi | 3 ++- stdlib/3.4/asyncio/transports.pyi | 2 +- stdlib/3/concurrent/futures/_base.pyi | 6 +++--- 12 files changed, 51 insertions(+), 41 deletions(-) diff --git a/stdlib/3.4/asyncio/__init__.pyi b/stdlib/3.4/asyncio/__init__.pyi index 60a7ddeae734..ebd0bb289c01 100644 --- a/stdlib/3.4/asyncio/__init__.pyi +++ b/stdlib/3.4/asyncio/__init__.pyi @@ -1,8 +1,5 @@ -"""The asyncio package, tracking PEP 3156.""" - -import socket import sys -from typing import Type +from typing import List, Type from asyncio.coroutines import ( coroutine as coroutine, @@ -49,7 +46,7 @@ from asyncio.tasks import ( ALL_COMPLETED as ALL_COMPLETED, as_completed as as_completed, ensure_future as ensure_future, - ensure_future as async, + async as async, gather as gather, run_coroutine_threadsafe as run_coroutine_threadsafe, shield as shield, @@ -63,6 +60,7 @@ from asyncio.events import ( AbstractEventLoop as AbstractEventLoop, AbstractServer as AbstractServer, Handle as Handle, + TimerHandle as TimerHandle, get_event_loop_policy as get_event_loop_policy, set_event_loop_policy as set_event_loop_policy, get_event_loop as get_event_loop, @@ -88,6 +86,12 @@ from asyncio.locks import ( if sys.version_info < (3, 5): from asyncio.queues import JoinableQueue as JoinableQueue +else: + from asyncio.futures import isfuture as isfuture + from asyncio.events import ( + _set_running_loop as _set_running_loop, + _get_running_loop as _get_running_loop, + ) if sys.platform != 'win32': from asyncio.streams import ( open_unix_connection as open_unix_connection, @@ -104,4 +108,4 @@ DefaultEventLoopPolicy = ... # type: Type[AbstractEventLoopPolicy] # TODO: AbstractChildWatcher (UNIX only) -__all__ = ... # type: str +__all__: List[str] diff --git a/stdlib/3.4/asyncio/coroutines.pyi b/stdlib/3.4/asyncio/coroutines.pyi index 9072dd7f8544..981ccd5a0018 100644 --- a/stdlib/3.4/asyncio/coroutines.pyi +++ b/stdlib/3.4/asyncio/coroutines.pyi @@ -1,6 +1,6 @@ -from typing import Any, Callable, Generator, TypeVar +from typing import Any, Callable, Generator, List, TypeVar -__all__ = ... # type: str +__all__: List[str] _F = TypeVar('_F', bound=Callable[..., Any]) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 804f88206af9..90090d5ce354 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -9,7 +9,7 @@ from asyncio.protocols import BaseProtocol from asyncio.tasks import Task from asyncio.transports import BaseTransport -__all__ = ... # type: str +__all__: List[str] _T = TypeVar('_T') _Context = Dict[str, Any] @@ -18,11 +18,6 @@ _ProtocolFactory = Callable[[], BaseProtocol] _SSLContext = Union[bool, None, ssl.SSLContext] _TransProtPair = Tuple[BaseTransport, BaseProtocol] -PIPE = ... # type: Any # from subprocess.PIPE - -AF_UNSPEC = 0 # from socket -AI_PASSIVE = 0 - class Handle: _cancelled = False _args = ... # type: List[Any] @@ -32,6 +27,11 @@ class Handle: def cancel(self) -> None: ... def _run(self) -> None: ... +class TimerHandle(Handle): + def __init__(self, when: float, callback: Callable[..., Any], args: List[Any], + loop: AbstractEventLoop) -> None: ... + def __hash__(self) -> int: ... + class AbstractServer: def close(self) -> None: ... @coroutine @@ -218,3 +218,6 @@ def new_event_loop() -> AbstractEventLoop: ... def get_child_watcher() -> Any: ... # TODO: unix_events.AbstractChildWatcher def set_child_watcher(watcher: Any) -> None: ... # TODO: unix_events.AbstractChildWatcher + +def _set_running_loop(loop: AbstractEventLoop) -> None: ... +def _get_running_loop() -> AbstractEventLoop: ... diff --git a/stdlib/3.4/asyncio/futures.pyi b/stdlib/3.4/asyncio/futures.pyi index f651c66b164e..00086f45a786 100644 --- a/stdlib/3.4/asyncio/futures.pyi +++ b/stdlib/3.4/asyncio/futures.pyi @@ -1,15 +1,14 @@ +import sys from typing import Any, Union, Callable, TypeVar, List, Generic, Iterable, Generator, Awaitable from .events import AbstractEventLoop -from concurrent.futures._base import ( - Error as Error, -) from concurrent.futures import ( CancelledError as CancelledError, TimeoutError as TimeoutError, - Future as ConcurrentFuture, + Future as _ConcurrentFuture, + Error, ) -__all__ = ... # type: str +__all__: List[str] _T = TypeVar('_T') @@ -23,6 +22,9 @@ class _TracebackLogger: def clear(self) -> None: ... def __del__(self) -> None: ... +if sys.version_info >= (3, 5): + def isfuture(obj: object) -> bool: ... + class Future(Iterable[_T], Awaitable[_T], Generic[_T]): _state = ... # type: str _exception = ... # type: BaseException @@ -46,4 +48,4 @@ class Future(Iterable[_T], Awaitable[_T], Generic[_T]): def __iter__(self) -> Generator[Any, None, _T]: ... def __await__(self) -> Generator[Any, None, _T]: ... -def wrap_future(f: Union[ConcurrentFuture[_T], Future[_T]]) -> Future[_T]: ... +def wrap_future(f: Union[_ConcurrentFuture[_T], Future[_T]]) -> Future[_T]: ... diff --git a/stdlib/3.4/asyncio/locks.pyi b/stdlib/3.4/asyncio/locks.pyi index 559effd980d9..fe53479b88e6 100644 --- a/stdlib/3.4/asyncio/locks.pyi +++ b/stdlib/3.4/asyncio/locks.pyi @@ -1,4 +1,4 @@ -from typing import Any, Callable, Generator, Iterable, Iterator, TypeVar, Union, Optional +from typing import Any, Callable, Generator, Iterable, Iterator, List, TypeVar, Union, Optional from .coroutines import coroutine from .events import AbstractEventLoop @@ -6,7 +6,7 @@ from .futures import Future _T = TypeVar('_T') -__all__ = ... # type: str +__all__: List[str] class _ContextManager: def __init__(self, lock: Union[Lock, Semaphore]) -> None: ... diff --git a/stdlib/3.4/asyncio/protocols.pyi b/stdlib/3.4/asyncio/protocols.pyi index 91b7d94dd806..118659ce2451 100644 --- a/stdlib/3.4/asyncio/protocols.pyi +++ b/stdlib/3.4/asyncio/protocols.pyi @@ -1,7 +1,7 @@ from asyncio import transports -from typing import AnyStr +from typing import AnyStr, List -__all__ = ... # type: str +__all__: List[str] class BaseProtocol: diff --git a/stdlib/3.4/asyncio/queues.pyi b/stdlib/3.4/asyncio/queues.pyi index 847f02b7c9c2..38baf669bc9a 100644 --- a/stdlib/3.4/asyncio/queues.pyi +++ b/stdlib/3.4/asyncio/queues.pyi @@ -2,9 +2,9 @@ import sys from asyncio.events import AbstractEventLoop from .coroutines import coroutine from .futures import Future -from typing import Any, Generator, Generic, TypeVar +from typing import Any, Generator, Generic, List, TypeVar -__all__ = ... # type: str +__all__: List[str] class QueueEmpty(Exception): ... diff --git a/stdlib/3.4/asyncio/streams.pyi b/stdlib/3.4/asyncio/streams.pyi index fd677ab75a8b..4bd027c53fa5 100644 --- a/stdlib/3.4/asyncio/streams.pyi +++ b/stdlib/3.4/asyncio/streams.pyi @@ -1,15 +1,15 @@ import sys -from typing import Any, Awaitable, Callable, Generator, Iterable, Optional, Tuple +from typing import Any, Awaitable, Callable, Generator, Iterable, List, Optional, Tuple from . import coroutines from . import events from . import protocols from . import transports -ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Optional[Awaitable[None]]] +_ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Optional[Awaitable[None]]] -__all__ = ... # type: str +__all__: List[str] class IncompleteReadError(EOFError): def __init__(self, partial: str, expected: int) -> None: ... @@ -29,7 +29,7 @@ def open_connection( @coroutines.coroutine def start_server( - client_connected_cb: ClientConnectedCallback, + client_connected_cb: _ClientConnectedCallback, host: str = ..., port: int = ..., *, @@ -50,7 +50,7 @@ if sys.platform != 'win32': @coroutines.coroutine def start_unix_server( - client_connected_cb: ClientConnectedCallback, + client_connected_cb: _ClientConnectedCallback, path: str = ..., *, loop: int = ..., @@ -62,7 +62,7 @@ class FlowControlMixin(protocols.Protocol): ... class StreamReaderProtocol(FlowControlMixin, protocols.Protocol): def __init__(self, stream_reader: StreamReader, - client_connected_cb: ClientConnectedCallback = ..., + client_connected_cb: _ClientConnectedCallback = ..., loop: events.AbstractEventLoop = ...) -> None: ... def connection_made(self, transport: transports.BaseTransport) -> None: ... def connection_lost(self, exc: Exception) -> None: ... @@ -99,8 +99,8 @@ class StreamReader: @coroutines.coroutine def readline(self) -> Generator[Any, None, bytes]: ... @coroutines.coroutine - def readuntil(self, separator=b'\n') -> Generator[Any, None, bytes]: ... + def readuntil(self, separator: bytes = ...) -> Generator[Any, None, bytes]: ... @coroutines.coroutine - def read(self, n=-1) -> Generator[Any, None, bytes]: ... + def read(self, n: int = ...) -> Generator[Any, None, bytes]: ... @coroutines.coroutine def readexactly(self, n) -> Generator[Any, None, bytes]: ... diff --git a/stdlib/3.4/asyncio/subprocess.pyi b/stdlib/3.4/asyncio/subprocess.pyi index a2592894fbf6..f975f462c461 100644 --- a/stdlib/3.4/asyncio/subprocess.pyi +++ b/stdlib/3.4/asyncio/subprocess.pyi @@ -3,9 +3,9 @@ from asyncio import protocols from asyncio import streams from asyncio import transports from asyncio.coroutines import coroutine -from typing import Any, AnyStr, Generator, Optional, Tuple, Union +from typing import Any, AnyStr, Generator, List, Optional, Tuple, Union -__all__ = ... # type: str +__all__: List[str] PIPE = ... # type: int STDOUT = ... # type: int diff --git a/stdlib/3.4/asyncio/tasks.pyi b/stdlib/3.4/asyncio/tasks.pyi index bf69df8ea57e..d50af2f99180 100644 --- a/stdlib/3.4/asyncio/tasks.pyi +++ b/stdlib/3.4/asyncio/tasks.pyi @@ -5,7 +5,7 @@ import concurrent.futures from .events import AbstractEventLoop from .futures import Future -__all__ = ... # type: str +__all__: List[str] _T = TypeVar('_T') _FutureT = Union[Future[_T], Generator[Any, None, _T], Awaitable[_T]] @@ -18,6 +18,7 @@ def as_completed(fs: Sequence[_FutureT[_T]], *, loop: AbstractEventLoop = ..., timeout: Optional[float] = ...) -> Iterator[Generator[Any, None, _T]]: ... def ensure_future(coro_or_future: _FutureT[_T], *, loop: AbstractEventLoop = ...) -> Future[_T]: ... +async = ensure_future # TODO: gather() should use variadic type vars instead of _TAny. _TAny = Any def gather(*coros_or_futures: _FutureT[_TAny], diff --git a/stdlib/3.4/asyncio/transports.pyi b/stdlib/3.4/asyncio/transports.pyi index 1c9639edcf2e..a8a3d3e1dc48 100644 --- a/stdlib/3.4/asyncio/transports.pyi +++ b/stdlib/3.4/asyncio/transports.pyi @@ -1,6 +1,6 @@ from typing import Dict, Any, TypeVar, Mapping, List -__all__ = ... # type: str +__all__: List[str] class BaseTransport: def __init__(self, extra: Mapping[Any, Any] = ...) -> None: ... diff --git a/stdlib/3/concurrent/futures/_base.pyi b/stdlib/3/concurrent/futures/_base.pyi index 547d68e7adca..510b7c0ff687 100644 --- a/stdlib/3/concurrent/futures/_base.pyi +++ b/stdlib/3/concurrent/futures/_base.pyi @@ -25,7 +25,7 @@ class Future(Generic[_T]): def cancelled(self) -> bool: ... def running(self) -> bool: ... def done(self) -> bool: ... - def add_done_callback(self, fn: Callable[[Future], Any]) -> None: ... + 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) -> None: ... @@ -39,6 +39,6 @@ class Executor: def __enter__(self) -> Executor: ... def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: ... -def as_completed(fs: Iterable[Future], timeout: Optional[float] = ...) -> Iterator[Future]: ... +def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ... -def wait(fs: Iterable[Future], timeout: Optional[float] = ..., return_when: str = ...) -> Tuple[Set[Future], Set[Future]]: ... +def wait(fs: Iterable[Future[_T]], timeout: Optional[float] = ..., return_when: str = ...) -> Tuple[Set[Future[_T]], Set[Future[_T]]]: ...