From fc7eeb8590f972c9899ae48a060c229b5f167f7c Mon Sep 17 00:00:00 2001 From: Joshua Oreman Date: Thu, 30 Nov 2023 18:07:03 -0700 Subject: [PATCH] Placate black and stubtest --- async_generator-stubs/__init__.pyi | 1 - trio-stubs/__init__.pyi | 103 +++++++++++++++++++++++------ trio-stubs/abc.pyi | 6 +- trio-stubs/from_thread.pyi | 1 + trio-stubs/lowlevel.pyi | 60 ++++++++++++++--- trio-stubs/socket.pyi | 12 ++-- 6 files changed, 146 insertions(+), 37 deletions(-) diff --git a/async_generator-stubs/__init__.pyi b/async_generator-stubs/__init__.pyi index 4fe6c95..f7d1566 100644 --- a/async_generator-stubs/__init__.pyi +++ b/async_generator-stubs/__init__.pyi @@ -40,7 +40,6 @@ async def yield_from_(agen: AsyncGenerator[Any, Any]) -> None: ... async def yield_from_(agen: AsyncIterable[Any]) -> None: ... def isasyncgen(obj: object) -> bool: ... def isasyncgenfunction(obj: object) -> bool: ... - def asynccontextmanager( fn: Callable[_P, AsyncIterator[_T]] ) -> Callable[_P, AsyncContextManager[_T]]: ... diff --git a/trio-stubs/__init__.pyi b/trio-stubs/__init__.pyi index ecc04e9..c764744 100644 --- a/trio-stubs/__init__.pyi +++ b/trio-stubs/__init__.pyi @@ -30,7 +30,7 @@ from types import TracebackType from _typeshed import StrOrBytesPath from _typeshed import OpenBinaryMode, OpenTextMode, ReadableBuffer, WriteableBuffer from trio_typing import TaskStatus, takes_callable_and_args -from typing_extensions import Protocol, Literal +from typing_extensions import Protocol, Literal, Buffer from mypy_extensions import NamedArg, VarArg import signal import io @@ -48,9 +48,6 @@ _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) -class _Statistics: - def __getattr__(self, name: str) -> Any: ... - # Inheriting from this (even outside of stubs) produces a class that # mypy thinks is abstract, but the interpreter thinks is concrete. class _NotConstructible(Protocol): @@ -208,13 +205,24 @@ class TooSlowError(Exception): pass # _sync +@attr.s(frozen=True, slots=True) +class EventStatistics: + tasks_waiting: int = attr.ib() + @final @attr.s(eq=False, repr=False, slots=True) class Event(metaclass=ABCMeta): def is_set(self) -> bool: ... def set(self) -> None: ... async def wait(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> EventStatistics: ... + +@attr.s(frozen=True, slots=True) +class CapacityLimiterStatistics: + borrowed_tokens: int = attr.ib() + total_tokens: int | float = attr.ib() + borrowers: list[Task | object] = attr.ib() + tasks_waiting: int = attr.ib() @final class CapacityLimiter(metaclass=ABCMeta): @@ -232,9 +240,14 @@ class CapacityLimiter(metaclass=ABCMeta): async def acquire_on_behalf_of(self, borrower: object) -> None: ... def release(self) -> None: ... def release_on_behalf_of(self, borrower: object) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> CapacityLimiterStatistics: ... async def __aenter__(self) -> None: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... @final class Semaphore(metaclass=ABCMeta): @@ -246,9 +259,20 @@ class Semaphore(metaclass=ABCMeta): def acquire_nowait(self) -> None: ... async def acquire(self) -> None: ... def release(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> lowlevel.ParkingLotStatistics: ... async def __aenter__(self) -> None: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... + +@attr.s(frozen=True, slots=True) +class LockStatistics: + locked: bool = attr.ib() + owner: Task | None = attr.ib() + tasks_waiting: int = attr.ib() @final class Lock(metaclass=ABCMeta): @@ -256,9 +280,14 @@ class Lock(metaclass=ABCMeta): def acquire_nowait(self) -> None: ... async def acquire(self) -> None: ... def release(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> LockStatistics: ... async def __aenter__(self) -> None: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... @final class StrictFIFOLock(metaclass=ABCMeta): @@ -266,9 +295,19 @@ class StrictFIFOLock(metaclass=ABCMeta): def acquire_nowait(self) -> None: ... async def acquire(self) -> None: ... def release(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> LockStatistics: ... async def __aenter__(self) -> None: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... + +@attr.s(frozen=True, slots=True) +class ConditionStatistics: + tasks_waiting: int = attr.ib() + lock_statistics: LockStatistics = attr.ib() @final class Condition(metaclass=ABCMeta): @@ -280,9 +319,14 @@ class Condition(metaclass=ABCMeta): async def wait(self) -> None: ... def notify(self, n: int = 1) -> None: ... def notify_all(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> ConditionStatistics: ... async def __aenter__(self) -> None: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... # _highlevel_generic async def aclose_forcefully(resource: trio.abc.AsyncResource) -> None: ... @@ -298,6 +342,15 @@ class StapledStream(trio.abc.HalfCloseableStream): async def send_eof(self) -> None: ... # _channel +@attr.s(frozen=True, slots=True) +class _MemoryChannelStats: + current_buffer_used: int = attr.ib() + max_buffer_size: int | float = attr.ib() + open_send_channels: int = attr.ib() + open_receive_channels: int = attr.ib() + tasks_waiting_send: int = attr.ib() + tasks_waiting_receive: int = attr.ib() + @final @attr.s(eq=False, repr=False) class MemorySendChannel(trio.abc.SendChannel[_T_contra]): @@ -305,7 +358,7 @@ class MemorySendChannel(trio.abc.SendChannel[_T_contra]): async def send(self, value: _T_contra) -> None: ... def clone(self: _T) -> _T: ... async def aclose(self) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> _MemoryChannelStats: ... def close(self) -> None: ... def __enter__(self) -> MemorySendChannel[_T_contra]: ... def __exit__( @@ -349,7 +402,10 @@ def open_signal_receiver( class SocketStream(trio.abc.HalfCloseableStream): socket: trio.socket.SocketType def __init__(self, socket: trio.socket.SocketType) -> None: ... - def setsockopt(self, level: int, option: int, value: Union[int, bytes]) -> None: ... + @overload + def setsockopt(self, level: int, option: int, value: int | Buffer) -> None: ... + @overload + def setsockopt(self, level: int, option: int, value: None, length: int) -> None: ... @overload def getsockopt(self, level: int, option: int) -> int: ... @overload @@ -400,6 +456,10 @@ class DTLSEndpoint(metaclass=ABCMeta): exc_tb: TracebackType | None, ) -> None: ... +@attr.frozen +class DTLSChannelStatistics: + incoming_packets_dropped_in_trio: int + @final class DTLSChannel(_NotConstructible, trio.abc.Channel[bytes], metaclass=ABCMeta): endpoint: DTLSEndpoint @@ -414,7 +474,7 @@ class DTLSChannel(_NotConstructible, trio.abc.Channel[bytes], metaclass=ABCMeta) def statistics(self) -> Any: ... async def aclose(self) -> None: ... def close(self) -> None: ... - def __enter__(self) -> DTLSChannel: ... + def __enter__(self) -> DTLSChannelStatistics: ... def __exit__( self, exc_type: type[BaseException] | None, @@ -452,7 +512,12 @@ class AsyncIO(AsyncIterator[AnyStr], Generic[AnyStr], trio.abc.AsyncResource): async def __anext__(self) -> AnyStr: ... def __aiter__(self) -> AsyncIterator[AnyStr]: ... async def __aenter__(self: _T) -> _T: ... - async def __aexit__(self, *exc: object) -> None: ... + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: ... class AsyncBinaryIO(AsyncIO[bytes]): pass diff --git a/trio-stubs/abc.pyi b/trio-stubs/abc.pyi index 10d2eae..79b7be7 100644 --- a/trio-stubs/abc.pyi +++ b/trio-stubs/abc.pyi @@ -43,9 +43,9 @@ class SocketFactory(metaclass=ABCMeta): @abstractmethod def socket( self, - family: Optional[int] = None, - type: Optional[int] = None, - proto: Optional[int] = None, + family: socket.AddressFamily | int = ..., + type: socket.SocketKind | int = ..., + proto: int = ..., ) -> trio.socket.SocketType: ... class AsyncResource(metaclass=ABCMeta): diff --git a/trio-stubs/from_thread.pyi b/trio-stubs/from_thread.pyi index 4995b18..b8923d0 100644 --- a/trio-stubs/from_thread.pyi +++ b/trio-stubs/from_thread.pyi @@ -17,3 +17,4 @@ def run_sync( *args: Any, trio_token: Optional[trio.lowlevel.TrioToken] = ..., ) -> _T: ... +def check_cancelled() -> None: ... diff --git a/trio-stubs/lowlevel.pyi b/trio-stubs/lowlevel.pyi index b6d8955..2ba4c3a 100644 --- a/trio-stubs/lowlevel.pyi +++ b/trio-stubs/lowlevel.pyi @@ -37,9 +37,6 @@ import sys _T = TypeVar("_T") _F = TypeVar("_F", bound=Callable[..., Any]) -class _Statistics: - def __getattr__(self, name: str) -> Any: ... - # _core._ki def enable_ki_protection(fn: _F) -> _F: ... def disable_ki_protection(fn: _F) -> _F: ... @@ -58,6 +55,11 @@ class TrioToken(metaclass=ABCMeta): ) -> None: ... # _core._unbounded_queue +@attr.s(slots=True, frozen=True) +class UnboundedQueueStatistics: + qsize: int = attr.ib() + tasks_waiting: int = attr.ib() + @final class UnboundedQueue(Generic[_T], metaclass=ABCMeta): def __init__(self) -> None: ... @@ -66,11 +68,42 @@ class UnboundedQueue(Generic[_T], metaclass=ABCMeta): def put_nowait(self, obj: _T) -> None: ... def get_batch_nowait(self) -> Sequence[_T]: ... async def get_batch(self) -> Sequence[_T]: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> UnboundedQueueStatistics: ... def __aiter__(self) -> AsyncIterator[Sequence[_T]]: ... async def __anext__(self) -> Sequence[_T]: ... # _core._run +if sys.platform == "win32": + @attr.frozen + class IOStatistics: + tasks_waiting_read: int = attr.ib() + tasks_waiting_write: int = attr.ib() + tasks_waiting_overlapped: int = attr.ib() + completion_key_monitors: int = attr.ib() + backend: Literal["windows"] = attr.ib(init=False, default="windows") + +elif sys.platform == "linux": + @attr.frozen + class IOStatistics: + tasks_waiting_read: int = attr.ib() + tasks_waiting_write: int = attr.ib() + backend: Literal["epoll"] = attr.ib(init=False, default="epoll") + +else: # kqueue + @attr.frozen + class IOStatistics: + tasks_waiting: int = attr.ib() + monitors: int = attr.ib() + backend: Literal["kqueue"] = attr.ib(init=False, default="kqueue") + +@attr.frozen +class RunStatistics: + tasks_living: int + tasks_runnable: int + seconds_to_next_deadline: float + io_statistics: IOStatistics + run_sync_soon_queue_size: int + @final @attr.s(eq=False, hash=False, repr=False, slots=True) class Task(metaclass=ABCMeta): @@ -90,7 +123,7 @@ async def checkpoint() -> None: ... async def checkpoint_if_cancelled() -> None: ... def current_task() -> Task: ... def current_root_task() -> Task: ... -def current_statistics() -> _Statistics: ... +def current_statistics() -> RunStatistics: ... def current_clock() -> trio.abc.Clock: ... def current_trio_token() -> TrioToken: ... def reschedule(task: Task, next_send: outcome.Outcome[Any] = ...) -> None: ... @@ -161,6 +194,10 @@ async def temporarily_detach_coroutine_object( async def reattach_detached_coroutine_object(task: Task, yield_value: Any) -> None: ... # _core._parking_lot +@attr.s(frozen=True, slots=True) +class ParkingLotStatistics: + tasks_waiting: int = attr.ib() + @final @attr.s(eq=False, hash=False, slots=True) class ParkingLot(metaclass=ABCMeta): @@ -171,11 +208,14 @@ class ParkingLot(metaclass=ABCMeta): def unpark_all(self) -> Sequence[Task]: ... def repark(self, new_lot: ParkingLot, *, count: int = 1) -> None: ... def repark_all(self, new_lot: ParkingLot) -> None: ... - def statistics(self) -> _Statistics: ... + def statistics(self) -> ParkingLotStatistics: ... # _core._local -class _RunVarToken: - pass +class _NoValue: ... + +class RunVarToken(Generic[_T]): + previous_value: T | type[_NoValue] + redeemed: bool @final @attr.s(eq=False, hash=False, slots=True) @@ -183,8 +223,8 @@ class RunVar(Generic[_T], metaclass=ABCMeta): _name: str = attr.ib() _default: _T = attr.ib(default=cast(_T, object())) def get(self, default: _T = ...) -> _T: ... - def set(self, value: _T) -> _RunVarToken: ... - def reset(self, token: _RunVarToken) -> None: ... + def set(self, value: _T) -> RunVarToken[_T]: ... + def reset(self, token: RunVarToken[_T]) -> None: ... # _core._thread_cache def start_thread_soon( diff --git a/trio-stubs/socket.pyi b/trio-stubs/socket.pyi index fa0c001..6ac85f9 100644 --- a/trio-stubs/socket.pyi +++ b/trio-stubs/socket.pyi @@ -402,12 +402,16 @@ async def getaddrinfo( ]: ... class SocketType: - family: int - type: int - proto: int - did_shutdown_SHUT_WR: bool def __enter__(self: _T) -> _T: ... def __exit__(self, *args: Any) -> None: ... + @property + def did_shutdown_SHUT_WR(self) -> bool: ... + @property + def family(self) -> int: ... + @property + def type(self) -> int: ... + @property + def proto(self) -> int: ... def dup(self) -> SocketType: ... def close(self) -> None: ... async def bind(self, address: Union[Tuple[Any, ...], str, bytes]) -> None: ...