Skip to content

Commit 27094a8

Browse files
committed
Expose TypeVars used to GroupingLatestValueCache
Making the part of the public API makes it easy for users to look them up and understand how to use them. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent ccd751e commit 27094a8

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/frequenz/channels/experimental/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,26 @@
1010
guidelines](https://github.com/frequenz-floss/docs/blob/v0.x.x/python/experimental-packages.md).
1111
"""
1212

13-
from ._grouping_latest_value_cache import GroupingLatestValueCache
13+
from ._grouping_latest_value_cache import (
14+
DefaultT,
15+
GroupingLatestValueCache,
16+
HashableT,
17+
ValueT_co,
18+
)
1419
from ._nop_receiver import NopReceiver
1520
from ._optional_receiver import OptionalReceiver
1621
from ._pipe import Pipe
1722
from ._relay_sender import RelaySender
1823
from ._with_previous import WithPrevious
1924

2025
__all__ = [
26+
"DefaultT",
2127
"GroupingLatestValueCache",
28+
"HashableT",
2229
"NopReceiver",
2330
"OptionalReceiver",
2431
"Pipe",
2532
"RelaySender",
33+
"ValueT_co",
2634
"WithPrevious",
2735
]

src/frequenz/channels/experimental/_grouping_latest_value_cache.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,24 @@
4545

4646
from .._receiver import Receiver
4747

48-
T_co = typing.TypeVar("T_co", covariant=True)
49-
T = typing.TypeVar("T")
48+
ValueT_co = typing.TypeVar("ValueT_co", covariant=True)
49+
"""Covariant type variable for the values cached by the `GroupingLatestValueCache`."""
50+
51+
DefaultT = typing.TypeVar("DefaultT")
52+
"""Type variable for the default value returned by `GroupingLatestValueCache.get`."""
53+
5054
HashableT = typing.TypeVar("HashableT", bound=typing.Hashable)
55+
"""Type variable for the keys used to group values in the `GroupingLatestValueCache`."""
5156

5257

53-
class GroupingLatestValueCache(Mapping[HashableT, T_co]):
58+
class GroupingLatestValueCache(Mapping[HashableT, ValueT_co]):
5459
"""A cache that stores the latest value in a receiver, grouped by key."""
5560

5661
def __init__(
5762
self,
58-
receiver: Receiver[T_co],
63+
receiver: Receiver[ValueT_co],
5964
*,
60-
key: typing.Callable[[T_co], HashableT],
65+
key: typing.Callable[[ValueT_co], HashableT],
6166
unique_id: str | None = None,
6267
) -> None:
6368
"""Create a new cache.
@@ -70,10 +75,10 @@ def __init__(
7075
provided, a unique identifier will be generated from the object's
7176
[`id()`][id]. It is used mostly for debugging purposes.
7277
"""
73-
self._receiver: Receiver[T_co] = receiver
74-
self._key: typing.Callable[[T_co], HashableT] = key
78+
self._receiver: Receiver[ValueT_co] = receiver
79+
self._key: typing.Callable[[ValueT_co], HashableT] = key
7580
self._unique_id: str = hex(id(self)) if unique_id is None else unique_id
76-
self._latest_value_by_key: dict[HashableT, T_co] = {}
81+
self._latest_value_by_key: dict[HashableT, ValueT_co] = {}
7782
self._task: asyncio.Task[None] = asyncio.create_task(
7883
self._run(), name=f"LatestValueCache«{self._unique_id}»"
7984
)
@@ -92,28 +97,30 @@ def keys(self) -> KeysView[HashableT]:
9297
return self._latest_value_by_key.keys()
9398

9499
@override
95-
def items(self) -> ItemsView[HashableT, T_co]:
100+
def items(self) -> ItemsView[HashableT, ValueT_co]:
96101
"""Return an iterator over the key-value pairs of the latest values received."""
97102
return self._latest_value_by_key.items()
98103

99104
@override
100-
def values(self) -> ValuesView[T_co]:
105+
def values(self) -> ValuesView[ValueT_co]:
101106
"""Return an iterator over the latest values received."""
102107
return self._latest_value_by_key.values()
103108

104109
@typing.overload
105-
def get(self, key: HashableT, default: None = None) -> T_co | None:
110+
def get(self, key: HashableT, default: None = None) -> ValueT_co | None:
106111
"""Return the latest value that has been received for a specific key."""
107112

108113
# MyPy passes this overload as a valid signature, but pylint does not like it.
109114
@typing.overload
110115
def get( # pylint: disable=signature-differs
111-
self, key: HashableT, default: T
112-
) -> T_co | T:
116+
self, key: HashableT, default: DefaultT
117+
) -> ValueT_co | DefaultT:
113118
"""Return the latest value that has been received for a specific key."""
114119

115120
@override
116-
def get(self, key: HashableT, default: T | None = None) -> T_co | T | None:
121+
def get(
122+
self, key: HashableT, default: DefaultT | None = None
123+
) -> ValueT_co | DefaultT | None:
117124
"""Return the latest value that has been received.
118125
119126
Args:
@@ -138,7 +145,7 @@ def __len__(self) -> int:
138145
return len(self._latest_value_by_key)
139146

140147
@override
141-
def __getitem__(self, key: HashableT) -> T_co:
148+
def __getitem__(self, key: HashableT) -> ValueT_co:
142149
"""Return the latest value that has been received for a specific key.
143150
144151
Args:

0 commit comments

Comments
 (0)