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

Fixed repr of MemoryObjectItemReceiver #767

Merged
merged 5 commits into from
Aug 17, 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/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fixed ``__repr__()`` of ``MemoryObjectItemReceiver``, when ``item`` is not defined
(`#767 <https://github.com/agronholm/anyio/pulls/767>`_; PR by @Danipulok)
- Added support for the ``from_uri()``, ``full_match()``, ``parser`` methods/properties
in ``anyio.Path``, newly added in Python 3.13
(`#737 <https://github.com/agronholm/anyio/issues/737>`_)
Expand Down
6 changes: 6 additions & 0 deletions src/anyio/streams/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class MemoryObjectItemReceiver(Generic[T_Item]):
task_info: TaskInfo = field(init=False, default_factory=get_current_task)
item: T_Item = field(init=False)

def __repr__(self) -> str:
# When item is not defined, we get following error with default __repr__:
# AttributeError: 'MemoryObjectItemReceiver' object has no attribute 'item'
item = getattr(self, "item", None)
return f"{self.__class__.__name__}(task_info={self.task_info}, item={item!r})"


@dataclass(eq=False)
class MemoryObjectStreamState(Generic[T_Item]):
Expand Down
23 changes: 22 additions & 1 deletion tests/streams/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
wait_all_tasks_blocked,
)
from anyio.abc import ObjectReceiveStream, ObjectSendStream, TaskStatus
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from anyio.streams.memory import (
MemoryObjectItemReceiver,
MemoryObjectReceiveStream,
MemoryObjectSendStream,
)

if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup
Expand Down Expand Up @@ -502,3 +506,20 @@ async def test_send_to_natively_cancelled_receiver() -> None:
await receive_task

assert receive.receive_nowait() == "hello"


async def test_memory_object_item_receiver_repr() -> None:
"""
Test the repr of `MemoryObjectItemReceiver`.
Since when `item` is not set, the default dataclass repr raises an AttributeError.
"""
receiver = MemoryObjectItemReceiver[str]()

assert str(receiver) is not None
receiver_repr = repr(receiver)
assert "item=None" in receiver_repr

assert str(receiver) is not None
receiver.item = "test_item"
receiver_repr = repr(receiver)
assert "item='test_item'" in receiver_repr
Loading