Skip to content

Commit ce4d0e6

Browse files
committed
Use forward type annotations
Besides improving code legibility, they are necessary for the rendered documentation to be properly cross-referenced (otherwise the types appear just as a string, without links). Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 6d4516f commit ce4d0e6

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/frequenz/channels/anycast.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""A channel for sending data across async tasks."""
55

6+
from __future__ import annotations
7+
68
from asyncio import Condition
79
from collections import deque
810
from typing import Deque, Generic, Optional
@@ -88,15 +90,15 @@ async def close(self) -> None:
8890
async with self.recv_cv:
8991
self.recv_cv.notify_all()
9092

91-
def get_sender(self) -> "Sender[T]":
93+
def get_sender(self) -> Sender[T]:
9294
"""Create a new sender.
9395
9496
Returns:
9597
A Sender instance attached to the Anycast channel.
9698
"""
9799
return Sender(self)
98100

99-
def get_receiver(self) -> "Receiver[T]":
101+
def get_receiver(self) -> Receiver[T]:
100102
"""Create a new receiver.
101103
102104
Returns:

src/frequenz/channels/base_classes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Baseclasses for Channel Sender and Receiver."""
55

6+
from __future__ import annotations
7+
68
from abc import ABC, abstractmethod
79
from typing import Callable, Generic, Optional, TypeVar
810

@@ -37,7 +39,7 @@ async def receive(self) -> Optional[T]:
3739
None, if the channel is closed, a message otherwise.
3840
"""
3941

40-
def __aiter__(self) -> "Receiver[T]":
42+
def __aiter__(self) -> Receiver[T]:
4143
"""Initialize the async iterator over received values.
4244
4345
Returns:
@@ -60,7 +62,7 @@ async def __anext__(self) -> T:
6062
raise StopAsyncIteration
6163
return received
6264

63-
def map(self, call: Callable[[T], U]) -> "Receiver[U]":
65+
def map(self, call: Callable[[T], U]) -> Receiver[U]:
6466
"""Return a receiver with `call` applied on incoming messages.
6567
6668
Args:
@@ -71,7 +73,7 @@ def map(self, call: Callable[[T], U]) -> "Receiver[U]":
7173
"""
7274
return _Map(self, call)
7375

74-
def into_peekable(self) -> "Peekable[T]":
76+
def into_peekable(self) -> Peekable[T]:
7577
"""Convert the `Receiver` implementation into a `Peekable`.
7678
7779
Once this function has been called, the receiver will no longer be

src/frequenz/channels/bidirectional.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""An abstraction to provide bi-directional communication between actors."""
55

6+
from __future__ import annotations
7+
68
from typing import Generic, Optional
79

810
from frequenz.channels.base_classes import Receiver, Sender, T, U
@@ -35,7 +37,7 @@ def __init__(self, client_id: str, service_id: str) -> None:
3537
)
3638

3739
@property
38-
def client_handle(self) -> "BidirectionalHandle[T, U]":
40+
def client_handle(self) -> BidirectionalHandle[T, U]:
3941
"""Get a BidirectionalHandle for the client to use.
4042
4143
Returns:
@@ -44,7 +46,7 @@ def client_handle(self) -> "BidirectionalHandle[T, U]":
4446
return self._client_handle
4547

4648
@property
47-
def service_handle(self) -> "BidirectionalHandle[U, T]":
49+
def service_handle(self) -> BidirectionalHandle[U, T]:
4850
"""Get a BidirectionalHandle for the service to use.
4951
5052
Returns:

src/frequenz/channels/broadcast.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""A channel to broadcast messages to all receivers."""
55

6+
from __future__ import annotations
7+
68
import logging
79
from asyncio import Condition
810
from collections import deque
@@ -110,7 +112,7 @@ def _drop_receiver(self, uuid: UUID) -> None:
110112
if uuid in self.receivers:
111113
del self.receivers[uuid]
112114

113-
def get_sender(self) -> "Sender[T]":
115+
def get_sender(self) -> Sender[T]:
114116
"""Create a new broadcast sender.
115117
116118
Returns:
@@ -120,7 +122,7 @@ def get_sender(self) -> "Sender[T]":
120122

121123
def get_receiver(
122124
self, name: Optional[str] = None, maxsize: int = 50
123-
) -> "Receiver[T]":
125+
) -> Receiver[T]:
124126
"""Create a new broadcast receiver.
125127
126128
Broadcast receivers have their own buffer, and when messages are not
@@ -137,13 +139,13 @@ def get_receiver(
137139
uuid = uuid4()
138140
if name is None:
139141
name = str(uuid)
140-
recv: "Receiver[T]" = Receiver(uuid, name, maxsize, self)
142+
recv: Receiver[T] = Receiver(uuid, name, maxsize, self)
141143
self.receivers[uuid] = recv
142144
if self._resend_latest and self._latest is not None:
143145
recv.enqueue(self._latest)
144146
return recv
145147

146-
def get_peekable(self) -> "Peekable[T]":
148+
def get_peekable(self) -> Peekable[T]:
147149
"""Create a new Peekable for the broadcast channel.
148150
149151
A Peekable provides a [peek()][frequenz.channels.Peekable.peek] method
@@ -283,7 +285,7 @@ async def receive(self) -> Optional[T]:
283285
ret = self._q.popleft()
284286
return ret
285287

286-
def into_peekable(self) -> "Peekable[T]":
288+
def into_peekable(self) -> Peekable[T]:
287289
"""Convert the `Receiver` implementation into a `Peekable`.
288290
289291
Once this function has been called, the receiver will no longer be

0 commit comments

Comments
 (0)