Skip to content

Commit b6475b7

Browse files
committed
Add @override decorators
Classes that implement the `Sender` or `Receiver` interfaces currently need to override the `send` or the `ready` and `consume` methods respectively. As we add more methods, to these interfaces, it becomes hard to track which methods are there for internal use and which ones are there to implement the interface. Using the `override` decorators helps with that. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 3f6143a commit b6475b7

File tree

8 files changed

+34
-0
lines changed

8 files changed

+34
-0
lines changed

src/frequenz/channels/_anycast.py

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

13+
from typing_extensions import override
14+
1315
from ._exceptions import ChannelClosedError
1416
from ._generic import ChannelMessageT
1517
from ._receiver import Receiver, ReceiverStoppedError
@@ -320,6 +322,7 @@ def __init__(self, channel: Anycast[_T], /) -> None:
320322
self._channel: Anycast[_T] = channel
321323
"""The channel that this sender belongs to."""
322324

325+
@override
323326
async def send(self, message: _T, /) -> None:
324327
"""Send a message across the channel.
325328
@@ -390,6 +393,7 @@ def __init__(self, channel: Anycast[_T], /) -> None:
390393

391394
self._next: _T | type[_Empty] = _Empty
392395

396+
@override
393397
async def ready(self) -> bool:
394398
"""Wait until the receiver is ready with a message or an error.
395399
@@ -417,6 +421,7 @@ async def ready(self) -> bool:
417421
# pylint: enable=protected-access
418422
return True
419423

424+
@override
420425
def consume(self) -> _T:
421426
"""Return the latest message once `ready()` is complete.
422427

src/frequenz/channels/_broadcast.py

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

14+
from typing_extensions import override
15+
1416
from ._exceptions import ChannelClosedError
1517
from ._generic import ChannelMessageT
1618
from ._receiver import Receiver, ReceiverStoppedError
@@ -327,6 +329,7 @@ def __init__(self, channel: Broadcast[_T], /) -> None:
327329
self._channel: Broadcast[_T] = channel
328330
"""The broadcast channel this sender belongs to."""
329331

332+
@override
330333
async def send(self, message: _T, /) -> None:
331334
"""Send a message to all broadcast receivers.
332335
@@ -441,6 +444,7 @@ def __len__(self) -> int:
441444
"""
442445
return len(self._q)
443446

447+
@override
444448
async def ready(self) -> bool:
445449
"""Wait until the receiver is ready with a message or an error.
446450
@@ -469,6 +473,7 @@ async def ready(self) -> bool:
469473
return True
470474
# pylint: enable=protected-access
471475

476+
@override
472477
def consume(self) -> _T:
473478
"""Return the latest message once `ready` is complete.
474479

src/frequenz/channels/_merge.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
from collections import deque
5555
from typing import Any
5656

57+
from typing_extensions import override
58+
5759
from ._generic import ReceiverMessageT_co
5860
from ._receiver import Receiver, ReceiverStoppedError
5961

@@ -135,6 +137,7 @@ async def stop(self) -> None:
135137
await asyncio.gather(*self._pending, return_exceptions=True)
136138
self._pending = set()
137139

140+
@override
138141
async def ready(self) -> bool:
139142
"""Wait until the receiver is ready with a message or an error.
140143
@@ -171,6 +174,7 @@ async def ready(self) -> bool:
171174
asyncio.create_task(anext(self._receivers[name]), name=name)
172175
)
173176

177+
@override
174178
def consume(self) -> ReceiverMessageT_co:
175179
"""Return the latest message once `ready` is complete.
176180

src/frequenz/channels/_receiver.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@
157157
from collections.abc import Callable
158158
from typing import TYPE_CHECKING, Any, Generic, Self, TypeGuard, TypeVar, overload
159159

160+
from typing_extensions import override
161+
160162
from ._exceptions import Error
161163
from ._generic import MappedMessageT_co, ReceiverMessageT_co
162164

@@ -433,6 +435,7 @@ def __init__(
433435
)
434436
"""The function to apply on the input data."""
435437

438+
@override
436439
async def ready(self) -> bool:
437440
"""Wait until the receiver is ready with a message or an error.
438441
@@ -448,6 +451,7 @@ async def ready(self) -> bool:
448451

449452
# We need a noqa here because the docs have a Raises section but the code doesn't
450453
# explicitly raise anything.
454+
@override
451455
def consume(self) -> MappedMessageT_co: # noqa: DOC502
452456
"""Return a transformed message once `ready()` is complete.
453457
@@ -509,6 +513,7 @@ def __init__(
509513

510514
self._recv_closed = False
511515

516+
@override
512517
async def ready(self) -> bool:
513518
"""Wait until the receiver is ready with a message or an error.
514519
@@ -528,6 +533,7 @@ async def ready(self) -> bool:
528533
self._recv_closed = True
529534
return False
530535

536+
@override
531537
def consume(self) -> ReceiverMessageT_co:
532538
"""Return a transformed message once `ready()` is complete.
533539

src/frequenz/channels/event.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import asyncio as _asyncio
1818

19+
from typing_extensions import override
20+
1921
from frequenz.channels._receiver import Receiver, ReceiverStoppedError
2022

2123

@@ -141,6 +143,7 @@ def set(self) -> None:
141143
self._is_set = True
142144
self._event.set()
143145

146+
@override
144147
async def ready(self) -> bool:
145148
"""Wait until this receiver is ready.
146149
@@ -152,6 +155,7 @@ async def ready(self) -> bool:
152155
await self._event.wait()
153156
return not self._is_stopped
154157

158+
@override
155159
def consume(self) -> None:
156160
"""Consume the event.
157161

src/frequenz/channels/experimental/_relay_sender.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import typing
1111

12+
from typing_extensions import override
13+
1214
from .._generic import SenderMessageT_contra
1315
from .._sender import Sender
1416

@@ -46,6 +48,7 @@ def __init__(self, *senders: Sender[SenderMessageT_contra]) -> None:
4648
"""
4749
self._senders = senders
4850

51+
@override
4952
async def send(self, message: SenderMessageT_contra, /) -> None:
5053
"""Send a message.
5154

src/frequenz/channels/file_watcher.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from datetime import timedelta
2626
from enum import Enum
2727

28+
from typing_extensions import override
2829
from watchfiles import Change, awatch
2930
from watchfiles.main import FileChange
3031

@@ -185,6 +186,7 @@ def __del__(self) -> None:
185186
# is stopped.
186187
self._stop_event.set()
187188

189+
@override
188190
async def ready(self) -> bool:
189191
"""Wait until the receiver is ready with a message or an error.
190192
@@ -212,6 +214,7 @@ async def ready(self) -> bool:
212214

213215
return True
214216

217+
@override
215218
def consume(self) -> Event:
216219
"""Return the latest event once `ready` is complete.
217220

src/frequenz/channels/timer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ async def main() -> None:
102102
import asyncio
103103
from datetime import timedelta
104104

105+
from typing_extensions import override
106+
105107
from ._receiver import Receiver, ReceiverStoppedError
106108

107109

@@ -644,6 +646,7 @@ def stop(self) -> None:
644646

645647
# We need a noqa here because the docs have a Raises section but the documented
646648
# exceptions are raised indirectly.
649+
@override
647650
async def ready(self) -> bool: # noqa: DOC502
648651
"""Wait until the timer `interval` passed.
649652
@@ -715,6 +718,7 @@ async def ready(self) -> bool: # noqa: DOC502
715718

716719
return True
717720

721+
@override
718722
def consume(self) -> timedelta:
719723
"""Return the latest drift once `ready()` is complete.
720724

0 commit comments

Comments
 (0)