Skip to content

Commit a5cbad3

Browse files
committed
Rename clear() to __delitem__
This follows the `dict` interface more closely, so it should be more familiar to users. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent d5d42c7 commit a5cbad3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/frequenz/channels/experimental/_grouping_latest_value_cache.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class GroupingLatestValueCache(Mapping[HashableT, ValueT_co]):
3737
a [`MutableMapping`][collections.abc.MutableMapping] because users can't mutate the
3838
cache directly, it is only mutated by the underlying receiver. There is one exception
3939
though, users can clear individual keys from the cache using the
40-
[clear][frequenz.channels.experimental.GroupingLatestValueCache.clear] method.
40+
[__delitem__][frequenz.channels.experimental.GroupingLatestValueCache.__delitem__]
41+
method.
4142
4243
Example:
4344
```python
@@ -57,6 +58,11 @@ class GroupingLatestValueCache(Mapping[HashableT, ValueT_co]):
5758
assert 6 in cache
5859
assert cache.get(6) == (6, "twenty-six")
5960
61+
del cache[6]
62+
63+
assert cache.get(6) is None
64+
assert 6 not in cache
65+
6066
await cache.stop()
6167
```
6268
"""
@@ -206,13 +212,13 @@ def __ne__(self, value: object, /) -> bool:
206212
return NotImplemented
207213
return self._latest_value_by_key != value._latest_value_by_key
208214

209-
def clear(self, key: HashableT) -> None:
215+
def __delitem__(self, key: HashableT) -> None:
210216
"""Clear the latest value for a specific key.
211217
212218
Args:
213219
key: The key for which to clear the latest value.
214220
"""
215-
_ = self._latest_value_by_key.pop(key, None)
221+
del self._latest_value_by_key[key]
216222

217223
async def stop(self) -> None:
218224
"""Stop the cache."""

tests/test_grouping_latest_value_cache_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def test_latest_value_cache_key() -> None:
5959

6060
assert cache.keys() == {5, 6, 12}
6161

62-
cache.clear(5)
62+
del cache[5]
6363
assert 5 not in cache
6464
assert 6 in cache
6565

0 commit comments

Comments
 (0)