Skip to content

Commit 040c845

Browse files
committed
Make some internal __init__ arguments positional/keyword-only
This is to make the code more readable/consistent. Note that `Select` is actually public, but the constructor is not meant to be used by users. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 76ded97 commit 040c845

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

src/frequenz/channels/_anycast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class _Sender(Sender[_T]):
309309
method.
310310
"""
311311

312-
def __init__(self, channel: Anycast[_T]) -> None:
312+
def __init__(self, channel: Anycast[_T], /) -> None:
313313
"""Initialize this sender.
314314
315315
Args:
@@ -377,7 +377,7 @@ class _Receiver(Receiver[_T]):
377377
method.
378378
"""
379379

380-
def __init__(self, channel: Anycast[_T]) -> None:
380+
def __init__(self, channel: Anycast[_T], /) -> None:
381381
"""Initialize this receiver.
382382
383383
Args:

src/frequenz/channels/_broadcast.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def new_receiver(self, *, name: str | None = None, limit: int = 50) -> Receiver[
279279
Returns:
280280
A new receiver attached to this channel.
281281
"""
282-
recv: _Receiver[_T] = _Receiver(name, limit, self)
282+
recv: _Receiver[_T] = _Receiver(self, name=name, limit=limit)
283283
self._receivers[hash(recv)] = weakref.ref(recv)
284284
if self.resend_latest and self._latest is not None:
285285
recv.enqueue(self._latest)
@@ -308,7 +308,7 @@ class _Sender(Sender[_T]):
308308
method.
309309
"""
310310

311-
def __init__(self, channel: Broadcast[_T]) -> None:
311+
def __init__(self, channel: Broadcast[_T], /) -> None:
312312
"""Initialize this sender.
313313
314314
Args:
@@ -364,21 +364,23 @@ class _Receiver(Receiver[_T]):
364364
method.
365365
"""
366366

367-
def __init__(self, name: str | None, limit: int, channel: Broadcast[_T]) -> None:
367+
def __init__(
368+
self, channel: Broadcast[_T], /, *, name: str | None, limit: int
369+
) -> None:
368370
"""Initialize this receiver.
369371
370372
Broadcast receivers have their own buffer, and when messages are not
371373
being consumed fast enough and the buffer fills up, old messages will
372374
get dropped just in this receiver.
373375
374376
Args:
377+
channel: a reference to the Broadcast channel that this receiver
378+
belongs to.
375379
name: A name to identify the receiver in the logs. If `None` an
376380
`id(self)`-based name will be used. This is only for debugging
377381
purposes, it will be shown in the string representation of the
378382
receiver.
379383
limit: Number of messages the receiver can hold in its buffer.
380-
channel: a reference to the Broadcast channel that this receiver
381-
belongs to.
382384
"""
383385
self._name: str = name if name is not None else f"{id(self):_}"
384386
"""The name to identify the receiver.
@@ -392,7 +394,7 @@ def __init__(self, name: str | None, limit: int, channel: Broadcast[_T]) -> None
392394
self._q: deque[_T] = deque(maxlen=limit)
393395
"""The receiver's internal message queue."""
394396

395-
def enqueue(self, message: _T) -> None:
397+
def enqueue(self, message: _T, /) -> None:
396398
"""Put a message into this receiver's queue.
397399
398400
To be called by broadcast senders. If the receiver's queue is already

src/frequenz/channels/_receiver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def map(self, mapping_function: Callable[[_T_co], _U_co]) -> Receiver[_U_co]:
240240
Returns:
241241
A new receiver that applies the function on the received messages.
242242
"""
243-
return _Mapper(self, mapping_function)
243+
return _Mapper(receiver=self, mapping_function=mapping_function)
244244

245245

246246
class ReceiverError(Error, Generic[_T_co]):
@@ -285,7 +285,7 @@ class _Mapper(Receiver[_U_co], Generic[_T_co, _U_co]):
285285
"""
286286

287287
def __init__(
288-
self, receiver: Receiver[_T_co], mapping_function: Callable[[_T_co], _U_co]
288+
self, *, receiver: Receiver[_T_co], mapping_function: Callable[[_T_co], _U_co]
289289
) -> None:
290290
"""Initialize this receiver mapper.
291291

src/frequenz/channels/_select.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Selected(Generic[_T]):
170170
Please see [`select()`][frequenz.channels.select] for an example.
171171
"""
172172

173-
def __init__(self, receiver: Receiver[_T]) -> None:
173+
def __init__(self, receiver: Receiver[_T], /) -> None:
174174
"""Initialize this selected result.
175175
176176
The receiver is consumed immediately when creating the instance and the received

0 commit comments

Comments
 (0)