Skip to content

Commit 77506bd

Browse files
committed
Rename close() to aclose() in Broadcast and Anycast
This is done to follow Python's convention. With this change now channels can be used with the `aclosing()` context manager for example. The name `close()` is still available for backwards compatibility, but deprecated. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 8f99011 commit 77506bd

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
## Upgrading
88

99
- Some minimal dependencies have been bumped, so you might need to adjust your dependencies accordingly.
10+
- `Broadcast` and `Anycast` channels method `close()` was renamed to `aclose()` to follow Python's convention. With this change now channels can be used with the [`aclosing()`](https://docs.python.org/3/library/contextlib.html#contextlib.aclosing) context manager for example.
11+
12+
The name `close()` is still available for backwards compatibility, but it will be removed in the next major release, so it is recommended to switch to `aclose()`.
1013

1114
## New Features
1215

benchmarks/benchmark_anycast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def update_tracker_on_receive(chan: Receiver[int]) -> None:
6666

6767
await asyncio.gather(*senders)
6868
for bcast in channels:
69-
await bcast.close()
69+
await bcast.aclose()
7070
await receivers_runs
7171
return recv_trackers[0]
7272

benchmarks/benchmark_broadcast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def update_tracker_on_receive(chan: Receiver[int]) -> None:
8484

8585
await asyncio.gather(*senders)
8686
for bcast in channels:
87-
await bcast.close()
87+
await bcast.aclose()
8888
await receivers_runs
8989
recv_tracker = sum(recv_trackers)
9090
assert recv_tracker == num_messages * num_channels * num_receivers

src/frequenz/channels/_anycast.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections import deque
1111
from typing import Generic, TypeVar
1212

13-
from typing_extensions import override
13+
from typing_extensions import deprecated, override
1414

1515
from ._exceptions import ChannelClosedError
1616
from ._generic import ChannelMessageT
@@ -78,7 +78,7 @@ class Anycast(Generic[ChannelMessageT]):
7878
respectively.
7979
8080
When the channel is not needed anymore, it should be closed with the
81-
[`close()`][frequenz.channels.Anycast.close] method. This will prevent further
81+
[`aclose()`][frequenz.channels.Anycast.aclose] method. This will prevent further
8282
attempts to [`send()`][frequenz.channels.Sender.send] data. Receivers will still be
8383
able to drain the pending messages on the channel, but after that, subsequent
8484
[`receive()`][frequenz.channels.Receiver.receive] calls will raise a
@@ -266,7 +266,7 @@ def limit(self) -> int:
266266
assert maxlen is not None
267267
return maxlen
268268

269-
async def close(self) -> None:
269+
async def aclose(self) -> None:
270270
"""Close the channel.
271271
272272
Any further attempts to [send()][frequenz.channels.Sender.send] data
@@ -283,6 +283,11 @@ async def close(self) -> None:
283283
async with self._recv_cv:
284284
self._recv_cv.notify_all()
285285

286+
@deprecated("The close() method is deprecated, use aclose() instead")
287+
async def close(self) -> None: # noqa: D402
288+
"""Close the channel, deprecated alias for `aclose()`.""" # noqa: D402
289+
return await self.aclose()
290+
286291
def new_sender(self) -> Sender[ChannelMessageT]:
287292
"""Return a new sender attached to this channel."""
288293
return _Sender(self)

src/frequenz/channels/_broadcast.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections import deque
1212
from typing import Generic, TypeVar
1313

14-
from typing_extensions import override
14+
from typing_extensions import deprecated, override
1515

1616
from ._exceptions import ChannelClosedError
1717
from ._generic import ChannelMessageT
@@ -65,7 +65,7 @@ class Broadcast(Generic[ChannelMessageT]):
6565
respectively.
6666
6767
When a channel is not needed anymore, it should be closed with
68-
[`close()`][frequenz.channels.Broadcast.close]. This will prevent further
68+
[`aclose()`][frequenz.channels.Broadcast.aclose]. This will prevent further
6969
attempts to [`send()`][frequenz.channels.Sender.send] data, and will allow
7070
receivers to drain the pending items on their queues, but after that,
7171
subsequent [receive()][frequenz.channels.Receiver.receive] calls will
@@ -248,7 +248,7 @@ def is_closed(self) -> bool:
248248
"""
249249
return self._closed
250250

251-
async def close(self) -> None:
251+
async def aclose(self) -> None:
252252
"""Close this channel.
253253
254254
Any further attempts to [send()][frequenz.channels.Sender.send] data
@@ -264,6 +264,11 @@ async def close(self) -> None:
264264
async with self._recv_cv:
265265
self._recv_cv.notify_all()
266266

267+
@deprecated("The close() method is deprecated, use aclose() instead")
268+
async def close(self) -> None: # noqa: D402
269+
"""Close the channel, deprecated alias for `aclose()`.""" # noqa: D402
270+
return await self.aclose()
271+
267272
def new_sender(self) -> Sender[ChannelMessageT]:
268273
"""Return a new sender attached to this channel."""
269274
return _Sender(self)

0 commit comments

Comments
 (0)