Skip to content

Commit 5e9f66c

Browse files
srittauJelleZijlstra
authored andcommitted
Add missing Python 3.7 and 3.8 annotations (#3399)
* Add explicit ssl_handshake_timeout arguments to open_connection and start_server * Add context arguments to call methods * Accept PathLike for create_unix_* paths * Add TimerHandle.when() Add missing version check * AbstractServer is now an async context manager * Add happy_eyeballs_delay and interleave arguments to create_connection * Re-export asyncio.windows_events from asyncio * Add name argument to Task constructor * Add Task.get_coro() * import and other fixes * Fix return type of get_coro()
1 parent 4770059 commit 5e9f66c

File tree

7 files changed

+213
-58
lines changed

7 files changed

+213
-58
lines changed

stdlib/3/asyncio/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ from asyncio.events import (
8787
_set_running_loop as _set_running_loop,
8888
_get_running_loop as _get_running_loop,
8989
)
90-
if sys.platform != 'win32':
90+
if sys.platform == 'win32':
91+
from asyncio.windows_events import *
92+
else:
9193
from asyncio.streams import (
9294
open_unix_connection as open_unix_connection,
9395
start_unix_server as start_unix_server,
@@ -111,8 +113,6 @@ if sys.version_info >= (3, 7):
111113
# currently disallows this.
112114
# See https://github.com/python/mypy/issues/1843
113115
SelectorEventLoop: Type[AbstractEventLoop]
114-
if sys.platform == 'win32':
115-
ProactorEventLoop: Type[AbstractEventLoop]
116116
DefaultEventLoopPolicy: Type[AbstractEventLoopPolicy]
117117

118118
# TODO: AbstractChildWatcher (UNIX only)

stdlib/3/asyncio/base_events.pyi

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ from asyncio.protocols import BaseProtocol
1111
from asyncio.tasks import Task
1212
from asyncio.transports import BaseTransport
1313

14+
if sys.version_info >= (3, 7):
15+
from contextvars import Context
16+
1417
_T = TypeVar('_T')
1518
_Context = Dict[str, Any]
1619
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
@@ -37,9 +40,18 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
3740
@coroutine
3841
def shutdown_asyncgens(self) -> Generator[Any, None, None]: ...
3942
# Methods scheduling callbacks. All these return Handles.
40-
def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
41-
def call_later(self, delay: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
42-
def call_at(self, when: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
43+
if sys.version_info >= (3, 7):
44+
def call_soon(self, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...) -> Handle: ...
45+
def call_later(
46+
self, delay: float, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...,
47+
) -> TimerHandle: ...
48+
def call_at(
49+
self, when: float, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...,
50+
) -> TimerHandle: ...
51+
else:
52+
def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
53+
def call_later(self, delay: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
54+
def call_at(self, when: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
4355
def time(self) -> float: ...
4456
# Future methods
4557
def create_future(self) -> Future[Any]: ...
@@ -53,7 +65,10 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
5365
def set_task_factory(self, factory: Optional[Callable[[AbstractEventLoop, Generator[Any, None, _T]], Future[_T]]]) -> None: ...
5466
def get_task_factory(self) -> Optional[Callable[[AbstractEventLoop, Generator[Any, None, _T]], Future[_T]]]: ...
5567
# Methods for interacting with threads
56-
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
68+
if sys.version_info >= (3, 7):
69+
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...) -> Handle: ...
70+
else:
71+
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
5772
@coroutine
5873
def run_in_executor(self, executor: Any,
5974
func: Callable[..., _T], *args: Any) -> Generator[Any, None, _T]: ...
@@ -67,9 +82,44 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
6782
flags: int = ...) -> Generator[Any, None, List[Tuple[int, int, int, str, Tuple[Any, ...]]]]: ...
6883
@coroutine
6984
def getnameinfo(self, sockaddr: Tuple[Any, ...], flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ...
70-
if sys.version_info >= (3, 7):
71-
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
72-
fallback: bool = ...) -> int: ...
85+
if sys.version_info >= (3, 8):
86+
@overload
87+
async def create_connection(
88+
self,
89+
protocol_factory: _ProtocolFactory,
90+
host: str = ...,
91+
port: int = ...,
92+
*,
93+
ssl: _SSLContext = ...,
94+
family: int = ...,
95+
proto: int = ...,
96+
flags: int = ...,
97+
sock: None = ...,
98+
local_addr: Optional[str] = ...,
99+
server_hostname: Optional[str] = ...,
100+
ssl_handshake_timeout: Optional[float] = ...,
101+
happy_eyeballs_delay: Optional[float] = ...,
102+
interleave: Optional[int] = ...,
103+
) -> _TransProtPair: ...
104+
@overload
105+
async def create_connection(
106+
self,
107+
protocol_factory: _ProtocolFactory,
108+
host: None = ...,
109+
port: None = ...,
110+
*,
111+
ssl: _SSLContext = ...,
112+
family: int = ...,
113+
proto: int = ...,
114+
flags: int = ...,
115+
sock: socket,
116+
local_addr: None = ...,
117+
server_hostname: Optional[str] = ...,
118+
ssl_handshake_timeout: Optional[float] = ...,
119+
happy_eyeballs_delay: Optional[float] = ...,
120+
interleave: Optional[int] = ...,
121+
) -> _TransProtPair: ...
122+
elif sys.version_info >= (3, 7):
73123
@overload
74124
async def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
75125
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
@@ -80,6 +130,20 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
80130
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
81131
sock: socket, local_addr: None = ..., server_hostname: Optional[str] = ...,
82132
ssl_handshake_timeout: Optional[float] = ...) -> _TransProtPair: ...
133+
else:
134+
@overload
135+
@coroutine
136+
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
137+
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
138+
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
139+
@overload
140+
@coroutine
141+
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
142+
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
143+
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
144+
if sys.version_info >= (3, 7):
145+
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
146+
fallback: bool = ...) -> int: ...
83147
@overload
84148
async def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ...,
85149
port: int = ..., *, family: int = ..., flags: int = ..., sock: None = ..., backlog: int = ...,
@@ -100,16 +164,6 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
100164
else:
101165
@overload
102166
@coroutine
103-
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
104-
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
105-
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
106-
@overload
107-
@coroutine
108-
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
109-
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
110-
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
111-
@overload
112-
@coroutine
113167
def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ..., port: int = ..., *,
114168
family: int = ..., flags: int = ...,
115169
sock: None = ..., backlog: int = ..., ssl: _SSLContext = ...,

stdlib/3/asyncio/events.pyi

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ class Handle:
2424
def __repr__(self) -> str: ...
2525
def cancel(self) -> None: ...
2626
def _run(self) -> None: ...
27-
def cancelled(self) -> bool: ...
27+
if sys.version_info >= (3, 7):
28+
def cancelled(self) -> bool: ...
2829

2930
class TimerHandle(Handle):
3031
def __init__(self, when: float, callback: Callable[..., Any], args: List[Any],
3132
loop: AbstractEventLoop) -> None: ...
3233
def __hash__(self) -> int: ...
34+
if sys.version_info >= (3, 7):
35+
def when(self) -> float: ...
3336

3437
class AbstractServer:
3538
sockets: Optional[List[socket]]
3639
def close(self) -> None: ...
3740
if sys.version_info >= (3, 7):
41+
async def __aenter__(self: _T) -> _T: ...
42+
async def __aexit__(self, *exc: Any) -> None: ...
3843
def get_loop(self) -> AbstractEventLoop: ...
3944
def is_serving(self) -> bool: ...
4045
async def start_serving(self) -> None: ...
@@ -112,10 +117,46 @@ class AbstractEventLoop(metaclass=ABCMeta):
112117
@abstractmethod
113118
@coroutine
114119
def getnameinfo(self, sockaddr: Tuple[Any, ...], flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ...
115-
if sys.version_info >= (3, 7):
120+
if sys.version_info >= (3, 8):
121+
@overload
116122
@abstractmethod
117-
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
118-
fallback: bool = ...) -> int: ...
123+
async def create_connection(
124+
self,
125+
protocol_factory: _ProtocolFactory,
126+
host: str = ...,
127+
port: int = ...,
128+
*,
129+
ssl: _SSLContext = ...,
130+
family: int = ...,
131+
proto: int = ...,
132+
flags: int = ...,
133+
sock: None = ...,
134+
local_addr: Optional[str] = ...,
135+
server_hostname: Optional[str] = ...,
136+
ssl_handshake_timeout: Optional[float] = ...,
137+
happy_eyeballs_delay: Optional[float] = ...,
138+
interleave: Optional[int] = ...,
139+
) -> _TransProtPair: ...
140+
@overload
141+
@abstractmethod
142+
async def create_connection(
143+
self,
144+
protocol_factory: _ProtocolFactory,
145+
host: None = ...,
146+
port: None = ...,
147+
*,
148+
ssl: _SSLContext = ...,
149+
family: int = ...,
150+
proto: int = ...,
151+
flags: int = ...,
152+
sock: socket,
153+
local_addr: None = ...,
154+
server_hostname: Optional[str] = ...,
155+
ssl_handshake_timeout: Optional[float] = ...,
156+
happy_eyeballs_delay: Optional[float] = ...,
157+
interleave: Optional[int] = ...,
158+
) -> _TransProtPair: ...
159+
elif sys.version_info >= (3, 7):
119160
@overload
120161
@abstractmethod
121162
async def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
@@ -128,6 +169,23 @@ class AbstractEventLoop(metaclass=ABCMeta):
128169
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
129170
sock: socket, local_addr: None = ..., server_hostname: Optional[str] = ...,
130171
ssl_handshake_timeout: Optional[float] = ...) -> _TransProtPair: ...
172+
else:
173+
@overload
174+
@abstractmethod
175+
@coroutine
176+
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
177+
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
178+
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
179+
@overload
180+
@abstractmethod
181+
@coroutine
182+
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
183+
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
184+
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
185+
if sys.version_info >= (3, 7):
186+
@abstractmethod
187+
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
188+
fallback: bool = ...) -> int: ...
131189
@overload
132190
@abstractmethod
133191
async def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ...,
@@ -162,18 +220,6 @@ class AbstractEventLoop(metaclass=ABCMeta):
162220
@overload
163221
@abstractmethod
164222
@coroutine
165-
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
166-
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
167-
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
168-
@overload
169-
@abstractmethod
170-
@coroutine
171-
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
172-
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
173-
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
174-
@overload
175-
@abstractmethod
176-
@coroutine
177223
def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ..., port: int = ..., *,
178224
family: int = ..., flags: int = ...,
179225
sock: None = ..., backlog: int = ..., ssl: _SSLContext = ...,

stdlib/3/asyncio/proactor_events.pyi

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
2-
from typing import Any, Mapping, Optional, Generator
3-
from . import base_events, transports, events, streams, futures, constants
1+
import sys
42
from asyncio import coroutine
53
from socket import socket
6-
import sys
4+
from typing import Any, Generator, Mapping, Optional, Union
5+
6+
from . import base_events, constants, events, futures, streams, transports
7+
8+
if sys.version_info >= (3, 7):
9+
from os import PathLike
10+
_Path = Union[str, PathLike[str]]
11+
else:
12+
_Path = str
13+
714
if sys.version_info >= (3, 8):
815
from typing import Literal
916
else:
@@ -45,12 +52,27 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
4552
# The methods below don't actually exist directly, ProactorEventLoops do not implement them. However, they are
4653
# needed to satisfy mypy
4754
if sys.version_info >= (3, 7):
48-
async def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *, ssl: events._SSLContext = ...,
49-
sock: Optional[socket] = ..., server_hostname: str = ...,
50-
ssl_handshake_timeout: Optional[float] = ...) -> events._TransProtPair: ...
51-
async def create_unix_server(self, protocol_factory: events._ProtocolFactory, path: str, *, sock: Optional[socket] = ...,
52-
backlog: int = ..., ssl: events._SSLContext = ..., ssl_handshake_timeout: Optional[float] = ...,
53-
start_serving: bool = ...) -> events.AbstractServer: ...
55+
async def create_unix_connection(
56+
self,
57+
protocol_factory: events._ProtocolFactory,
58+
path: _Path,
59+
*,
60+
ssl: events._SSLContext = ...,
61+
sock: Optional[socket] = ...,
62+
server_hostname: str = ...,
63+
ssl_handshake_timeout: Optional[float] = ...,
64+
) -> events._TransProtPair: ...
65+
async def create_unix_server(
66+
self,
67+
protocol_factory: events._ProtocolFactory,
68+
path: _Path,
69+
*,
70+
sock: Optional[socket] = ...,
71+
backlog: int = ...,
72+
ssl: events._SSLContext = ...,
73+
ssl_handshake_timeout: Optional[float] = ...,
74+
start_serving: bool = ...,
75+
) -> events.AbstractServer: ...
5476
else:
5577
@coroutine
5678
def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *,

stdlib/3/asyncio/selector_events.pyi

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
2-
from typing import Optional, Any, Generator
3-
from . import base_events, events
4-
from socket import socket
5-
from asyncio import coroutine
61
import selectors
72
import sys
3+
from asyncio import coroutine
4+
from socket import socket
5+
from typing import Any, Generator, Optional, Union
6+
7+
from . import base_events, events
8+
9+
if sys.version_info >= (3, 7):
10+
from os import PathLike
11+
_Path = Union[str, PathLike[str]]
12+
else:
13+
_Path = str
814

915
class BaseSelectorEventLoop(base_events.BaseEventLoop):
1016

1117
def __init__(self, selector: selectors.BaseSelector = ...) -> None: ...
1218
if sys.version_info >= (3, 7):
13-
async def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *, ssl: events._SSLContext = ...,
14-
sock: Optional[socket] = ..., server_hostname: str = ...,
15-
ssl_handshake_timeout: Optional[float] = ...) -> events._TransProtPair: ...
16-
async def create_unix_server(self, protocol_factory: events._ProtocolFactory, path: str, *, sock: Optional[socket] = ...,
17-
backlog: int = ..., ssl: events._SSLContext = ..., ssl_handshake_timeout: Optional[float] = ...,
18-
start_serving: bool = ...) -> events.AbstractServer: ...
19+
async def create_unix_connection(
20+
self,
21+
protocol_factory: events._ProtocolFactory,
22+
path: _Path,
23+
*,
24+
ssl: events._SSLContext = ...,
25+
sock: Optional[socket] = ...,
26+
server_hostname: str = ...,
27+
ssl_handshake_timeout: Optional[float] = ...,
28+
) -> events._TransProtPair: ...
29+
async def create_unix_server(
30+
self,
31+
protocol_factory: events._ProtocolFactory,
32+
path: _Path,
33+
*,
34+
sock: Optional[socket] = ...,
35+
backlog: int = ...,
36+
ssl: events._SSLContext = ...,
37+
ssl_handshake_timeout: Optional[float] = ...,
38+
start_serving: bool = ...,
39+
) -> events.AbstractServer: ...
1940
else:
2041
@coroutine
2142
def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *,

stdlib/3/asyncio/streams.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def open_connection(
2525
*,
2626
loop: Optional[events.AbstractEventLoop] = ...,
2727
limit: int = ...,
28+
ssl_handshake_timeout: Optional[float] = ...,
2829
**kwds: Any
2930
) -> Generator[Any, None, Tuple[StreamReader, StreamWriter]]: ...
3031

@@ -36,6 +37,7 @@ def start_server(
3637
*,
3738
loop: Optional[events.AbstractEventLoop] = ...,
3839
limit: int = ...,
40+
ssl_handshake_timeout: Optional[float] = ...,
3941
**kwds: Any
4042
) -> Generator[Any, None, events.AbstractServer]: ...
4143

0 commit comments

Comments
 (0)