From bab95f6365afeefe2f101dca54f82fcc592d07b6 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 26 Feb 2017 19:41:10 -0800 Subject: [PATCH 01/10] Add missing methods and @coroutine decorators to AbstractEventLoop Closes #953. I reviewed the documentation at https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop and added missing methods and missing @coroutine decorators. I ran mypy on the sample file from the issue report to confirm that mypy handles the combination of @abstractmethod and @coroutine correctly. There are a number of types in this file that are annotated as Any but could be given more precise types (e.g., sockets and protocol factories). I may submit another PR to fix those separately. --- stdlib/3.4/asyncio/events.pyi | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index ade397d0ba89..246e384bbdb8 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -3,12 +3,15 @@ from typing import Any, Awaitable, Callable, Dict, Generator, List, Optional, Tu from abc import ABCMeta, abstractmethod from asyncio.futures import Future from asyncio.coroutines import coroutine +from asyncio.protocols import BaseProtocol from asyncio.tasks import Task +from asyncio.transports import BaseTransport __all__ = ... # type: str _T = TypeVar('_T') _Context = Dict[str, Any] +_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any] PIPE = ... # type: Any # from subprocess.PIPE @@ -46,7 +49,13 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod def is_running(self) -> bool: ... @abstractmethod + def is_closed(self) -> bool: ... + @abstractmethod def close(self) -> None: ... + if sys.version_info >= (3, 6): + @abstractmethod + @coroutine + def shutdown_asyncgens(self) -> None: ... # Methods scheduling callbacks. All these return Handles. @abstractmethod def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ... @@ -71,39 +80,52 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ... @abstractmethod + @coroutine def run_in_executor(self, executor: Any, callback: Callable[..., Any], *args: Any) -> Future[Any]: ... @abstractmethod def set_default_executor(self, executor: Any) -> None: ... # Network I/O methods returning Futures. @abstractmethod + @coroutine def getaddrinfo(self, host: str, port: int, *, family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Future[List[Tuple[int, int, int, str, tuple]]]: ... @abstractmethod + @coroutine def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Future[Tuple[str, int]]: ... @abstractmethod + @coroutine def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *, ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., local_addr: str = ..., server_hostname: str = ...) -> tuple: ... @abstractmethod + @coroutine def create_server(self, protocol_factory: Any, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Any: ... @abstractmethod + @coroutine def create_unix_connection(self, protocol_factory: Any, path: str, *, ssl: Any = ..., sock: Any = ..., server_hostname: str = ...) -> tuple: ... @abstractmethod + @coroutine def create_unix_server(self, protocol_factory: Any, path: str, *, sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Any: ... @abstractmethod + @coroutine def create_datagram_endpoint(self, protocol_factory: Any, local_addr: str = ..., remote_addr: str = ..., *, family: int = ..., proto: int = ..., flags: int = ...) -> tuple: ... + @abstractmethod + @coroutine + def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Tuple[BaseTransport, BaseProtocol]: ... # Pipes and subprocesses. @abstractmethod + @coroutine def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ... @abstractmethod + @coroutine def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ... @abstractmethod def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = ..., @@ -123,12 +145,16 @@ class AbstractEventLoop(metaclass=ABCMeta): def remove_writer(self, fd: int) -> None: ... # Completion based I/O methods returning Futures. @abstractmethod + @coroutine def sock_recv(self, sock: Any, nbytes: int) -> Any: ... # TODO @abstractmethod + @coroutine def sock_sendall(self, sock: Any, data: bytes) -> None: ... # TODO @abstractmethod + @coroutine def sock_connect(self, sock: Any, address: str) -> Any: ... # TODO @abstractmethod + @coroutine def sock_accept(self, sock: Any) -> Any: ... # Signal handling. @abstractmethod @@ -137,7 +163,9 @@ class AbstractEventLoop(metaclass=ABCMeta): def remove_signal_handler(self, sig: int) -> None: ... # Error handlers. @abstractmethod - def set_exception_handler(self, handler: Callable[[AbstractEventLoop, _Context], Any]) -> None: ... + def set_exception_handler(self, handler: _ExceptionHandler) -> None: ... + @abstractmethod + def get_exception_handler(self) -> _ExceptionHandler: ... @abstractmethod def default_exception_handler(self, context: _Context) -> None: ... @abstractmethod From d0f587e702039806777c76e2047c01855cf01218 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:00:45 -0800 Subject: [PATCH 02/10] make sure all @coroutine functions return a Generator --- stdlib/3.4/asyncio/events.pyi | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 246e384bbdb8..e933ee5077a1 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -55,7 +55,7 @@ class AbstractEventLoop(metaclass=ABCMeta): if sys.version_info >= (3, 6): @abstractmethod @coroutine - def shutdown_asyncgens(self) -> None: ... + def shutdown_asyncgens(self) -> Generator[Any, Any, None]: ... # Methods scheduling callbacks. All these return Handles. @abstractmethod def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ... @@ -82,59 +82,61 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def run_in_executor(self, executor: Any, - callback: Callable[..., Any], *args: Any) -> Future[Any]: ... + callback: Callable[..., Any], *args: Any) -> Generator[None, None, Any]: ... @abstractmethod def set_default_executor(self, executor: Any) -> None: ... # Network I/O methods returning Futures. @abstractmethod @coroutine def getaddrinfo(self, host: str, port: int, *, - family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Future[List[Tuple[int, int, int, str, tuple]]]: ... + family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, List[Tuple[int, int, int, str, tuple]]]: ... @abstractmethod @coroutine - def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Future[Tuple[str, int]]: ... + def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[None, None, Tuple[str, int]]: ... @abstractmethod @coroutine def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *, ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., - local_addr: str = ..., server_hostname: str = ...) -> tuple: ... + local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, tuple]: ... @abstractmethod @coroutine def create_server(self, protocol_factory: Any, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Any: ... + sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: Any, path: str, *, ssl: Any = ..., sock: Any = ..., - server_hostname: str = ...) -> tuple: ... + server_hostname: str = ...) -> Generator[None, None, tuple]: ... @abstractmethod @coroutine def create_unix_server(self, protocol_factory: Any, path: str, *, - sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Any: ... + sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine def create_datagram_endpoint(self, protocol_factory: Any, local_addr: str = ..., remote_addr: str = ..., *, - family: int = ..., proto: int = ..., flags: int = ...) -> tuple: ... + family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, tuple]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Tuple[BaseTransport, BaseProtocol]: ... + def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Generator[None, None, Tuple[BaseTransport, BaseProtocol]]: ... # Pipes and subprocesses. @abstractmethod @coroutine - def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ... + def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, tuple]: ... @abstractmethod @coroutine - def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ... + def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, tuple]: ... @abstractmethod + @coroutine def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> tuple: ... + **kwargs: Any) -> Generator[None, None, tuple]: ... @abstractmethod + @coroutine def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> tuple: ... + **kwargs: Any) -> Generator[None, None, tuple]: ... @abstractmethod def add_reader(self, fd: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... @abstractmethod @@ -146,16 +148,16 @@ class AbstractEventLoop(metaclass=ABCMeta): # Completion based I/O methods returning Futures. @abstractmethod @coroutine - def sock_recv(self, sock: Any, nbytes: int) -> Any: ... # TODO + def sock_recv(self, sock: Any, nbytes: int) -> Generator[None, None, Any]: ... # TODO @abstractmethod @coroutine - def sock_sendall(self, sock: Any, data: bytes) -> None: ... # TODO + def sock_sendall(self, sock: Any, data: bytes) -> Generator[None, None, None]: ... # TODO @abstractmethod @coroutine - def sock_connect(self, sock: Any, address: str) -> Any: ... # TODO + def sock_connect(self, sock: Any, address: str) -> Generator[None, None, Any]: ... # TODO @abstractmethod @coroutine - def sock_accept(self, sock: Any) -> Any: ... + def sock_accept(self, sock: Any) -> Generator[None, None, Any]: ... # Signal handling. @abstractmethod def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... From b5ef325ab35f925903f8b82ccfff632acb88c908 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:05:25 -0800 Subject: [PATCH 03/10] get rid of some Tuple[Any] types, add alias for transport protocol pair --- stdlib/3.4/asyncio/events.pyi | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index e933ee5077a1..00f86ebb7fc7 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -12,6 +12,7 @@ __all__ = ... # type: str _T = TypeVar('_T') _Context = Dict[str, Any] _ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any] +_TransProtPair = Tuple[BaseTransport, BaseProtocol] PIPE = ... # type: Any # from subprocess.PIPE @@ -89,7 +90,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def getaddrinfo(self, host: str, port: int, *, - family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, List[Tuple[int, int, int, str, tuple]]]: ... + family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, List[Tuple[int, int, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]]: ... @abstractmethod @coroutine def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[None, None, Tuple[str, int]]: ... @@ -97,7 +98,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @coroutine def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *, ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., - local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, tuple]: ... + local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def create_server(self, protocol_factory: Any, host: str = ..., port: int = ..., *, @@ -107,7 +108,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @coroutine def create_unix_connection(self, protocol_factory: Any, path: str, *, ssl: Any = ..., sock: Any = ..., - server_hostname: str = ...) -> Generator[None, None, tuple]: ... + server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def create_unix_server(self, protocol_factory: Any, path: str, *, @@ -116,27 +117,27 @@ class AbstractEventLoop(metaclass=ABCMeta): @coroutine def create_datagram_endpoint(self, protocol_factory: Any, local_addr: str = ..., remote_addr: str = ..., *, - family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, tuple]: ... + family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Generator[None, None, Tuple[BaseTransport, BaseProtocol]]: ... + def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Generator[None, None, _TransProtPair]: ... # Pipes and subprocesses. @abstractmethod @coroutine - def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, tuple]: ... + def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, tuple]: ... + def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> Generator[None, None, tuple]: ... + **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> Generator[None, None, tuple]: ... + **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod def add_reader(self, fd: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... @abstractmethod From 8607c1bfb4054cea6f75709227cb312bfee1279d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:07:34 -0800 Subject: [PATCH 04/10] add type for protocol factories --- stdlib/3.4/asyncio/events.pyi | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 00f86ebb7fc7..db8bcdbbf3cf 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -12,6 +12,7 @@ __all__ = ... # type: str _T = TypeVar('_T') _Context = Dict[str, Any] _ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any] +_ProtocolFactory = Callable[[], BaseProtocol] _TransProtPair = Tuple[BaseTransport, BaseProtocol] PIPE = ... # type: Any # from subprocess.PIPE @@ -96,46 +97,46 @@ class AbstractEventLoop(metaclass=ABCMeta): def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[None, None, Tuple[str, int]]: ... @abstractmethod @coroutine - def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *, + def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def create_server(self, protocol_factory: Any, host: str = ..., port: int = ..., *, + def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine - def create_unix_connection(self, protocol_factory: Any, path: str, *, + def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *, ssl: Any = ..., sock: Any = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def create_unix_server(self, protocol_factory: Any, path: str, *, + def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *, sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine - def create_datagram_endpoint(self, protocol_factory: Any, + def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory, local_addr: str = ..., remote_addr: str = ..., *, family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: Any, sock: Any, *, ssl: Any = ...) -> Generator[None, None, _TransProtPair]: ... + def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: Any = ...) -> Generator[None, None, _TransProtPair]: ... # Pipes and subprocesses. @abstractmethod @coroutine - def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, _TransProtPair]: ... + def connect_read_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> Generator[None, None, _TransProtPair]: ... + def connect_write_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = ..., + def subprocess_shell(self, protocol_factory: _ProtocolFactory, cmd: Union[bytes, str], *, stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any = ..., + def subprocess_exec(self, protocol_factory: _ProtocolFactory, *args: List[Any], stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... @abstractmethod From 7733f6b2ec1390a44119c4c6ce8b377da380244e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:14:21 -0800 Subject: [PATCH 05/10] add type for SSLContext --- stdlib/3.4/asyncio/events.pyi | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index db8bcdbbf3cf..34bec3fab405 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -1,3 +1,4 @@ +import ssl import sys from typing import Any, Awaitable, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union, overload from abc import ABCMeta, abstractmethod @@ -13,6 +14,7 @@ _T = TypeVar('_T') _Context = Dict[str, Any] _ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any] _ProtocolFactory = Callable[[], BaseProtocol] +_SSLContext = Union[bool, None, ssl.SSLContext] _TransProtPair = Tuple[BaseTransport, BaseProtocol] PIPE = ... # type: Any # from subprocess.PIPE @@ -98,22 +100,22 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, - ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., + ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Generator[None, None, Any]: ... + sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *, - ssl: Any = ..., sock: Any = ..., + ssl: _SSLContext = ..., sock: Any = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *, - sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Generator[None, None, Any]: ... + sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[None, None, Any]: ... @abstractmethod @coroutine def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory, @@ -121,7 +123,7 @@ class AbstractEventLoop(metaclass=ABCMeta): family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: Any = ...) -> Generator[None, None, _TransProtPair]: ... + def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: _SSLContext = ...) -> Generator[None, None, _TransProtPair]: ... # Pipes and subprocesses. @abstractmethod @coroutine From 8ee657bb59e302cfbfb74e7626fb7f00593d0e0d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:16:00 -0800 Subject: [PATCH 06/10] standardize arguments to Generator for @coroutine functions --- stdlib/3.4/asyncio/events.pyi | 40 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 34bec3fab405..c3259162d9d0 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -34,7 +34,7 @@ class Handle: class AbstractServer: def close(self) -> None: ... @coroutine - def wait_closed(self) -> Generator[Any, Any, None]: ... + def wait_closed(self) -> Generator[Any, None, None]: ... class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @@ -43,7 +43,7 @@ class AbstractEventLoop(metaclass=ABCMeta): # Can't use a union, see mypy issue # 1873. @overload @abstractmethod - def run_until_complete(self, future: Generator[Any, Any, _T]) -> _T: ... + def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ... @overload @abstractmethod def run_until_complete(self, future: Awaitable[_T]) -> _T: ... @@ -59,7 +59,7 @@ class AbstractEventLoop(metaclass=ABCMeta): if sys.version_info >= (3, 6): @abstractmethod @coroutine - def shutdown_asyncgens(self) -> Generator[Any, Any, None]: ... + def shutdown_asyncgens(self) -> Generator[Any, None, None]: ... # Methods scheduling callbacks. All these return Handles. @abstractmethod def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ... @@ -86,61 +86,61 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def run_in_executor(self, executor: Any, - callback: Callable[..., Any], *args: Any) -> Generator[None, None, Any]: ... + callback: Callable[..., Any], *args: Any) -> Generator[Any, None, Any]: ... @abstractmethod def set_default_executor(self, executor: Any) -> None: ... # Network I/O methods returning Futures. @abstractmethod @coroutine def getaddrinfo(self, host: str, port: int, *, - family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, List[Tuple[int, int, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]]: ... + family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Generator[Any, None, List[Tuple[int, int, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]]]: ... @abstractmethod @coroutine - def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[None, None, Tuple[str, int]]: ... + def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ... @abstractmethod @coroutine def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., - local_addr: str = ..., server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... + local_addr: str = ..., server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[None, None, Any]: ... + sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, Any]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *, ssl: _SSLContext = ..., sock: Any = ..., - server_hostname: str = ...) -> Generator[None, None, _TransProtPair]: ... + server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *, - sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[None, None, Any]: ... + sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, Any]: ... @abstractmethod @coroutine def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory, local_addr: str = ..., remote_addr: str = ..., *, - family: int = ..., proto: int = ..., flags: int = ...) -> Generator[None, None, _TransProtPair]: ... + family: int = ..., proto: int = ..., flags: int = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: _SSLContext = ...) -> Generator[None, None, _TransProtPair]: ... + def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: _SSLContext = ...) -> Generator[Any, None, _TransProtPair]: ... # Pipes and subprocesses. @abstractmethod @coroutine - def connect_read_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[None, None, _TransProtPair]: ... + def connect_read_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_write_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[None, None, _TransProtPair]: ... + def connect_write_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def subprocess_shell(self, protocol_factory: _ProtocolFactory, cmd: Union[bytes, str], *, stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... + **kwargs: Any) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def subprocess_exec(self, protocol_factory: _ProtocolFactory, *args: List[Any], stdin: Any = ..., stdout: Any = ..., stderr: Any = ..., - **kwargs: Any) -> Generator[None, None, _TransProtPair]: ... + **kwargs: Any) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod def add_reader(self, fd: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... @abstractmethod @@ -152,16 +152,16 @@ class AbstractEventLoop(metaclass=ABCMeta): # Completion based I/O methods returning Futures. @abstractmethod @coroutine - def sock_recv(self, sock: Any, nbytes: int) -> Generator[None, None, Any]: ... # TODO + def sock_recv(self, sock: Any, nbytes: int) -> Generator[Any, None, Any]: ... # TODO @abstractmethod @coroutine - def sock_sendall(self, sock: Any, data: bytes) -> Generator[None, None, None]: ... # TODO + def sock_sendall(self, sock: Any, data: bytes) -> Generator[Any, None, None]: ... # TODO @abstractmethod @coroutine - def sock_connect(self, sock: Any, address: str) -> Generator[None, None, Any]: ... # TODO + def sock_connect(self, sock: Any, address: str) -> Generator[Any, None, Any]: ... # TODO @abstractmethod @coroutine - def sock_accept(self, sock: Any) -> Generator[None, None, Any]: ... + def sock_accept(self, sock: Any) -> Generator[Any, None, Any]: ... # Signal handling. @abstractmethod def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... From 2e361e32ff7e3094df9e7e961b4429562084e01e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:19:40 -0800 Subject: [PATCH 07/10] concretize arguments to sock_* methods --- stdlib/3.4/asyncio/events.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index c3259162d9d0..be60309d3bac 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -1,3 +1,4 @@ +from socket import socket import ssl import sys from typing import Any, Awaitable, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union, overload @@ -152,16 +153,16 @@ class AbstractEventLoop(metaclass=ABCMeta): # Completion based I/O methods returning Futures. @abstractmethod @coroutine - def sock_recv(self, sock: Any, nbytes: int) -> Generator[Any, None, Any]: ... # TODO + def sock_recv(self, sock: socket, nbytes: int) -> Generator[Any, None, bytes]: ... @abstractmethod @coroutine - def sock_sendall(self, sock: Any, data: bytes) -> Generator[Any, None, None]: ... # TODO + def sock_sendall(self, sock: socket, data: bytes) -> Generator[Any, None, None]: ... @abstractmethod @coroutine - def sock_connect(self, sock: Any, address: str) -> Generator[Any, None, Any]: ... # TODO + def sock_connect(self, sock: socket, address: str) -> Generator[Any, None, None]: ... @abstractmethod @coroutine - def sock_accept(self, sock: Any) -> Generator[Any, None, Any]: ... + def sock_accept(self, sock: socket) -> Generator[Any, None, Tuple[socket, Any]]: ... # Signal handling. @abstractmethod def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: List[Any]) -> None: ... From aff835e42f9c95f96a2626b78708575999a56bfe Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:21:41 -0800 Subject: [PATCH 08/10] add more socket arguments --- stdlib/3.4/asyncio/events.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index be60309d3bac..7abec97f6bab 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -101,22 +101,22 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, - ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ..., + ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket = ..., local_addr: str = ..., server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, Any]: ... + sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, Any]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *, - ssl: _SSLContext = ..., sock: Any = ..., + ssl: _SSLContext = ..., sock: socket = ..., server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *, - sock: Any = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, Any]: ... + sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, Any]: ... @abstractmethod @coroutine def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory, @@ -124,7 +124,7 @@ class AbstractEventLoop(metaclass=ABCMeta): family: int = ..., proto: int = ..., flags: int = ...) -> Generator[Any, None, _TransProtPair]: ... @abstractmethod @coroutine - def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: Any, *, ssl: _SSLContext = ...) -> Generator[Any, None, _TransProtPair]: ... + def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: socket, *, ssl: _SSLContext = ...) -> Generator[Any, None, _TransProtPair]: ... # Pipes and subprocesses. @abstractmethod @coroutine From 6e366edc96bd4dba760bdf42b57b2d1c8b6c118e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:26:09 -0800 Subject: [PATCH 09/10] fix a few more Any return types --- stdlib/3.4/asyncio/events.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 7abec97f6bab..1b33f235aaed 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -87,7 +87,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def run_in_executor(self, executor: Any, - callback: Callable[..., Any], *args: Any) -> Generator[Any, None, Any]: ... + callback: Callable[..., _T], *args: Any) -> Generator[Any, None, _T]: ... @abstractmethod def set_default_executor(self, executor: Any) -> None: ... # Network I/O methods returning Futures. @@ -107,7 +107,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @coroutine def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, Any]: ... + sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, AbstractServer]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *, @@ -116,7 +116,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @abstractmethod @coroutine def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *, - sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, Any]: ... + sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, AbstractServer]: ... @abstractmethod @coroutine def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory, From 69694d94924d705fb544ecd264a4091b67b94602 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 10 Mar 2017 19:28:55 -0800 Subject: [PATCH 10/10] reuse_address is an Optional bool --- stdlib/3.4/asyncio/events.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3.4/asyncio/events.pyi b/stdlib/3.4/asyncio/events.pyi index 1b33f235aaed..7aaa331e2b49 100644 --- a/stdlib/3.4/asyncio/events.pyi +++ b/stdlib/3.4/asyncio/events.pyi @@ -107,7 +107,7 @@ class AbstractEventLoop(metaclass=ABCMeta): @coroutine def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *, family: int = ..., flags: int = ..., - sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Any = ...) -> Generator[Any, None, AbstractServer]: ... + sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Optional[bool] = ...) -> Generator[Any, None, AbstractServer]: ... @abstractmethod @coroutine def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *,