Skip to content

Commit b29d2e9

Browse files
refactor: switch input parameters from dict to Mapping
Switch all input parameters from `dict[str, Any]` to `Mapping[str, Any]` while keeping return types as `dict[str, Any]`. This follows the Liskov Substitution Principle - accept more general types as inputs, return more specific types as outputs. This change improves the API's flexibility by accepting any mapping-like object (dict, OrderedDict, custom mappings, etc.) as input while maintaining backward compatibility. All existing code continues to work since dict is a subclass of Mapping. Changes: - Updated AsyncKeyValueProtocol with Mapping for put() and put_many() parameters - Updated ManagedEntry.value field to accept Mapping[str, Any] - Updated BaseStore and all wrapper implementations - Updated adapters (RaiseOnMissingAdapter) - Added dict() conversions where needed (encryption, compression, limit_size wrappers) Note: Sync library will need to be regenerated from the async version. Fixes #94 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: William Easton <strawgate@users.noreply.github.com>
1 parent e6eccbb commit b29d2e9

File tree

19 files changed

+65
-64
lines changed

19 files changed

+65
-64
lines changed

key-value/key-value-aio/src/key_value/aio/adapters/raise_on_missing/adapter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, Literal, SupportsFloat, overload
33

44
from key_value.shared.errors import MissingKeyError
@@ -138,7 +138,7 @@ async def ttl_many(
138138

139139
return results
140140

141-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
141+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
142142
"""Store a key-value pair in the specified collection with optional TTL.
143143
144144
Args:
@@ -153,7 +153,7 @@ async def put(self, key: str, value: dict[str, Any], *, collection: str | None =
153153
async def put_many(
154154
self,
155155
keys: list[str],
156-
values: Sequence[dict[str, Any]],
156+
values: Sequence[Mapping[str, Any]],
157157
*,
158158
collection: str | None = None,
159159
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/protocols/key_value.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, Protocol, SupportsFloat, runtime_checkable
33

44

@@ -36,7 +36,7 @@ async def ttl(self, key: str, *, collection: str | None = None) -> tuple[dict[st
3636
"""
3737
...
3838

39-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
39+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
4040
"""Store a key-value pair in the specified collection with optional TTL.
4141
4242
Args:
@@ -85,7 +85,7 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
8585
async def put_many(
8686
self,
8787
keys: list[str],
88-
values: Sequence[dict[str, Any]],
88+
values: Sequence[Mapping[str, Any]],
8989
*,
9090
collection: str | None = None,
9191
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/stores/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from abc import ABC, abstractmethod
66
from asyncio.locks import Lock
77
from collections import defaultdict
8-
from collections.abc import Sequence
8+
from collections.abc import Mapping, Sequence
99
from types import TracebackType
1010
from typing import Any, SupportsFloat
1111

@@ -190,7 +190,7 @@ async def _put_managed_entries(self, *, collection: str, keys: list[str], manage
190190
)
191191

192192
@override
193-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
193+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
194194
"""Store a key-value pair in the specified collection with optional TTL."""
195195
collection = collection or self.default_collection
196196
await self.setup_collection(collection=collection)
@@ -204,8 +204,8 @@ async def put(self, key: str, value: dict[str, Any], *, collection: str | None =
204204
)
205205

206206
def _prepare_put_many(
207-
self, *, keys: list[str], values: Sequence[dict[str, Any]], ttl: Sequence[SupportsFloat | None] | SupportsFloat | None
208-
) -> tuple[list[str], Sequence[dict[str, Any]], list[float | None]]:
207+
self, *, keys: list[str], values: Sequence[Mapping[str, Any]], ttl: Sequence[SupportsFloat | None] | SupportsFloat | None
208+
) -> tuple[list[str], Sequence[Mapping[str, Any]], list[float | None]]:
209209
"""Prepare multiple managed entries for a put_many operation.
210210
211211
Inheriting classes can use this method if they need to modify a put_many operation."""
@@ -226,7 +226,7 @@ def _prepare_put_many(
226226
async def put_many(
227227
self,
228228
keys: list[str],
229-
values: Sequence[dict[str, Any]],
229+
values: Sequence[Mapping[str, Any]],
230230
*,
231231
collection: str | None = None,
232232
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/wrappers/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, SupportsFloat
33

44
from typing_extensions import override
@@ -28,14 +28,14 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
2828
return await self.key_value.ttl_many(collection=collection, keys=keys)
2929

3030
@override
31-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
31+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
3232
return await self.key_value.put(collection=collection, key=key, value=value, ttl=ttl)
3333

3434
@override
3535
async def put_many(
3636
self,
3737
keys: list[str],
38-
values: Sequence[dict[str, Any]],
38+
values: Sequence[Mapping[str, Any]],
3939
*,
4040
collection: str | None = None,
4141
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/wrappers/compression/wrapper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import gzip
33
import json
4-
from collections.abc import Sequence
4+
from collections.abc import Mapping, Sequence
55
from typing import Any, SupportsFloat
66

77
from key_value.shared.utils.managed_entry import ManagedEntry
@@ -131,18 +131,18 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
131131
return [(self._decompress_value(value), ttl) for value, ttl in results]
132132

133133
@override
134-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
135-
compressed_value = self._compress_value(value)
134+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
135+
compressed_value = self._compress_value(dict(value))
136136
return await self.key_value.put(key=key, value=compressed_value, collection=collection, ttl=ttl)
137137

138138
@override
139139
async def put_many(
140140
self,
141141
keys: list[str],
142-
values: Sequence[dict[str, Any]],
142+
values: Sequence[Mapping[str, Any]],
143143
*,
144144
collection: str | None = None,
145145
ttl: Sequence[SupportsFloat | None] | None = None,
146146
) -> None:
147-
compressed_values = [self._compress_value(value) for value in values]
147+
compressed_values = [self._compress_value(dict(value)) for value in values]
148148
return await self.key_value.put_many(keys=keys, values=compressed_values, collection=collection, ttl=ttl)

key-value/key-value-aio/src/key_value/aio/wrappers/encryption/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
import json
3-
from collections.abc import Callable, Sequence
3+
from collections.abc import Callable, Mapping, Sequence
44
from typing import Any, SupportsFloat
55

66
from key_value.shared.errors.key_value import SerializationError
@@ -158,18 +158,18 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
158158
return [(self._decrypt_value(value), ttl) for value, ttl in results]
159159

160160
@override
161-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
162-
encrypted_value = self._encrypt_value(value)
161+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
162+
encrypted_value = self._encrypt_value(dict(value))
163163
return await self.key_value.put(key=key, value=encrypted_value, collection=collection, ttl=ttl)
164164

165165
@override
166166
async def put_many(
167167
self,
168168
keys: list[str],
169-
values: Sequence[dict[str, Any]],
169+
values: Sequence[Mapping[str, Any]],
170170
*,
171171
collection: str | None = None,
172172
ttl: Sequence[SupportsFloat | None] | None = None,
173173
) -> None:
174-
encrypted_values = [self._encrypt_value(value) for value in values]
174+
encrypted_values = [self._encrypt_value(dict(value)) for value in values]
175175
return await self.key_value.put_many(keys=keys, values=encrypted_values, collection=collection, ttl=ttl)

key-value/key-value-aio/src/key_value/aio/wrappers/fallback/wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, SupportsFloat
33

44
from typing_extensions import override
@@ -71,7 +71,7 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
7171
return await self.fallback_key_value.ttl_many(keys=keys, collection=collection)
7272

7373
@override
74-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
74+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
7575
if self.write_to_fallback:
7676
try:
7777
return await self.primary_key_value.put(key=key, value=value, collection=collection, ttl=ttl)
@@ -84,7 +84,7 @@ async def put(self, key: str, value: dict[str, Any], *, collection: str | None =
8484
async def put_many(
8585
self,
8686
keys: list[str],
87-
values: Sequence[dict[str, Any]],
87+
values: Sequence[Mapping[str, Any]],
8888
*,
8989
collection: str | None = None,
9090
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/wrappers/limit_size/wrapper.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, SupportsFloat
33

44
from key_value.shared.errors.wrappers.limit_size import EntryTooLargeError, EntryTooSmallError
@@ -80,28 +80,28 @@ def _within_size_limit(self, value: dict[str, Any], *, collection: str | None =
8080
return True
8181

8282
@override
83-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
84-
if self._within_size_limit(value=value, collection=collection, key=key):
83+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
84+
if self._within_size_limit(value=dict(value), collection=collection, key=key):
8585
await self.key_value.put(collection=collection, key=key, value=value, ttl=ttl)
8686

8787
@override
8888
async def put_many(
8989
self,
9090
keys: list[str],
91-
values: Sequence[dict[str, Any]],
91+
values: Sequence[Mapping[str, Any]],
9292
*,
9393
collection: str | None = None,
9494
ttl: Sequence[SupportsFloat | None] | None = None,
9595
) -> None:
9696
filtered_keys: list[str] = []
97-
filtered_values: list[dict[str, Any]] = []
97+
filtered_values: list[Mapping[str, Any]] = []
9898
filtered_ttls: list[SupportsFloat | None] | None = None
9999

100100
if isinstance(ttl, Sequence):
101101
filtered_ttls = []
102102

103103
for i, (k, v) in enumerate(zip(keys, values, strict=True)):
104-
if self._within_size_limit(value=v, collection=collection, key=k):
104+
if self._within_size_limit(value=dict(v), collection=collection, key=k):
105105
filtered_keys.append(k)
106106
filtered_values.append(v)
107107
if isinstance(ttl, Sequence):

key-value/key-value-aio/src/key_value/aio/wrappers/logging/wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import logging
3-
from collections.abc import Sequence
3+
from collections.abc import Mapping, Sequence
44
from typing import Any, Literal, SupportsFloat
55

66
from typing_extensions import override
@@ -146,7 +146,7 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
146146
return results
147147

148148
@override
149-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
149+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
150150
self._log(state="start", action="PUT", keys=key, collection=collection, values=value, extra={"ttl": ttl})
151151

152152
await self.key_value.put(key=key, value=value, collection=collection, ttl=ttl)
@@ -157,7 +157,7 @@ async def put(self, key: str, value: dict[str, Any], *, collection: str | None =
157157
async def put_many(
158158
self,
159159
keys: list[str],
160-
values: Sequence[dict[str, Any]],
160+
values: Sequence[Mapping[str, Any]],
161161
*,
162162
collection: str | None = None,
163163
ttl: Sequence[SupportsFloat | None] | None = None,

key-value/key-value-aio/src/key_value/aio/wrappers/passthrough_cache/wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Mapping, Sequence
22
from typing import Any, SupportsFloat
33

44
from typing_extensions import override
@@ -155,7 +155,7 @@ async def ttl_many(self, keys: list[str], *, collection: str | None = None) -> l
155155
return [key_to_value[key] for key in keys]
156156

157157
@override
158-
async def put(self, key: str, value: dict[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
158+
async def put(self, key: str, value: Mapping[str, Any], *, collection: str | None = None, ttl: SupportsFloat | None = None) -> None:
159159
_ = await self.cache_key_value.delete(collection=collection, key=key)
160160

161161
await self.primary_key_value.put(collection=collection, key=key, value=value, ttl=ttl)
@@ -164,7 +164,7 @@ async def put(self, key: str, value: dict[str, Any], *, collection: str | None =
164164
async def put_many(
165165
self,
166166
keys: list[str],
167-
values: Sequence[dict[str, Any]],
167+
values: Sequence[Mapping[str, Any]],
168168
*,
169169
collection: str | None = None,
170170
ttl: Sequence[SupportsFloat | None] | None = None,

0 commit comments

Comments
 (0)