Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PEP-646 in stubs for asyncio #11015

Merged
merged 10 commits into from
Dec 8, 2023
23 changes: 14 additions & 9 deletions stdlib/asyncio/base_events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ from collections.abc import Callable, Iterable, Sequence
from contextvars import Context
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, TypeVar, overload
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal, TypeAlias, TypeVarTuple, Unpack

if sys.version_info >= (3, 9):
__all__ = ("BaseEventLoop", "Server")
else:
__all__ = ("BaseEventLoop",)

_T = TypeVar("_T")
_Ts = TypeVarTuple("_Ts")
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
_Context: TypeAlias = dict[str, Any]
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], object]
Expand Down Expand Up @@ -71,12 +72,14 @@ class BaseEventLoop(AbstractEventLoop):
def close(self) -> None: ...
async def shutdown_asyncgens(self) -> None: ...
# Methods scheduling callbacks. All these return Handles.
def call_soon(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
def call_soon(
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> Handle: ...
def call_later(
self, delay: float, callback: Callable[..., object], *args: Any, context: Context | None = None
self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> TimerHandle: ...
def call_at(
self, when: float, callback: Callable[..., object], *args: Any, context: Context | None = None
self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> TimerHandle: ...
def time(self) -> float: ...
# Future methods
Expand All @@ -92,8 +95,10 @@ class BaseEventLoop(AbstractEventLoop):
def set_task_factory(self, factory: _TaskFactory | None) -> None: ...
def get_task_factory(self) -> _TaskFactory | None: ...
# Methods for interacting with threads
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
def run_in_executor(self, executor: Any, func: Callable[..., _T], *args: Any) -> Future[_T]: ...
def call_soon_threadsafe(
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> Handle: ...
def run_in_executor(self, executor: Any, func: Callable[[Unpack[_Ts]], _T], *args: Unpack[_Ts]) -> Future[_T]: ...
def set_default_executor(self, executor: Any) -> None: ...
# Network I/O methods returning Futures.
async def getaddrinfo(
Expand Down Expand Up @@ -441,9 +446,9 @@ class BaseEventLoop(AbstractEventLoop):
errors: None = None,
**kwargs: Any,
) -> tuple[SubprocessTransport, _ProtocolT]: ...
def add_reader(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
def add_reader(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
def remove_reader(self, fd: FileDescriptorLike) -> bool: ...
def add_writer(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
def add_writer(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
def remove_writer(self, fd: FileDescriptorLike) -> bool: ...
# The sock_* methods (and probably some others) are not actually implemented on
# BaseEventLoop, only on subclasses. We list them here for now for convenience.
Expand All @@ -457,7 +462,7 @@ class BaseEventLoop(AbstractEventLoop):
async def sock_recvfrom_into(self, sock: socket, buf: WriteableBuffer, nbytes: int = 0) -> tuple[int, _RetAddress]: ...
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> int: ...
# Signal handling.
def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: Any) -> None: ...
def add_signal_handler(self, sig: int, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
def remove_signal_handler(self, sig: int) -> bool: ...
# Error handlers.
def set_exception_handler(self, handler: _ExceptionHandler | None) -> None: ...
Expand Down
43 changes: 26 additions & 17 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from collections.abc import Callable, Coroutine, Generator, Sequence
from contextvars import Context
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Protocol, TypeVar, overload
from typing_extensions import Literal, Self, TypeAlias
from typing_extensions import Literal, Self, TypeAlias, TypeVarTuple, Unpack

from . import _AwaitableLike, _CoroutineLike
from .base_events import Server
Expand Down Expand Up @@ -56,6 +56,7 @@ else:
)

_T = TypeVar("_T")
_Ts = TypeVarTuple("_Ts")
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
_Context: TypeAlias = dict[str, Any]
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], object]
Expand All @@ -69,9 +70,13 @@ class _TaskFactory(Protocol):

class Handle:
_cancelled: bool
_args: Sequence[Any]
_args: tuple[Any, ...]
def __init__(
self, callback: Callable[..., object], args: Sequence[Any], loop: AbstractEventLoop, context: Context | None = None
self,
callback: Callable[[Unpack[_Ts]], object],
args: tuple[Unpack[_Ts]],
loop: AbstractEventLoop,
context: Context | None = None,
) -> None: ...
def cancel(self) -> None: ...
def _run(self) -> None: ...
Expand All @@ -83,8 +88,8 @@ class TimerHandle(Handle):
def __init__(
self,
when: float,
callback: Callable[..., object],
args: Sequence[Any],
callback: Callable[[Unpack[_Ts]], object],
args: tuple[Unpack[_Ts]],
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
loop: AbstractEventLoop,
context: Context | None = None,
) -> None: ...
Expand Down Expand Up @@ -131,22 +136,24 @@ class AbstractEventLoop:
# Methods scheduling callbacks. All these return Handles.
if sys.version_info >= (3, 9): # "context" added in 3.9.10/3.10.2
@abstractmethod
def call_soon(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
def call_soon(
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> Handle: ...
@abstractmethod
def call_later(
self, delay: float, callback: Callable[..., object], *args: Any, context: Context | None = None
self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> TimerHandle: ...
@abstractmethod
def call_at(
self, when: float, callback: Callable[..., object], *args: Any, context: Context | None = None
self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> TimerHandle: ...
else:
@abstractmethod
def call_soon(self, callback: Callable[..., object], *args: Any) -> Handle: ...
def call_soon(self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> Handle: ...
@abstractmethod
def call_later(self, delay: float, callback: Callable[..., object], *args: Any) -> TimerHandle: ...
def call_later(self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> TimerHandle: ...
@abstractmethod
def call_at(self, when: float, callback: Callable[..., object], *args: Any) -> TimerHandle: ...
def call_at(self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> TimerHandle: ...

@abstractmethod
def time(self) -> float: ...
Expand All @@ -173,13 +180,15 @@ class AbstractEventLoop:
# Methods for interacting with threads
if sys.version_info >= (3, 9): # "context" added in 3.9.10/3.10.2
@abstractmethod
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
def call_soon_threadsafe(
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
) -> Handle: ...
else:
@abstractmethod
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any) -> Handle: ...
def call_soon_threadsafe(self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> Handle: ...

@abstractmethod
def run_in_executor(self, executor: Any, func: Callable[..., _T], *args: Any) -> Future[_T]: ...
def run_in_executor(self, executor: Any, func: Callable[[Unpack[_Ts]], _T], *args: Unpack[_Ts]) -> Future[_T]: ...
@abstractmethod
def set_default_executor(self, executor: Any) -> None: ...
# Network I/O methods returning Futures.
Expand Down Expand Up @@ -542,11 +551,11 @@ class AbstractEventLoop:
**kwargs: Any,
) -> tuple[SubprocessTransport, _ProtocolT]: ...
@abstractmethod
def add_reader(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
def add_reader(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
@abstractmethod
def remove_reader(self, fd: FileDescriptorLike) -> bool: ...
@abstractmethod
def add_writer(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
def add_writer(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
@abstractmethod
def remove_writer(self, fd: FileDescriptorLike) -> bool: ...
# Completion based I/O methods returning Futures prior to 3.7
Expand All @@ -569,7 +578,7 @@ class AbstractEventLoop:
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> int: ...
# Signal handling.
@abstractmethod
def add_signal_handler(self, sig: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_signal_handler(self, sig: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
@abstractmethod
def remove_signal_handler(self, sig: int) -> bool: ...
# Error handlers.
Expand Down
17 changes: 9 additions & 8 deletions stdlib/asyncio/unix_events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import sys
import types
from abc import ABCMeta, abstractmethod
from collections.abc import Callable
from typing import Any
from typing_extensions import Literal, Self
from typing_extensions import Literal, Self, TypeVarTuple, Unpack

from .events import AbstractEventLoop, BaseDefaultEventLoopPolicy
from .selector_events import BaseSelectorEventLoop

_Ts = TypeVarTuple("_Ts")

# This is also technically not available on Win,
# but other parts of typeshed need this definition.
# So, it is special cased.
class AbstractChildWatcher:
@abstractmethod
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
@abstractmethod
def remove_child_handler(self, pid: int) -> bool: ...
@abstractmethod
Expand Down Expand Up @@ -65,13 +66,13 @@ if sys.platform != "win32":
class SafeChildWatcher(BaseChildWatcher):
def __enter__(self) -> Self: ...
def __exit__(self, a: type[BaseException] | None, b: BaseException | None, c: types.TracebackType | None) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...

class FastChildWatcher(BaseChildWatcher):
def __enter__(self) -> Self: ...
def __exit__(self, a: type[BaseException] | None, b: BaseException | None, c: types.TracebackType | None) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...

class _UnixSelectorEventLoop(BaseSelectorEventLoop): ...
Expand All @@ -91,7 +92,7 @@ if sys.platform != "win32":
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: types.TracebackType | None
) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...
def attach_loop(self, loop: AbstractEventLoop | None) -> None: ...

Expand All @@ -103,7 +104,7 @@ if sys.platform != "win32":
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: types.TracebackType | None
) -> None: ...
def __del__(self) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...
def attach_loop(self, loop: AbstractEventLoop | None) -> None: ...

Expand All @@ -116,5 +117,5 @@ if sys.platform != "win32":
def is_active(self) -> bool: ...
def close(self) -> None: ...
def attach_loop(self, loop: AbstractEventLoop | None) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[..., object], *args: Any) -> None: ...
def add_child_handler(self, pid: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...
Loading