Skip to content

Commit

Permalink
Add missing Python 3.7 and 3.8 annotations (#3399)
Browse files Browse the repository at this point in the history
* 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()
  • Loading branch information
srittau authored and JelleZijlstra committed Oct 30, 2019
1 parent 4770059 commit 5e9f66c
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 58 deletions.
6 changes: 3 additions & 3 deletions stdlib/3/asyncio/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ from asyncio.events import (
_set_running_loop as _set_running_loop,
_get_running_loop as _get_running_loop,
)
if sys.platform != 'win32':
if sys.platform == 'win32':
from asyncio.windows_events import *
else:
from asyncio.streams import (
open_unix_connection as open_unix_connection,
start_unix_server as start_unix_server,
Expand All @@ -111,8 +113,6 @@ if sys.version_info >= (3, 7):
# currently disallows this.
# See https://github.com/python/mypy/issues/1843
SelectorEventLoop: Type[AbstractEventLoop]
if sys.platform == 'win32':
ProactorEventLoop: Type[AbstractEventLoop]
DefaultEventLoopPolicy: Type[AbstractEventLoopPolicy]

# TODO: AbstractChildWatcher (UNIX only)
Expand Down
88 changes: 71 additions & 17 deletions stdlib/3/asyncio/base_events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ from asyncio.protocols import BaseProtocol
from asyncio.tasks import Task
from asyncio.transports import BaseTransport

if sys.version_info >= (3, 7):
from contextvars import Context

_T = TypeVar('_T')
_Context = Dict[str, Any]
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
Expand All @@ -37,9 +40,18 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
@coroutine
def shutdown_asyncgens(self) -> Generator[Any, None, None]: ...
# Methods scheduling callbacks. All these return Handles.
def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
def call_later(self, delay: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
def call_at(self, when: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
if sys.version_info >= (3, 7):
def call_soon(self, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...) -> Handle: ...
def call_later(
self, delay: float, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...,
) -> TimerHandle: ...
def call_at(
self, when: float, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...,
) -> TimerHandle: ...
else:
def call_soon(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
def call_later(self, delay: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
def call_at(self, when: float, callback: Callable[..., Any], *args: Any) -> TimerHandle: ...
def time(self) -> float: ...
# Future methods
def create_future(self) -> Future[Any]: ...
Expand All @@ -53,7 +65,10 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
def set_task_factory(self, factory: Optional[Callable[[AbstractEventLoop, Generator[Any, None, _T]], Future[_T]]]) -> None: ...
def get_task_factory(self) -> Optional[Callable[[AbstractEventLoop, Generator[Any, None, _T]], Future[_T]]]: ...
# Methods for interacting with threads
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
if sys.version_info >= (3, 7):
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any, context: Optional[Context] = ...) -> Handle: ...
else:
def call_soon_threadsafe(self, callback: Callable[..., Any], *args: Any) -> Handle: ...
@coroutine
def run_in_executor(self, executor: Any,
func: Callable[..., _T], *args: Any) -> Generator[Any, None, _T]: ...
Expand All @@ -67,9 +82,44 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
flags: int = ...) -> Generator[Any, None, List[Tuple[int, int, int, str, Tuple[Any, ...]]]]: ...
@coroutine
def getnameinfo(self, sockaddr: Tuple[Any, ...], flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ...
if sys.version_info >= (3, 7):
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
fallback: bool = ...) -> int: ...
if sys.version_info >= (3, 8):
@overload
async def create_connection(
self,
protocol_factory: _ProtocolFactory,
host: str = ...,
port: int = ...,
*,
ssl: _SSLContext = ...,
family: int = ...,
proto: int = ...,
flags: int = ...,
sock: None = ...,
local_addr: Optional[str] = ...,
server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...,
happy_eyeballs_delay: Optional[float] = ...,
interleave: Optional[int] = ...,
) -> _TransProtPair: ...
@overload
async def create_connection(
self,
protocol_factory: _ProtocolFactory,
host: None = ...,
port: None = ...,
*,
ssl: _SSLContext = ...,
family: int = ...,
proto: int = ...,
flags: int = ...,
sock: socket,
local_addr: None = ...,
server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...,
happy_eyeballs_delay: Optional[float] = ...,
interleave: Optional[int] = ...,
) -> _TransProtPair: ...
elif sys.version_info >= (3, 7):
@overload
async def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
Expand All @@ -80,6 +130,20 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
sock: socket, local_addr: None = ..., server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...) -> _TransProtPair: ...
else:
@overload
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
if sys.version_info >= (3, 7):
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
fallback: bool = ...) -> int: ...
@overload
async def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ...,
port: int = ..., *, family: int = ..., flags: int = ..., sock: None = ..., backlog: int = ...,
Expand All @@ -100,16 +164,6 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
else:
@overload
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@coroutine
def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ..., port: int = ..., *,
family: int = ..., flags: int = ...,
sock: None = ..., backlog: int = ..., ssl: _SSLContext = ...,
Expand Down
78 changes: 62 additions & 16 deletions stdlib/3/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ class Handle:
def __repr__(self) -> str: ...
def cancel(self) -> None: ...
def _run(self) -> None: ...
def cancelled(self) -> bool: ...
if sys.version_info >= (3, 7):
def cancelled(self) -> bool: ...

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

class AbstractServer:
sockets: Optional[List[socket]]
def close(self) -> None: ...
if sys.version_info >= (3, 7):
async def __aenter__(self: _T) -> _T: ...
async def __aexit__(self, *exc: Any) -> None: ...
def get_loop(self) -> AbstractEventLoop: ...
def is_serving(self) -> bool: ...
async def start_serving(self) -> None: ...
Expand Down Expand Up @@ -112,10 +117,46 @@ class AbstractEventLoop(metaclass=ABCMeta):
@abstractmethod
@coroutine
def getnameinfo(self, sockaddr: Tuple[Any, ...], flags: int = ...) -> Generator[Any, None, Tuple[str, int]]: ...
if sys.version_info >= (3, 7):
if sys.version_info >= (3, 8):
@overload
@abstractmethod
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
fallback: bool = ...) -> int: ...
async def create_connection(
self,
protocol_factory: _ProtocolFactory,
host: str = ...,
port: int = ...,
*,
ssl: _SSLContext = ...,
family: int = ...,
proto: int = ...,
flags: int = ...,
sock: None = ...,
local_addr: Optional[str] = ...,
server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...,
happy_eyeballs_delay: Optional[float] = ...,
interleave: Optional[int] = ...,
) -> _TransProtPair: ...
@overload
@abstractmethod
async def create_connection(
self,
protocol_factory: _ProtocolFactory,
host: None = ...,
port: None = ...,
*,
ssl: _SSLContext = ...,
family: int = ...,
proto: int = ...,
flags: int = ...,
sock: socket,
local_addr: None = ...,
server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...,
happy_eyeballs_delay: Optional[float] = ...,
interleave: Optional[int] = ...,
) -> _TransProtPair: ...
elif sys.version_info >= (3, 7):
@overload
@abstractmethod
async def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
Expand All @@ -128,6 +169,23 @@ class AbstractEventLoop(metaclass=ABCMeta):
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ...,
sock: socket, local_addr: None = ..., server_hostname: Optional[str] = ...,
ssl_handshake_timeout: Optional[float] = ...) -> _TransProtPair: ...
else:
@overload
@abstractmethod
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@abstractmethod
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
if sys.version_info >= (3, 7):
@abstractmethod
async def sock_sendfile(self, sock: socket, file: IO[bytes], offset: int = ..., count: Optional[int] = ..., *,
fallback: bool = ...) -> int: ...
@overload
@abstractmethod
async def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ...,
Expand Down Expand Up @@ -162,18 +220,6 @@ class AbstractEventLoop(metaclass=ABCMeta):
@overload
@abstractmethod
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: str = ..., port: int = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: None = ...,
local_addr: Optional[str] = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@abstractmethod
@coroutine
def create_connection(self, protocol_factory: _ProtocolFactory, host: None = ..., port: None = ..., *,
ssl: _SSLContext = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: socket,
local_addr: None = ..., server_hostname: Optional[str] = ...) -> Generator[Any, None, _TransProtPair]: ...
@overload
@abstractmethod
@coroutine
def create_server(self, protocol_factory: _ProtocolFactory, host: Optional[Union[str, Sequence[str]]] = ..., port: int = ..., *,
family: int = ..., flags: int = ...,
sock: None = ..., backlog: int = ..., ssl: _SSLContext = ...,
Expand Down
42 changes: 32 additions & 10 deletions stdlib/3/asyncio/proactor_events.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@

from typing import Any, Mapping, Optional, Generator
from . import base_events, transports, events, streams, futures, constants
import sys
from asyncio import coroutine
from socket import socket
import sys
from typing import Any, Generator, Mapping, Optional, Union

from . import base_events, constants, events, futures, streams, transports

if sys.version_info >= (3, 7):
from os import PathLike
_Path = Union[str, PathLike[str]]
else:
_Path = str

if sys.version_info >= (3, 8):
from typing import Literal
else:
Expand Down Expand Up @@ -45,12 +52,27 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
# The methods below don't actually exist directly, ProactorEventLoops do not implement them. However, they are
# needed to satisfy mypy
if sys.version_info >= (3, 7):
async def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *, ssl: events._SSLContext = ...,
sock: Optional[socket] = ..., server_hostname: str = ...,
ssl_handshake_timeout: Optional[float] = ...) -> events._TransProtPair: ...
async def create_unix_server(self, protocol_factory: events._ProtocolFactory, path: str, *, sock: Optional[socket] = ...,
backlog: int = ..., ssl: events._SSLContext = ..., ssl_handshake_timeout: Optional[float] = ...,
start_serving: bool = ...) -> events.AbstractServer: ...
async def create_unix_connection(
self,
protocol_factory: events._ProtocolFactory,
path: _Path,
*,
ssl: events._SSLContext = ...,
sock: Optional[socket] = ...,
server_hostname: str = ...,
ssl_handshake_timeout: Optional[float] = ...,
) -> events._TransProtPair: ...
async def create_unix_server(
self,
protocol_factory: events._ProtocolFactory,
path: _Path,
*,
sock: Optional[socket] = ...,
backlog: int = ...,
ssl: events._SSLContext = ...,
ssl_handshake_timeout: Optional[float] = ...,
start_serving: bool = ...,
) -> events.AbstractServer: ...
else:
@coroutine
def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *,
Expand Down
43 changes: 32 additions & 11 deletions stdlib/3/asyncio/selector_events.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@

from typing import Optional, Any, Generator
from . import base_events, events
from socket import socket
from asyncio import coroutine
import selectors
import sys
from asyncio import coroutine
from socket import socket
from typing import Any, Generator, Optional, Union

from . import base_events, events

if sys.version_info >= (3, 7):
from os import PathLike
_Path = Union[str, PathLike[str]]
else:
_Path = str

class BaseSelectorEventLoop(base_events.BaseEventLoop):

def __init__(self, selector: selectors.BaseSelector = ...) -> None: ...
if sys.version_info >= (3, 7):
async def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *, ssl: events._SSLContext = ...,
sock: Optional[socket] = ..., server_hostname: str = ...,
ssl_handshake_timeout: Optional[float] = ...) -> events._TransProtPair: ...
async def create_unix_server(self, protocol_factory: events._ProtocolFactory, path: str, *, sock: Optional[socket] = ...,
backlog: int = ..., ssl: events._SSLContext = ..., ssl_handshake_timeout: Optional[float] = ...,
start_serving: bool = ...) -> events.AbstractServer: ...
async def create_unix_connection(
self,
protocol_factory: events._ProtocolFactory,
path: _Path,
*,
ssl: events._SSLContext = ...,
sock: Optional[socket] = ...,
server_hostname: str = ...,
ssl_handshake_timeout: Optional[float] = ...,
) -> events._TransProtPair: ...
async def create_unix_server(
self,
protocol_factory: events._ProtocolFactory,
path: _Path,
*,
sock: Optional[socket] = ...,
backlog: int = ...,
ssl: events._SSLContext = ...,
ssl_handshake_timeout: Optional[float] = ...,
start_serving: bool = ...,
) -> events.AbstractServer: ...
else:
@coroutine
def create_unix_connection(self, protocol_factory: events._ProtocolFactory, path: str, *,
Expand Down
2 changes: 2 additions & 0 deletions stdlib/3/asyncio/streams.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def open_connection(
*,
loop: Optional[events.AbstractEventLoop] = ...,
limit: int = ...,
ssl_handshake_timeout: Optional[float] = ...,
**kwds: Any
) -> Generator[Any, None, Tuple[StreamReader, StreamWriter]]: ...

Expand All @@ -36,6 +37,7 @@ def start_server(
*,
loop: Optional[events.AbstractEventLoop] = ...,
limit: int = ...,
ssl_handshake_timeout: Optional[float] = ...,
**kwds: Any
) -> Generator[Any, None, events.AbstractServer]: ...

Expand Down
Loading

0 comments on commit 5e9f66c

Please sign in to comment.