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

Add documentation for statistics() and aclose() on memory channels #3101

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/reference-core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,8 @@ more features beyond the core channel interface:
.. autoclass:: MemoryReceiveChannel
:members:

.. autoclass:: MemoryChannelStatistics
:members:

A simple channel example
++++++++++++++++++++++++
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3101.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add docstrings for memory channels' ``statistics()`` and ``aclose`` methods.
1 change: 1 addition & 0 deletions src/trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Submodules imported by default
from . import abc, from_thread, lowlevel, socket, to_thread
from ._channel import (
MemoryChannelStatistics as MemoryChannelStatistics,
MemoryReceiveChannel as MemoryReceiveChannel,
MemorySendChannel as MemorySendChannel,
open_memory_channel as open_memory_channel,
Expand Down
20 changes: 15 additions & 5 deletions src/trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self, max_buffer_size: int | float): # noqa: PYI041


@attrs.frozen
class MemoryChannelStats:
class MemoryChannelStatistics:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is backwards compatible as far as we care.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it wasn't publicaly re-exported originally, I would think this should count as backwards compatible, and if anyone does have issues we can tell them its their fault for digging into Trio's internals.

current_buffer_used: int
max_buffer_size: int | float
open_send_channels: int
Expand All @@ -132,8 +132,8 @@ class MemoryChannelState(Generic[T]):
# {task: None}
receive_tasks: OrderedDict[Task, None] = attrs.Factory(OrderedDict)

def statistics(self) -> MemoryChannelStats:
return MemoryChannelStats(
def statistics(self) -> MemoryChannelStatistics:
return MemoryChannelStatistics(
current_buffer_used=len(self.data),
max_buffer_size=self.max_buffer_size,
open_send_channels=self.open_send_channels,
Expand All @@ -159,7 +159,9 @@ def __attrs_post_init__(self) -> None:
def __repr__(self) -> str:
return f"<send channel at {id(self):#x}, using buffer at {id(self._state):#x}>"

def statistics(self) -> MemoryChannelStats:
def statistics(self) -> MemoryChannelStatistics:
"""Returns a `MemoryChannelStatistics` for the memory channel this is
associated with."""
# XX should we also report statistics specific to this object?
return self._state.statistics()

Expand Down Expand Up @@ -282,6 +284,9 @@ def close(self) -> None:

@enable_ki_protection
async def aclose(self) -> None:
"""Close this send channel object asynchronously.

See `MemorySendChannel.close`."""
self.close()
await trio.lowlevel.checkpoint()

Expand All @@ -296,7 +301,9 @@ class MemoryReceiveChannel(ReceiveChannel[ReceiveType], metaclass=NoPublicConstr
def __attrs_post_init__(self) -> None:
self._state.open_receive_channels += 1

def statistics(self) -> MemoryChannelStats:
def statistics(self) -> MemoryChannelStatistics:
"""Returns a `MemoryChannelStatistics` for the memory channel this is
associated with."""
return self._state.statistics()

def __repr__(self) -> str:
Expand Down Expand Up @@ -430,5 +437,8 @@ def close(self) -> None:

@enable_ki_protection
async def aclose(self) -> None:
"""Close this receive channel object asynchronously.

See `MemoryReceiveChannel.close`."""
self.close()
await trio.lowlevel.checkpoint()
7 changes: 2 additions & 5 deletions src/trio/_tests/_check_type_completeness.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
"all": [
"No docstring found for class \"trio.MemoryReceiveChannel\"",
"No docstring found for class \"trio._channel.MemoryReceiveChannel\"",
"No docstring found for function \"trio._channel.MemoryReceiveChannel.statistics\"",
"No docstring found for class \"trio._channel.MemoryChannelStats\"",
"No docstring found for function \"trio._channel.MemoryReceiveChannel.aclose\"",
"No docstring found for class \"trio.MemoryChannelStatistics\"",
"No docstring found for class \"trio._channel.MemoryChannelStatistics\"",
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could've added a docstring to the class at the same time to get rid of these as well

"No docstring found for class \"trio.MemorySendChannel\"",
"No docstring found for class \"trio._channel.MemorySendChannel\"",
"No docstring found for function \"trio._channel.MemorySendChannel.statistics\"",
"No docstring found for function \"trio._channel.MemorySendChannel.aclose\"",
"No docstring found for class \"trio._core._run.Task\"",
"No docstring found for class \"trio._socket.SocketType\"",
"No docstring found for function \"trio._highlevel_socket.SocketStream.send_all\"",
Expand Down
Loading