| 
10 | 10 | takes a [Receiver][frequenz.channels.Receiver] and a `key` function as arguments and  | 
11 | 11 | stores the latest value received by that receiver for each key separately.  | 
12 | 12 | 
  | 
13 |  | -As soon as a value is received for a `key`, the  | 
14 |  | -[`has_value`][frequenz.channels.experimental.GroupingLatestValueCache.has_value] method  | 
15 |  | -returns `True` for that `key`, and the [`get`][frequenz.channels.LatestValueCache.get]  | 
16 |  | -method for that `key` returns the latest value received.  The `get` method will raise an  | 
17 |  | -exception if called before any messages have been received from the receiver for a given  | 
18 |  | -`key`.  | 
 | 13 | +The `GroupingLatestValueCache` implements the [`Mapping`][collections.abc.Mapping]  | 
 | 14 | +interface, so it can be used like a dictionary.  In addition, it provides a  | 
 | 15 | +[has_value][frequenz.channels.experimental.GroupingLatestValueCache.has_value] method to  | 
 | 16 | +check if a value has been received for a specific key, and a  | 
 | 17 | +[clear][frequenz.channels.experimental.GroupingLatestValueCache.clear] method to clear  | 
 | 18 | +the cached value for a specific key.  | 
19 | 19 | 
  | 
20 | 20 | Example:  | 
21 | 21 | ```python  | 
 | 
39 | 39 | 
 
  | 
40 | 40 | import asyncio  | 
41 | 41 | import typing  | 
42 |  | -from collections.abc import Set  | 
 | 42 | +from collections.abc import Iterator, KeysView, Mapping  | 
 | 43 | + | 
 | 44 | +from typing_extensions import override  | 
43 | 45 | 
 
  | 
44 | 46 | from .._receiver import Receiver  | 
45 | 47 | 
 
  | 
46 | 48 | T_co = typing.TypeVar("T_co", covariant=True)  | 
 | 49 | +T = typing.TypeVar("T")  | 
47 | 50 | HashableT = typing.TypeVar("HashableT", bound=typing.Hashable)  | 
48 | 51 | 
 
  | 
49 | 52 | 
 
  | 
50 |  | -class GroupingLatestValueCache(typing.Generic[T_co, HashableT]):  | 
51 |  | -    """A cache that stores the latest value in a receiver.  | 
52 |  | -
  | 
53 |  | -    It provides a way to look up the latest value in a stream without any delay,  | 
54 |  | -    as long as there has been one value received.  | 
55 |  | -    """  | 
 | 53 | +class GroupingLatestValueCache(  | 
 | 54 | +    typing.Generic[T_co, HashableT], Mapping[HashableT, T_co]  | 
 | 55 | +):  | 
 | 56 | +    """A cache that stores the latest value in a receiver, grouped by key."""  | 
56 | 57 | 
 
  | 
57 | 58 |     def __init__(  | 
58 | 59 |         self,  | 
@@ -84,32 +85,65 @@ def unique_id(self) -> str:  | 
84 | 85 |         """The unique identifier of this instance."""  | 
85 | 86 |         return self._unique_id  | 
86 | 87 | 
 
  | 
87 |  | -    def keys(self) -> Set[HashableT]:  | 
 | 88 | +    @override  | 
 | 89 | +    def keys(self) -> KeysView[HashableT]:  | 
88 | 90 |         """Return the set of keys for which values have been received.  | 
89 | 91 | 
  | 
90 | 92 |         If no key function is provided, this will return an empty set.  | 
91 | 93 |         """  | 
92 | 94 |         return self._latest_value_by_key.keys()  | 
93 | 95 | 
 
  | 
94 |  | -    def get(self, key: HashableT) -> T_co:  | 
95 |  | -        """Return the latest value that has been received.  | 
 | 96 | +    @typing.overload  | 
 | 97 | +    def get(self, key: HashableT, default: None = None) -> T_co | None:  | 
 | 98 | +        """Return the latest value that has been received for a specific key."""  | 
96 | 99 | 
 
  | 
97 |  | -        This raises a `ValueError` if no value has been received yet. Use `has_value` to  | 
98 |  | -        check whether a value has been received yet, before trying to access the value,  | 
99 |  | -        to avoid the exception.  | 
 | 100 | +    # MyPy passes this overload as a valid signature, but pylint does not like it.  | 
 | 101 | +    @typing.overload  | 
 | 102 | +    def get(  # pylint: disable=signature-differs  | 
 | 103 | +        self, key: HashableT, default: T  | 
 | 104 | +    ) -> T_co | T:  | 
 | 105 | +        """Return the latest value that has been received for a specific key."""  | 
 | 106 | + | 
 | 107 | +    @override  | 
 | 108 | +    def get(self, key: HashableT, default: T | None = None) -> T_co | T | None:  | 
 | 109 | +        """Return the latest value that has been received.  | 
100 | 110 | 
  | 
101 | 111 |         Args:  | 
102 | 112 |             key: An optional key to retrieve the latest value for that key. If not  | 
103 | 113 |                 provided, it retrieves the latest value received overall.  | 
 | 114 | +            default: The default value to return if no value has been received yet for  | 
 | 115 | +                the specified key. If not provided, it defaults to `None`.  | 
104 | 116 | 
  | 
105 | 117 |         Returns:  | 
106 | 118 |             The latest value that has been received.  | 
 | 119 | +        """  | 
 | 120 | +        return self._latest_value_by_key.get(key, default)  | 
 | 121 | + | 
 | 122 | +    @override  | 
 | 123 | +    def __iter__(self) -> Iterator[HashableT]:  | 
 | 124 | +        """Return an iterator over the keys for which values have been received."""  | 
 | 125 | +        return iter(self._latest_value_by_key)  | 
 | 126 | + | 
 | 127 | +    @override  | 
 | 128 | +    def __len__(self) -> int:  | 
 | 129 | +        """Return the number of keys for which values have been received."""  | 
 | 130 | +        return len(self._latest_value_by_key)  | 
 | 131 | + | 
 | 132 | +    @override  | 
 | 133 | +    def __getitem__(self, key: HashableT) -> T_co:  | 
 | 134 | +        """Return the latest value that has been received for a specific key.  | 
 | 135 | +
  | 
 | 136 | +        Args:  | 
 | 137 | +            key: The key to retrieve the latest value for.  | 
 | 138 | +
  | 
 | 139 | +        Returns:  | 
 | 140 | +            The latest value that has been received for that key.  | 
107 | 141 | 
  | 
108 | 142 |         Raises:  | 
109 |  | -            ValueError: If no value has been received yet.  | 
 | 143 | +            KeyError: If no value has been received yet for that key.  | 
110 | 144 |         """  | 
111 | 145 |         if key not in self._latest_value_by_key:  | 
112 |  | -            raise ValueError(f"No value received for key: {key!r}")  | 
 | 146 | +            raise KeyError(f"No value received for key: {key!r}")  | 
113 | 147 |         return self._latest_value_by_key[key]  | 
114 | 148 | 
 
  | 
115 | 149 |     def has_value(self, key: HashableT) -> bool:  | 
 | 
0 commit comments