Skip to content

Commit

Permalink
Placate black and stubtest
Browse files Browse the repository at this point in the history
  • Loading branch information
oremanj committed Dec 1, 2023
1 parent 98f5051 commit fc7eeb8
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 37 deletions.
1 change: 0 additions & 1 deletion async_generator-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]: ...
Expand Down
103 changes: 84 additions & 19 deletions trio-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -246,29 +259,55 @@ 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):
def locked(self) -> bool: ...
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):
def locked(self) -> bool: ...
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):
Expand All @@ -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: ...
Expand All @@ -298,14 +342,23 @@ 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]):
def send_nowait(self, value: _T_contra) -> None: ...
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__(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions trio-stubs/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions trio-stubs/from_thread.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ def run_sync(
*args: Any,
trio_token: Optional[trio.lowlevel.TrioToken] = ...,
) -> _T: ...
def check_cancelled() -> None: ...
60 changes: 50 additions & 10 deletions trio-stubs/lowlevel.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand All @@ -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):
Expand All @@ -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: ...
Expand Down Expand Up @@ -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):
Expand All @@ -171,20 +208,23 @@ 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)
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(
Expand Down
12 changes: 8 additions & 4 deletions trio-stubs/socket.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down

0 comments on commit fc7eeb8

Please sign in to comment.