-
-
Notifications
You must be signed in to change notification settings - Fork 392
lots of typing improvements #2682
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
Changes from all commits
af601ac
0050ca2
8b8383b
b1f9f6f
ba64565
adb8782
3c9583c
0f29a1a
c2012a7
3628fc6
9a0c3e2
3094d06
f83373b
ac4b357
8f89519
77a3706
50ce72f
f34787b
1265024
a8751bb
ed05e13
67166a5
8e5ba0b
89f308f
2536843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,15 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from abc import ABCMeta, abstractmethod | ||
| from typing import Generic, TypeVar | ||
| from typing import TYPE_CHECKING, Generic, TypeVar | ||
|
|
||
| import trio | ||
|
|
||
| if TYPE_CHECKING: | ||
| from types import TracebackType | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
|
|
||
| # We use ABCMeta instead of ABC, plus set __slots__=(), so as not to force a | ||
| # __dict__ onto subclasses. | ||
|
|
@@ -12,15 +19,15 @@ class Clock(metaclass=ABCMeta): | |
| __slots__ = () | ||
|
|
||
| @abstractmethod | ||
| def start_clock(self): | ||
| def start_clock(self) -> None: | ||
| """Do any setup this clock might need. | ||
|
|
||
| Called at the beginning of the run. | ||
|
|
||
| """ | ||
|
|
||
| @abstractmethod | ||
| def current_time(self): | ||
| def current_time(self) -> float: | ||
| """Return the current time, according to this clock. | ||
|
|
||
| This is used to implement functions like :func:`trio.current_time` and | ||
|
|
@@ -32,7 +39,7 @@ def current_time(self): | |
| """ | ||
|
|
||
| @abstractmethod | ||
| def deadline_to_sleep_time(self, deadline): | ||
| def deadline_to_sleep_time(self, deadline: float) -> float: | ||
| """Compute the real time until the given deadline. | ||
|
|
||
| This is called before we enter a system-specific wait function like | ||
|
|
@@ -225,7 +232,7 @@ class AsyncResource(metaclass=ABCMeta): | |
| __slots__ = () | ||
|
|
||
| @abstractmethod | ||
| async def aclose(self): | ||
| async def aclose(self) -> None: | ||
| """Close this resource, possibly blocking. | ||
|
|
||
| IMPORTANT: This method may block in order to perform a "graceful" | ||
|
|
@@ -253,10 +260,15 @@ async def aclose(self): | |
|
|
||
| """ | ||
|
|
||
| async def __aenter__(self): | ||
| async def __aenter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __aexit__(self, *args): | ||
| async def __aexit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc_value: BaseException | None, | ||
| traceback: TracebackType | None, | ||
| ) -> None: | ||
| await self.aclose() | ||
|
|
||
|
|
||
|
|
@@ -279,7 +291,7 @@ class SendStream(AsyncResource): | |
| __slots__ = () | ||
|
|
||
| @abstractmethod | ||
| async def send_all(self, data): | ||
| async def send_all(self, data: bytes | bytearray | memoryview) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're very welcome to open it for discussion! I'm not aware of the nuances here at all, just copy-pasting types. |
||
| """Sends the given data through the stream, blocking if necessary. | ||
|
|
||
| Args: | ||
|
|
@@ -305,7 +317,7 @@ async def send_all(self, data): | |
| """ | ||
|
|
||
| @abstractmethod | ||
| async def wait_send_all_might_not_block(self): | ||
| async def wait_send_all_might_not_block(self) -> None: | ||
| """Block until it's possible that :meth:`send_all` might not block. | ||
|
|
||
| This method may return early: it's possible that after it returns, | ||
|
|
@@ -385,7 +397,7 @@ class ReceiveStream(AsyncResource): | |
| __slots__ = () | ||
|
|
||
| @abstractmethod | ||
| async def receive_some(self, max_bytes=None): | ||
| async def receive_some(self, max_bytes: int | None = None) -> bytes | bytearray: | ||
A5rocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Wait until there is data available on this stream, and then return | ||
| some of it. | ||
|
|
||
|
|
@@ -413,10 +425,10 @@ async def receive_some(self, max_bytes=None): | |
|
|
||
| """ | ||
|
|
||
| def __aiter__(self): | ||
| def __aiter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __anext__(self): | ||
| async def __anext__(self) -> bytes | bytearray: | ||
| data = await self.receive_some() | ||
| if not data: | ||
| raise StopAsyncIteration | ||
|
|
@@ -446,7 +458,7 @@ class HalfCloseableStream(Stream): | |
| __slots__ = () | ||
|
|
||
| @abstractmethod | ||
| async def send_eof(self): | ||
| async def send_eof(self) -> None: | ||
| """Send an end-of-file indication on this stream, if possible. | ||
|
|
||
| The difference between :meth:`send_eof` and | ||
|
|
@@ -632,7 +644,7 @@ async def receive(self) -> ReceiveType: | |
|
|
||
| """ | ||
|
|
||
| def __aiter__(self): | ||
| def __aiter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __anext__(self) -> ReceiveType: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of documenting the class itself here, it might be possible to just put an
indexdirective in thestatistics()method, so the name links back there. That way we don't have the same information repeated twice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After trying a bunch of different variants I can't get
indexto create aclass-type reference. I can create an index namedtrio.EventStatisticsthat I can link to with:ref:- but I failed to create one I can link to with:class:which is what autodoc generates the docstring to do.