Skip to content

Commit 16681da

Browse files
fix: Address CodeRabbit PR feedback
- Add explicit list[str] type annotations to fix pyright errors - Fix max-length/prefix enforcement in CharacterSanitizationStrategy - Add truncation when add_hash is False - Ensure S_ prefix when sanitization changes input - Use local variables to narrow types for None checks - Add noqa for "too many statements" warning Co-authored-by: William Easton <[email protected]>
1 parent aa6cf2e commit 16681da

File tree

3 files changed

+57
-39
lines changed
  • key-value
    • key-value-aio/src/key_value/aio/wrappers/sanitization_migration
    • key-value-shared/src/key_value/shared/utils
    • key-value-sync/src/key_value/sync/code_gen/wrappers/sanitization_migration

3 files changed

+57
-39
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async def get(self, key: str, *, collection: str | None = None) -> dict[str, Any
183183
return result
184184

185185
@override
186-
async def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[dict[str, Any] | None]: # noqa: PLR0912
186+
async def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[dict[str, Any] | None]: # noqa: PLR0912, PLR0915
187187
"""Get multiple values, falling back to legacy store for missing keys.
188188
189189
Args:
@@ -194,10 +194,10 @@ async def get_many(self, keys: Sequence[str], *, collection: str | None = None)
194194
List of values (None for missing keys).
195195
"""
196196
# Separate keys by cached location
197-
current_keys = []
198-
legacy_keys = []
199-
missing_keys = []
200-
unknown_keys = []
197+
current_keys: list[str] = []
198+
legacy_keys: list[str] = []
199+
missing_keys: list[str] = []
200+
unknown_keys: list[str] = []
201201

202202
for key in keys:
203203
cached_location = self._cache_get(key, collection)
@@ -211,7 +211,7 @@ async def get_many(self, keys: Sequence[str], *, collection: str | None = None)
211211
unknown_keys.append(key)
212212

213213
# Start with all None
214-
key_to_value: dict[str, dict[str, Any] | None] = dict.fromkeys(keys, None)
214+
key_to_value: dict[str, dict[str, Any] | None] = dict.fromkeys(keys, None) # type: ignore[arg-type]
215215

216216
# Fetch known current keys
217217
if current_keys:
@@ -223,12 +223,13 @@ async def get_many(self, keys: Sequence[str], *, collection: str | None = None)
223223
if legacy_keys:
224224
legacy_results = await self.legacy_store.get_many(keys=legacy_keys, collection=collection)
225225
for i, key in enumerate(legacy_keys):
226-
key_to_value[key] = legacy_results[i]
226+
legacy_value = legacy_results[i]
227+
key_to_value[key] = legacy_value
227228

228229
# Optionally migrate
229-
if self.migrate_on_read and legacy_results[i] is not None:
230+
if self.migrate_on_read and legacy_value is not None:
230231
_, ttl = await self.legacy_store.ttl(key=key, collection=collection)
231-
await self.current_store.put(key=key, value=legacy_results[i], collection=collection, ttl=ttl)
232+
await self.current_store.put(key=key, value=legacy_value, collection=collection, ttl=ttl)
232233
if self.delete_after_migration:
233234
await self.legacy_store.delete(key=key, collection=collection)
234235
self._cache_put(key, collection, "current")
@@ -238,7 +239,7 @@ async def get_many(self, keys: Sequence[str], *, collection: str | None = None)
238239
current_results = await self.current_store.get_many(keys=unknown_keys, collection=collection)
239240

240241
# Identify which unknown keys were not found in current
241-
not_in_current = []
242+
not_in_current: list[str] = []
242243
for i, key in enumerate(unknown_keys):
243244
if current_results[i] is not None:
244245
key_to_value[key] = current_results[i]
@@ -250,13 +251,14 @@ async def get_many(self, keys: Sequence[str], *, collection: str | None = None)
250251
if not_in_current:
251252
legacy_results = await self.legacy_store.get_many(keys=not_in_current, collection=collection)
252253
for i, key in enumerate(not_in_current):
253-
if legacy_results[i] is not None:
254-
key_to_value[key] = legacy_results[i]
254+
legacy_value = legacy_results[i]
255+
if legacy_value is not None:
256+
key_to_value[key] = legacy_value
255257

256258
# Optionally migrate
257259
if self.migrate_on_read:
258260
_, ttl = await self.legacy_store.ttl(key=key, collection=collection)
259-
await self.current_store.put(key=key, value=legacy_results[i], collection=collection, ttl=ttl)
261+
await self.current_store.put(key=key, value=legacy_value, collection=collection, ttl=ttl)
260262
if self.delete_after_migration:
261263
await self.legacy_store.delete(key=key, collection=collection)
262264
self._cache_put(key, collection, "current")
@@ -324,10 +326,10 @@ async def ttl_many(self, keys: Sequence[str], *, collection: str | None = None)
324326
List of (value, ttl) tuples.
325327
"""
326328
# Similar logic to get_many but with TTL
327-
current_keys = []
328-
legacy_keys = []
329-
missing_keys = []
330-
unknown_keys = []
329+
current_keys: list[str] = []
330+
legacy_keys: list[str] = []
331+
missing_keys: list[str] = []
332+
unknown_keys: list[str] = []
331333

332334
for key in keys:
333335
cached_location = self._cache_get(key, collection)
@@ -340,7 +342,7 @@ async def ttl_many(self, keys: Sequence[str], *, collection: str | None = None)
340342
else:
341343
unknown_keys.append(key)
342344

343-
key_to_value: dict[str, tuple[dict[str, Any] | None, float | None]] = dict.fromkeys(keys, (None, None)) # type: ignore
345+
key_to_value: dict[str, tuple[dict[str, Any] | None, float | None]] = dict.fromkeys(keys, (None, None))
344346

345347
# Fetch from current
346348
if current_keys:
@@ -365,7 +367,7 @@ async def ttl_many(self, keys: Sequence[str], *, collection: str | None = None)
365367
if unknown_keys:
366368
current_results = await self.current_store.ttl_many(keys=unknown_keys, collection=collection)
367369

368-
not_in_current = []
370+
not_in_current: list[str] = []
369371
for i, key in enumerate(unknown_keys):
370372
if current_results[i][0] is not None:
371373
key_to_value[key] = current_results[i]

key-value/key-value-shared/src/key_value/shared/utils/sanitization_strategy.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,21 @@ def sanitize(self, value: str) -> str:
229229
# Add prefix, hash fragment, and return
230230
return f"S_{sanitized}-{hash_fragment}"
231231

232-
# No hash needed - return sanitized value (or original if unchanged)
232+
# No hash needed - but still need to enforce max_length and prefix if changed
233+
needs_truncation = len(sanitized) > self.max_length
234+
if needs_truncation:
235+
sanitized = sanitized[: self.max_length]
236+
changed = True
237+
238+
if changed:
239+
# Add S_ prefix since we modified the input
240+
prefixed = f"S_{sanitized}"
241+
# Ensure the prefixed result fits within max_length
242+
if len(prefixed) > self.max_length:
243+
sanitized = sanitized[: self.max_length - 2]
244+
prefixed = f"S_{sanitized}"
245+
return prefixed
246+
233247
return sanitized
234248

235249
def validate(self, value: str) -> None:

key-value/key-value-sync/src/key_value/sync/code_gen/wrappers/sanitization_migration/wrapper.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def get(self, key: str, *, collection: str | None = None) -> dict[str, Any] | No
186186
return result
187187

188188
@override
189-
def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[dict[str, Any] | None]: # noqa: PLR0912
189+
def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> list[dict[str, Any] | None]: # noqa: PLR0912, PLR0915
190190
"""Get multiple values, falling back to legacy store for missing keys.
191191
192192
Args:
@@ -197,10 +197,10 @@ def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
197197
List of values (None for missing keys).
198198
"""
199199
# Separate keys by cached location
200-
current_keys = []
201-
legacy_keys = []
202-
missing_keys = []
203-
unknown_keys = []
200+
current_keys: list[str] = []
201+
legacy_keys: list[str] = []
202+
missing_keys: list[str] = []
203+
unknown_keys: list[str] = []
204204

205205
for key in keys:
206206
cached_location = self._cache_get(key, collection)
@@ -214,7 +214,7 @@ def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
214214
unknown_keys.append(key)
215215

216216
# Start with all None
217-
key_to_value: dict[str, dict[str, Any] | None] = dict.fromkeys(keys, None)
217+
key_to_value: dict[str, dict[str, Any] | None] = dict.fromkeys(keys, None) # type: ignore[arg-type]
218218

219219
# Fetch known current keys
220220
if current_keys:
@@ -226,12 +226,13 @@ def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
226226
if legacy_keys:
227227
legacy_results = self.legacy_store.get_many(keys=legacy_keys, collection=collection)
228228
for i, key in enumerate(legacy_keys):
229-
key_to_value[key] = legacy_results[i]
229+
legacy_value = legacy_results[i]
230+
key_to_value[key] = legacy_value
230231

231232
# Optionally migrate
232-
if self.migrate_on_read and legacy_results[i] is not None:
233+
if self.migrate_on_read and legacy_value is not None:
233234
(_, ttl) = self.legacy_store.ttl(key=key, collection=collection)
234-
self.current_store.put(key=key, value=legacy_results[i], collection=collection, ttl=ttl)
235+
self.current_store.put(key=key, value=legacy_value, collection=collection, ttl=ttl)
235236
if self.delete_after_migration:
236237
self.legacy_store.delete(key=key, collection=collection)
237238
self._cache_put(key, collection, "current")
@@ -241,7 +242,7 @@ def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
241242
current_results = self.current_store.get_many(keys=unknown_keys, collection=collection)
242243

243244
# Identify which unknown keys were not found in current
244-
not_in_current = []
245+
not_in_current: list[str] = []
245246
for i, key in enumerate(unknown_keys):
246247
if current_results[i] is not None:
247248
key_to_value[key] = current_results[i]
@@ -253,13 +254,14 @@ def get_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
253254
if not_in_current:
254255
legacy_results = self.legacy_store.get_many(keys=not_in_current, collection=collection)
255256
for i, key in enumerate(not_in_current):
256-
if legacy_results[i] is not None:
257-
key_to_value[key] = legacy_results[i]
257+
legacy_value = legacy_results[i]
258+
if legacy_value is not None:
259+
key_to_value[key] = legacy_value
258260

259261
# Optionally migrate
260262
if self.migrate_on_read:
261263
(_, ttl) = self.legacy_store.ttl(key=key, collection=collection)
262-
self.current_store.put(key=key, value=legacy_results[i], collection=collection, ttl=ttl)
264+
self.current_store.put(key=key, value=legacy_value, collection=collection, ttl=ttl)
263265
if self.delete_after_migration:
264266
self.legacy_store.delete(key=key, collection=collection)
265267
self._cache_put(key, collection, "current")
@@ -327,10 +329,10 @@ def ttl_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
327329
List of (value, ttl) tuples.
328330
"""
329331
# Similar logic to get_many but with TTL
330-
current_keys = []
331-
legacy_keys = []
332-
missing_keys = []
333-
unknown_keys = []
332+
current_keys: list[str] = []
333+
legacy_keys: list[str] = []
334+
missing_keys: list[str] = []
335+
unknown_keys: list[str] = []
334336

335337
for key in keys:
336338
cached_location = self._cache_get(key, collection)
@@ -343,7 +345,7 @@ def ttl_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
343345
else:
344346
unknown_keys.append(key)
345347

346-
key_to_value: dict[str, tuple[dict[str, Any] | None, float | None]] = dict.fromkeys(keys, (None, None)) # type: ignore
348+
key_to_value: dict[str, tuple[dict[str, Any] | None, float | None]] = dict.fromkeys(keys, (None, None))
347349

348350
# Fetch from current
349351
if current_keys:
@@ -368,7 +370,7 @@ def ttl_many(self, keys: Sequence[str], *, collection: str | None = None) -> lis
368370
if unknown_keys:
369371
current_results = self.current_store.ttl_many(keys=unknown_keys, collection=collection)
370372

371-
not_in_current = []
373+
not_in_current: list[str] = []
372374
for i, key in enumerate(unknown_keys):
373375
if current_results[i][0] is not None:
374376
key_to_value[key] = current_results[i]

0 commit comments

Comments
 (0)