Skip to content

Commit 6d8628c

Browse files
gvanrossummatthiaskramm
authored andcommitted
Misc asyncio changes (#373)
* Add stub for cgi.parse_header(). * Improve asyncio stubs (far from complete) * More asyncio changes. * Use @overload to solve strange test failures. * Add some TODOs. Make ProactorEventLoop conditional. * Future should not inherit from Awaitable or implement __await__. At least not yet. * Fix AbstractServer.wait_closed() return type. It's a generator, not a future.
1 parent 6aeea2a commit 6d8628c

File tree

7 files changed

+117
-24
lines changed

7 files changed

+117
-24
lines changed

stdlib/3.4/asyncio/__init__.pyi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""The asyncio package, tracking PEP 3156."""
2+
3+
import sys
4+
from typing import Type
5+
26
from asyncio.coroutines import (
37
coroutine as coroutine,
48
iscoroutinefunction as iscoroutinefunction,
@@ -48,7 +52,13 @@ from asyncio.events import (
4852
AbstractEventLoop as AbstractEventLoop,
4953
AbstractServer as AbstractServer,
5054
Handle as Handle,
55+
get_event_loop_policy as get_event_loop_policy,
56+
set_event_loop_policy as set_event_loop_policy,
5157
get_event_loop as get_event_loop,
58+
set_event_loop as set_event_loop,
59+
new_event_loop as new_event_loop,
60+
get_child_watcher as get_child_watcher,
61+
set_child_watcher as set_child_watcher,
5262
)
5363
from asyncio.queues import (
5464
Queue as Queue,
@@ -58,5 +68,22 @@ from asyncio.queues import (
5868
QueueFull as QueueFull,
5969
QueueEmpty as QueueEmpty,
6070
)
71+
from asyncio.locks import (
72+
Lock as Lock,
73+
Event as Event,
74+
Condition as Condition,
75+
Semaphore as Semaphore,
76+
BoundedSemaphore as BoundedSemaphore,
77+
)
78+
79+
# TODO: It should be possible to instantiate these classes, but mypy
80+
# currently disallows this.
81+
# See https://github.com/python/mypy/issues/1843
82+
SelectorEventLoop = ... # type: Type[AbstractEventLoop]
83+
if sys.platform == 'win32':
84+
ProactorEventLoop = ... # type: Type[AbstractEventLoop] # TODO: Windows only
85+
DefaultEventLoopPolicy = ... # type: Type[AbstractEventLoopPolicy]
86+
87+
# TODO: AbstractChildWatcher (UNIX only)
6188

6289
__all__ = ... # type: str

stdlib/3.4/asyncio/coroutines.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Callable, Any, TypeVar
1+
from typing import Any, Callable, Generator, TypeVar
22

33
__all__ = ... # type: str
44

5-
_T = TypeVar('_T')
5+
_F = TypeVar('_F', bound=Callable[..., Any])
66

7-
def coroutine(func: _T) -> _T: ...
7+
def coroutine(func: _F) -> _F: ...
88
def iscoroutinefunction(func: Callable[..., Any]) -> bool: ...
99
def iscoroutine(obj: Any) -> bool: ...

stdlib/3.4/asyncio/events.pyi

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Handle:
2525
class AbstractServer:
2626
def close(self) -> None: ...
2727
@coroutine
28-
def wait_closed(self) -> None: ...
28+
def wait_closed(self) -> Generator[Any, Any, None]: ...
2929

3030
class AbstractEventLoop(metaclass=ABCMeta):
3131
@abstractmethod
@@ -58,9 +58,9 @@ class AbstractEventLoop(metaclass=ABCMeta):
5858
# Network I/O methods returning Futures.
5959
@abstractmethod
6060
def getaddrinfo(self, host: str, port: int, *,
61-
family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> List[Tuple[int, int, int, str, tuple]]: ...
61+
family: int = ..., type: int = ..., proto: int = ..., flags: int = ...) -> Future[List[Tuple[int, int, int, str, tuple]]]: ...
6262
@abstractmethod
63-
def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Tuple[str, int]: ...
63+
def getnameinfo(self, sockaddr: tuple, flags: int = ...) -> Future[Tuple[str, int]]: ...
6464
@abstractmethod
6565
def create_connection(self, protocol_factory: Any, host: str = ..., port: int = ..., *,
6666
ssl: Any = ..., family: int = ..., proto: int = ..., flags: int = ..., sock: Any = ...,
@@ -152,18 +152,25 @@ class AbstractEventLoopPolicy(metaclass=ABCMeta):
152152
@abstractmethod
153153
def set_event_loop(self, loop: AbstractEventLoop): ...
154154
@abstractmethod
155-
def new_event_loop(self) -> Any: ... # return selector_events.BaseSelectorEventLoop
155+
def new_event_loop(self) -> AbstractEventLoop: ...
156156
# Child processes handling (Unix only).
157157
@abstractmethod
158-
def get_child_watcher(self) -> Any: ... # return unix_events.AbstractChildWatcher
158+
def get_child_watcher(self) -> Any: ... # TODO: unix_events.AbstractChildWatcher
159159
@abstractmethod
160-
def set_child_watcher(self, watcher: Any) -> None: ... # gen unix_events.AbstractChildWatcher
160+
def set_child_watcher(self, watcher: Any) -> None: ... # TODO: unix_events.AbstractChildWatcher
161161

162162
class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
163163
def __init__(self) -> None: ...
164164
def get_event_loop(self) -> AbstractEventLoop: ...
165165
def set_event_loop(self, loop: AbstractEventLoop): ...
166-
def new_event_loop(self) -> Any: ... # Same return than AbstractEventLoop
166+
def new_event_loop(self) -> AbstractEventLoop: ...
167167

168+
def get_event_loop_policy() -> AbstractEventLoopPolicy: ...
169+
def set_event_loop_policy(policy: AbstractEventLoopPolicy) -> None: ...
168170

169171
def get_event_loop() -> AbstractEventLoop: ...
172+
def set_event_loop(loop: AbstractEventLoop) -> None: ...
173+
def new_event_loop() -> AbstractEventLoop: ...
174+
175+
def get_child_watcher() -> Any: ... # TODO: unix_events.AbstractChildWatcher
176+
def set_child_watcher(watcher: Any) -> None: ... # TODO: unix_events.AbstractChildWatcher

stdlib/3.4/asyncio/locks.pyi

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Any, Callable, Generator, Iterable, Iterator, TypeVar, Union
2+
3+
from .coroutines import coroutine
4+
from .events import AbstractEventLoop
5+
from .futures import Future
6+
7+
T = TypeVar('T')
8+
9+
__all__ = ... # type: str
10+
11+
class _ContextManager:
12+
def __init__(self, lock: Union[Lock, Semaphore]) -> None: ...
13+
def __enter__(self) -> object: ...
14+
def __exit__(self, *args: Any) -> None: ...
15+
16+
class _ContextManagerMixin(Future[_ContextManager]):
17+
# Apparently this exists to *prohibit* use as a context manager.
18+
def __enter__(self) -> object: ...
19+
def __exit__(self, *args: Any) -> None: ...
20+
def __aenter__(self): ...
21+
def __aexit__(self, exc_type, exc, tb): ...
22+
23+
class Lock(_ContextManagerMixin):
24+
def __init__(self, *, loop: AbstractEventLoop = None) -> None: ...
25+
def locked(self) -> bool: ...
26+
@coroutine
27+
def acquire(self) -> Future[bool]: ...
28+
def release(self) -> None: ...
29+
30+
class Event:
31+
def __init__(self, *, loop: AbstractEventLoop = None) -> None: ...
32+
def is_set(self) -> bool: ...
33+
def set(self) -> None: ...
34+
def clear(self) -> None: ...
35+
36+
def wait(self) -> bool: ...
37+
38+
class Condition(_ContextManagerMixin):
39+
def __init__(self, lock: Lock = None, *, loop: AbstractEventLoop = None) -> None: ...
40+
def locked(self) -> bool: ...
41+
@coroutine
42+
def acquire(self) -> Future[bool]: ...
43+
def release(self) -> None: ...
44+
@coroutine
45+
def wait(self) -> Future[bool]: ...
46+
@coroutine
47+
def wait_for(self, predicate: Callable[[], T]) -> Future[T]: ...
48+
def notify(self, n: int = 1) -> None: ...
49+
def notify_all(self) -> None: ...
50+
51+
class Semaphore(_ContextManagerMixin):
52+
def __init__(self, value: int = 1, *, loop: AbstractEventLoop = None) -> None: ...
53+
def locked(self) -> bool: ...
54+
@coroutine
55+
def acquire(self) -> Future[bool]: ...
56+
def release(self) -> None: ...
57+
58+
class BoundedSemaphore(Semaphore):
59+
def __init__(self, value=1, *, loop: AbstractEventLoop = None) -> None: ...

stdlib/3.4/asyncio/protocols.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BaseProtocol:
1111
def resume_writing(self) -> None: ...
1212

1313
class Protocol(BaseProtocol):
14-
def data_received(self, data: AnyStr) -> None: ...
14+
def data_received(self, data: bytes) -> None: ...
1515
def eof_received(self) -> bool: ...
1616

1717
class DatagramProtocol(BaseProtocol):

stdlib/3.4/asyncio/streams.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Iterable, Tuple, Callable, Any, AnyStr
1+
from typing import Any, Callable, Generator, Iterable, Tuple
22

33
ClientConnectedCallback = Callable[[Tuple[StreamReader, StreamWriter]], None]
44
import socket
@@ -23,7 +23,7 @@ def open_connection(
2323
*,
2424
loop: events.AbstractEventLoop = ...,
2525
limit: int = ...,
26-
**kwds: Any) -> Tuple[StreamReader, StreamWriter]: ...
26+
**kwds: Any) -> Generator[Any, None, Tuple[StreamReader, StreamWriter]]: ...
2727

2828
@coroutines.coroutine
2929
def start_server(
@@ -62,7 +62,7 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
6262
loop: events.AbstractEventLoop = ...) -> None: ...
6363
def connection_made(self, transport: transports.BaseTransport) -> None: ...
6464
def connection_lost(self, exc: Exception) -> None: ...
65-
def data_received(self, data: AnyStr) -> None: ...
65+
def data_received(self, data: bytes) -> None: ...
6666
def eof_received(self) -> bool: ...
6767

6868
class StreamWriter:
@@ -73,12 +73,12 @@ class StreamWriter:
7373
loop: events.AbstractEventLoop) -> None: ...
7474
@property
7575
def transport(self) -> transports.BaseTransport: ...
76-
def write(self, data: AnyStr) -> None: ...
77-
def writelines(self, data: Iterable[str]) -> None: ...
76+
def write(self, data: bytes) -> None: ...
77+
def writelines(self, data: Iterable[bytes]) -> None: ...
7878
def write_eof(self) -> None: ...
7979
def can_write_eof(self) -> bool: ...
8080
def close(self) -> None: ...
81-
def get_extra_info(self, name: Any, default: Any = ...) -> Any: ...
81+
def get_extra_info(self, name: str, default: Any = ...) -> Any: ...
8282
def drain(self) -> None: ...
8383

8484
class StreamReader:
@@ -90,12 +90,12 @@ class StreamReader:
9090
def set_transport(self, transport: transports.BaseTransport) -> None: ...
9191
def feed_eof(self) -> None: ...
9292
def at_eof(self) -> bool: ...
93-
def feed_data(self, data: AnyStr): ...
93+
def feed_data(self, data: bytes): ...
9494
@coroutines.coroutine
95-
def readline(self) -> str: ...
95+
def readline(self) -> Generator[Any, None, bytes]: ...
9696
@coroutines.coroutine
97-
def readuntil(self, separator=b'\n') -> str: ...
97+
def readuntil(self, separator=b'\n') -> Generator[Any, None, bytes]: ...
9898
@coroutines.coroutine
99-
def read(self, n=-1) -> str: ...
99+
def read(self, n=-1) -> Generator[Any, None, bytes]: ...
100100
@coroutines.coroutine
101-
def readexactly(self, n) -> str: ...
101+
def readexactly(self, n) -> Generator[Any, None, bytes]: ...

stdlib/3.4/asyncio/tasks.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Iterable, TypeVar, Set, Dict, List, TextIO, Union, Tuple, Generic, Callable, Generator
1+
from typing import Any, TypeVar, Set, Dict, List, TextIO, Union, Tuple, Generic, Callable, Generator, Iterable, Awaitable
22

33
__all__ = ... # type: str
44

@@ -22,7 +22,7 @@ class Task(Future[_T], Generic[_T]):
2222
def current_task(cls, loop: AbstractEventLoop = ...) -> Task: ...
2323
@classmethod
2424
def all_tasks(cls, loop: AbstractEventLoop = ...) -> Set[Task]: ...
25-
def __init__(self, coro: Union[Future[_T], Generator[Any, None, _T]], *, loop: AbstractEventLoop = ...) -> None: ...
25+
def __init__(self, coro: Union[Awaitable[_T], Iterable[_T], Future[_T], Generator[Any, None, _T]], *, loop: AbstractEventLoop = ...) -> None: ...
2626
def __repr__(self) -> str: ...
2727
def get_stack(self, *, limit: int = ...) -> List[Any]: ... # return List[stackframe]
2828
def print_stack(self, *, limit: int = ..., file: TextIO = ...) -> None: ...

0 commit comments

Comments
 (0)