Skip to content

Commit 665ad01

Browse files
committed
Make Selected.was_stopped a property
We are already using properties for anything that can be trivially calculated, so this looks like a good candidate too to make the interface more consistent. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent d65d7ef commit 665ad01

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/frequenz/channels/_select.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
7474
When a single [receiver][frequenz.channels.Receiver] is stopped, it will be reported
7575
via the [`Selected`][frequenz.channels.Selected] object. You can use the
76-
[`was_stopped()`][frequenz.channels.Selected.was_stopped] method to check if the
76+
[`was_stopped`][frequenz.channels.Selected.was_stopped] method to check if the
7777
selected [receiver][frequenz.channels.Receiver] was stopped:
7878
7979
```python show_lines="8:"
@@ -86,15 +86,15 @@
8686
8787
async for selected in select(receiver1, receiver2):
8888
if selected_from(selected, receiver1):
89-
if selected.was_stopped():
89+
if selected.was_stopped:
9090
print("receiver1 was stopped")
9191
continue
9292
print(f"Received from receiver1, the next number is: {selected.value + 1}")
9393
# ...
9494
```
9595
9696
Tip:
97-
The [`was_stopped()`][frequenz.channels.Selected.was_stopped] method is a
97+
The [`was_stopped`][frequenz.channels.Selected.was_stopped] method is a
9898
convenience method that is equivalent to checking if the
9999
[`exception`][frequenz.channels.Selected.exception] attribute is an instance of
100100
[`ReceiverStoppedError`][frequenz.channels.ReceiverStoppedError].
@@ -228,16 +228,9 @@ def exception(self) -> Exception | None:
228228
"""
229229
return self._exception
230230

231+
@property
231232
def was_stopped(self) -> bool:
232-
"""Check if the selected receiver was stopped.
233-
234-
Check if the selected receiver raised
235-
a [`ReceiverStoppedError`][frequenz.channels.ReceiverStoppedError] while
236-
consuming a value.
237-
238-
Returns:
239-
Whether the receiver was stopped.
240-
"""
233+
"""Whether the selected receiver was stopped while receiving a value."""
241234
return isinstance(self._exception, ReceiverStoppedError)
242235

243236
def __str__(self) -> str:

tests/test_select.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_with_value(self) -> None:
2222
assert selected_from(selected, recv)
2323
assert selected.value == 42
2424
assert selected.exception is None
25-
assert not selected.was_stopped()
25+
assert not selected.was_stopped
2626

2727
def test_with_exception(self) -> None:
2828
"""Test selected from a receiver with an exception."""
@@ -35,7 +35,7 @@ def test_with_exception(self) -> None:
3535
with pytest.raises(Exception, match="test"):
3636
_ = selected.value
3737
assert selected.exception is exception
38-
assert not selected.was_stopped()
38+
assert not selected.was_stopped
3939

4040
def test_with_stopped(self) -> None:
4141
"""Test selected from a stopped receiver."""
@@ -51,4 +51,4 @@ def test_with_stopped(self) -> None:
5151
):
5252
_ = selected.value
5353
assert selected.exception is exception
54-
assert selected.was_stopped()
54+
assert selected.was_stopped

tests/test_select_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def assert_received_from(
9090
assert selected_from(selected, receiver)
9191
assert selected.value is None
9292
assert selected.exception is None
93-
assert not selected.was_stopped()
93+
assert not selected.was_stopped
9494
if expected_pending_tasks > 0:
9595
assert len(asyncio.all_tasks(self.loop)) == expected_pending_tasks
9696
elif expected_pending_tasks < 0:
@@ -121,7 +121,7 @@ def assert_receiver_stopped(
121121
it is 0, no check is performed.
122122
"""
123123
assert selected_from(selected, receiver)
124-
assert selected.was_stopped()
124+
assert selected.was_stopped
125125
assert isinstance(selected.exception, ReceiverStoppedError)
126126
assert selected.exception.receiver is receiver
127127
if expected_pending_tasks > 0:

0 commit comments

Comments
 (0)