From de4e87f574e40bcfea15e0254d1640f63a737c6e Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 3 Aug 2016 17:01:35 -0700 Subject: [PATCH] Changes required by mypy async-await support (#435) --- stdlib/3.4/asyncio/events.pyi | 11 +++++++++-- stdlib/3.4/asyncio/futures.pyi | 5 +++-- stdlib/3.4/asyncio/streams.pyi | 6 +++--- stdlib/3.4/asyncio/tasks.pyi | 10 ++++++++-- stdlib/3/typing.pyi | 10 +++++++--- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 43b527b38..262758fbd 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -1,4 +1,4 @@ -from typing import Any, Awaitable, TypeVar, List, Callable, Tuple, Union, Dict, Generator +from typing import Any, Awaitable, TypeVar, List, Callable, Tuple, Union, Dict, Generator, overload from abc import ABCMeta, abstractmethod from asyncio.futures import Future from asyncio.coroutines import coroutine @@ -30,8 +30,15 @@ class AbstractServer: class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod def run_forever(self) -> None: ... + + # Can't use a union, see mypy issue #1873. + @overload @abstractmethod - def run_until_complete(self, future: Union[Awaitable[_T], Future[_T], Generator[Any, Any, _T]]) -> _T: ... + def run_until_complete(self, future: Generator[Any, Any, _T]) -> _T: ... + @overload + @abstractmethod + def run_until_complete(self, future: Awaitable[_T]) -> _T: ... + @abstractmethod def stop(self) -> None: ... @abstractmethod diff --git a/stdlib/3.4/asyncio/futures.pyi b/stdlib/3.4/asyncio/futures.pyi index a3de8a8fd..eab1d4cfb 100644 --- a/stdlib/3.4/asyncio/futures.pyi +++ b/stdlib/3.4/asyncio/futures.pyi @@ -1,4 +1,4 @@ -from typing import Any, Union, Callable, TypeVar, List, Generic, Iterable, Generator +from typing import Any, Union, Callable, TypeVar, List, Generic, Iterable, Generator, Awaitable from .events import AbstractEventLoop __all__ = ... # type: str @@ -14,7 +14,7 @@ class _TracebackLogger: def clear(self) -> None: ... def __del__(self) -> None: ... -class Future(Iterable[_T], Generic[_T]): +class Future(Iterable[_T], Awaitable[_T], Generic[_T]): _state = ... # type: str _exception = ... # type: BaseException _blocking = False @@ -35,3 +35,4 @@ class Future(Iterable[_T], Generic[_T]): def set_exception(self, exception: Union[type, BaseException]) -> None: ... def _copy_state(self, other: Any) -> None: ... def __iter__(self) -> Generator[Any, None, _T]: ... + def __await__(self) -> Generator[Any, None, _T]: ... diff --git a/stdlib/3.4/asyncio/streams.pyi b/stdlib/3.4/asyncio/streams.pyi index c9086b53a..00fe38e50 100644 --- a/stdlib/3.4/asyncio/streams.pyi +++ b/stdlib/3.4/asyncio/streams.pyi @@ -33,7 +33,7 @@ def start_server( *, loop: events.AbstractEventLoop = ..., limit: int = ..., - **kwds: Any) -> events.AbstractServer: ... + **kwds: Any) -> Generator[Any, None, events.AbstractServer]: ... if hasattr(socket, 'AF_UNIX'): @coroutines.coroutine @@ -42,7 +42,7 @@ if hasattr(socket, 'AF_UNIX'): *, loop: events.AbstractEventLoop = ..., limit: int = ..., - **kwds: Any): ... + **kwds: Any)-> Generator[Any, None, Tuple[StreamReader, StreamWriter]]: ... @coroutines.coroutine def start_unix_server( @@ -51,7 +51,7 @@ if hasattr(socket, 'AF_UNIX'): *, loop: int = ..., limit: int = ..., - **kwds: Any) -> events.AbstractServer: ... + **kwds: Any) -> Generator[Any, None, events.AbstractServer]: ... class FlowControlMixin(protocols.Protocol): ... diff --git a/stdlib/3.4/asyncio/tasks.pyi b/stdlib/3.4/asyncio/tasks.pyi index 57f1eac9f..69a4ac397 100644 --- a/stdlib/3.4/asyncio/tasks.pyi +++ b/stdlib/3.4/asyncio/tasks.pyi @@ -1,4 +1,4 @@ -from typing import Any, TypeVar, Set, Dict, List, TextIO, Union, Tuple, Generic, Callable, Generator, Iterable, Awaitable +from typing import Any, TypeVar, Set, Dict, List, TextIO, Union, Tuple, Generic, Callable, Generator, Iterable, Awaitable, overload __all__ = ... # type: str @@ -22,7 +22,13 @@ class Task(Future[_T], Generic[_T]): def current_task(cls, loop: AbstractEventLoop = ...) -> Task: ... @classmethod def all_tasks(cls, loop: AbstractEventLoop = ...) -> Set[Task]: ... - def __init__(self, coro: Union[Awaitable[_T], Iterable[_T], Future[_T], Generator[Any, None, _T]], *, loop: AbstractEventLoop = ...) -> None: ... + + # Can't use a union, see mypy issue #1873. + @overload + def __init__(self, coro: Generator[Any, None, _T], *, loop: AbstractEventLoop = ...) -> None: ... + @overload + def __init__(self, coro: Awaitable[_T], *, loop: AbstractEventLoop = ...) -> None: ... + def __repr__(self) -> str: ... def get_stack(self, *, limit: int = ...) -> List[Any]: ... # return List[stackframe] def print_stack(self, *, limit: int = ..., file: TextIO = ...) -> None: ... diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 69013f701..84dffe248 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -113,11 +113,15 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]): @abstractmethod def __iter__(self) -> 'Generator[_T_co, _T_contra, _V_co]': ... -class AbstractFuture(Generic[_T]): ... - class Awaitable(Generic[_T_co]): @abstractmethod - def __await__(self) -> Generator[AbstractFuture[_T_co], Any, _T_co]:... + def __await__(self) -> Generator[Any, None, _T_co]:... + +# NOTE: This type does not exist in typing.py or PEP 484. +# The parameters corrrespond to Generator, but the 4th is the original type. +class AwaitableGenerator(Generator[_T_co, _T_contra, _V_co], Awaitable[_T_co], + Generic[_T_co, _T_contra, _V_co, _S]): + pass class AsyncIterable(Generic[_T_co]): @abstractmethod