Skip to content

Commit 3e94c46

Browse files
JelleZijlstragvanrossum
authored andcommitted
Add missing methods and @coroutine decorators to AbstractEventLoop (#958)
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. Also fixed 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).
1 parent b6eec58 commit 3e94c46

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed

stdlib/3.4/asyncio/events.pyi

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
from socket import socket
2+
import ssl
13
import sys
24
from typing import Any, Awaitable, Callable, Dict, Generator, List, Optional, Tuple, TypeVar, Union, overload
35
from abc import ABCMeta, abstractmethod
46
from asyncio.futures import Future
57
from asyncio.coroutines import coroutine
8+
from asyncio.protocols import BaseProtocol
69
from asyncio.tasks import Task
10+
from asyncio.transports import BaseTransport
711

812
__all__ = ... # type: str
913

1014
_T = TypeVar('_T')
1115
_Context = Dict[str, Any]
16+
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
17+
_ProtocolFactory = Callable[[], BaseProtocol]
18+
_SSLContext = Union[bool, None, ssl.SSLContext]
19+
_TransProtPair = Tuple[BaseTransport, BaseProtocol]
1220

1321
PIPE = ... # type: Any # from subprocess.PIPE
1422

@@ -27,7 +35,7 @@ class Handle:
2735
class AbstractServer:
2836
def close(self) -> None: ...
2937
@coroutine
30-
def wait_closed(self) -> Generator[Any, Any, None]: ...
38+
def wait_closed(self) -> Generator[Any, None, None]: ...
3139

3240
class AbstractEventLoop(metaclass=ABCMeta):
3341
@abstractmethod
@@ -36,7 +44,7 @@ class AbstractEventLoop(metaclass=ABCMeta):
3644
# Can't use a union, see mypy issue # 1873.
3745
@overload
3846
@abstractmethod
39-
def run_until_complete(self, future: Generator[Any, Any, _T]) -> _T: ...
47+
def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ...
4048
@overload
4149
@abstractmethod
4250
def run_until_complete(self, future: Awaitable[_T]) -> _T: ...
@@ -46,7 +54,13 @@ class AbstractEventLoop(metaclass=ABCMeta):
4654
@abstractmethod
4755
def is_running(self) -> bool: ...
4856
@abstractmethod
57+
def is_closed(self) -> bool: ...
58+
@abstractmethod
4959
def close(self) -> None: ...
60+
if sys.version_info >= (3, 6):
61+
@abstractmethod
62+
@coroutine
63+
def shutdown_asyncgens(self) -> Generator[Any, None, None]: ...
5064
# Methods scheduling callbacks. All these return Handles.
5165
@abstractmethod
5266
def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
@@ -71,48 +85,63 @@ class AbstractEventLoop(metaclass=ABCMeta):
7185
@abstractmethod
7286
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
7387
@abstractmethod
88+
@coroutine
7489
def run_in_executor(self, executor: Any,
75-
callback: Callable[..., Any], *args: Any) -> Future[Any]: ...
90+
callback: Callable[..., _T], *args: Any) -> Generator[Any, None, _T]: ...
7691
@abstractmethod
7792
def set_default_executor(self, executor: Any) -> None: ...
7893
# Network I/O methods returning Futures.
7994
@abstractmethod
95+
@coroutine
8096
def getaddrinfo(self, host: str, port: int, *,
81-
family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Future[List[Tuple[int, int, int, str, tuple]]]: ...
97+
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]]]]]: ...
8298
@abstractmethod
83-
def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Future[Tuple[str, int]]: ...
99+
@coroutine
100+
def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ...
84101
@abstractmethod
85-
def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *,
86-
ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ...,
87-
local_addr: str = ..., server_hostname: str = ...) -> tuple: ...
102+
@coroutine
103+
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
104+
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket = ...,
105+
local_addr: str = ..., server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ...
88106
@abstractmethod
89-
def create_server(self, protocol_factory: Any, host: str = ..., port: int = ..., *,
107+
@coroutine
108+
def create_server(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
90109
family: int = ..., flags: int = ...,
91-
sock: Any = ..., backlog: int = ..., ssl: Any = ..., reuse_address: Any = ...) -> Any: ...
110+
sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ..., reuse_address: Optional[bool] = ...) -> Generator[Any, None, AbstractServer]: ...
92111
@abstractmethod
93-
def create_unix_connection(self, protocol_factory: Any, path: str, *,
94-
ssl: Any = ..., sock: Any = ...,
95-
server_hostname: str = ...) -> tuple: ...
112+
@coroutine
113+
def create_unix_connection(self, protocol_factory: _ProtocolFactory, path: str, *,
114+
ssl: _SSLContext = ..., sock: socket = ...,
115+
server_hostname: str = ...) -> Generator[Any, None, _TransProtPair]: ...
96116
@abstractmethod
97-
def create_unix_server(self, protocol_factory: Any, path: str, *,
98-
sock: Any = ..., backlog: int = ..., ssl: Any = ...) -> Any: ...
117+
@coroutine
118+
def create_unix_server(self, protocol_factory: _ProtocolFactory, path: str, *,
119+
sock: socket = ..., backlog: int = ..., ssl: _SSLContext = ...) -> Generator[Any, None, AbstractServer]: ...
99120
@abstractmethod
100-
def create_datagram_endpoint(self, protocol_factory: Any,
121+
@coroutine
122+
def create_datagram_endpoint(self, protocol_factory: _ProtocolFactory,
101123
local_addr: str = ..., remote_addr: str = ..., *,
102-
family: int = ..., proto: int = ..., flags: int = ...) -> tuple: ...
124+
family: int = ..., proto: int = ..., flags: int = ...) -> Generator[Any, None, _TransProtPair]: ...
125+
@abstractmethod
126+
@coroutine
127+
def connect_accepted_socket(self, protocol_factory: _ProtocolFactory, sock: socket, *, ssl: _SSLContext = ...) -> Generator[Any, None, _TransProtPair]: ...
103128
# Pipes and subprocesses.
104129
@abstractmethod
105-
def connect_read_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ...
130+
@coroutine
131+
def connect_read_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[Any, None, _TransProtPair]: ...
106132
@abstractmethod
107-
def connect_write_pipe(self, protocol_factory: Any, pipe: Any) -> tuple: ...
133+
@coroutine
134+
def connect_write_pipe(self, protocol_factory: _ProtocolFactory, pipe: Any) -> Generator[Any, None, _TransProtPair]: ...
108135
@abstractmethod
109-
def subprocess_shell(self, protocol_factory: Any, cmd: Union[bytes, str], *, stdin: Any = ...,
136+
@coroutine
137+
def subprocess_shell(self, protocol_factory: _ProtocolFactory, cmd: Union[bytes, str], *, stdin: Any = ...,
110138
stdout: Any = ..., stderr: Any = ...,
111-
**kwargs: Any) -> tuple: ...
139+
**kwargs: Any) -> Generator[Any, None, _TransProtPair]: ...
112140
@abstractmethod
113-
def subprocess_exec(self, protocol_factory: Any, *args: List[Any], stdin: Any = ...,
141+
@coroutine
142+
def subprocess_exec(self, protocol_factory: _ProtocolFactory, *args: List[Any], stdin: Any = ...,
114143
stdout: Any = ..., stderr: Any = ...,
115-
**kwargs: Any) -> tuple: ...
144+
**kwargs: Any) -> Generator[Any, None, _TransProtPair]: ...
116145
@abstractmethod
117146
def add_reader(self, fd: int, callback: Callable[..., Any], *args: List[Any]) -> None: ...
118147
@abstractmethod
@@ -123,21 +152,27 @@ class AbstractEventLoop(metaclass=ABCMeta):
123152
def remove_writer(self, fd: int) -> None: ...
124153
# Completion based I/O methods returning Futures.
125154
@abstractmethod
126-
def sock_recv(self, sock: Any, nbytes: int) -> Any: ... # TODO
155+
@coroutine
156+
def sock_recv(self, sock: socket, nbytes: int) -> Generator[Any, None, bytes]: ...
127157
@abstractmethod
128-
def sock_sendall(self, sock: Any, data: bytes) -> None: ... # TODO
158+
@coroutine
159+
def sock_sendall(self, sock: socket, data: bytes) -> Generator[Any, None, None]: ...
129160
@abstractmethod
130-
def sock_connect(self, sock: Any, address: str) -> Any: ... # TODO
161+
@coroutine
162+
def sock_connect(self, sock: socket, address: str) -> Generator[Any, None, None]: ...
131163
@abstractmethod
132-
def sock_accept(self, sock: Any) -> Any: ...
164+
@coroutine
165+
def sock_accept(self, sock: socket) -> Generator[Any, None, Tuple[socket, Any]]: ...
133166
# Signal handling.
134167
@abstractmethod
135168
def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: List[Any]) -> None: ...
136169
@abstractmethod
137170
def remove_signal_handler(self, sig: int) -> None: ...
138171
# Error handlers.
139172
@abstractmethod
140-
def set_exception_handler(self, handler: Callable[[AbstractEventLoop, _Context], Any]) -> None: ...
173+
def set_exception_handler(self, handler: _ExceptionHandler) -> None: ...
174+
@abstractmethod
175+
def get_exception_handler(self) -> _ExceptionHandler: ...
141176
@abstractmethod
142177
def default_exception_handler(self, context: _Context) -> None: ...
143178
@abstractmethod

0 commit comments

Comments
 (0)