Skip to content

Commit d13ff16

Browse files
refactor: extract collection guard pattern into helper method
Extract repeated collection validation logic into a private helper method _get_collection_or_raise() to reduce code duplication in MemoryStore. - Created _get_collection_or_raise() helper with proper docstring - Refactored 4 methods to use the helper: - _get_managed_entry - _put_managed_entry - _delete_managed_entry - _get_collection_keys - Reduces 20 lines of duplicated code - Maintains identical behavior and error messages Addresses CodeRabbit review feedback. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: William Easton <[email protected]>
1 parent 419dc68 commit d13ff16

File tree

1 file changed

+19
-21
lines changed
  • key-value/key-value-aio/src/key_value/aio/stores/memory

1 file changed

+19
-21
lines changed

key-value/key-value-aio/src/key_value/aio/stores/memory/store.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,27 @@ async def _setup_collection(self, *, collection: str) -> None:
153153
collection_cache = MemoryCollection(max_entries=self.max_entries_per_collection)
154154
self._cache[collection] = collection_cache
155155

156-
@override
157-
async def _get_managed_entry(self, *, key: str, collection: str) -> ManagedEntry | None:
158-
collection_cache: MemoryCollection | None = self._cache.get(collection)
156+
def _get_collection_or_raise(self, collection: str) -> MemoryCollection:
157+
"""Get a collection or raise KeyError if not setup.
158+
159+
Args:
160+
collection: The collection name.
159161
162+
Returns:
163+
The MemoryCollection instance.
164+
165+
Raises:
166+
KeyError: If the collection has not been setup via setup_collection().
167+
"""
168+
collection_cache: MemoryCollection | None = self._cache.get(collection)
160169
if collection_cache is None:
161170
msg = f"Collection '{collection}' has not been setup. Call setup_collection() first."
162171
raise KeyError(msg)
172+
return collection_cache
163173

174+
@override
175+
async def _get_managed_entry(self, *, key: str, collection: str) -> ManagedEntry | None:
176+
collection_cache = self._get_collection_or_raise(collection)
164177
return collection_cache.get(key=key)
165178

166179
@override
@@ -171,32 +184,17 @@ async def _put_managed_entry(
171184
collection: str,
172185
managed_entry: ManagedEntry,
173186
) -> None:
174-
collection_cache: MemoryCollection | None = self._cache.get(collection)
175-
176-
if collection_cache is None:
177-
msg = f"Collection '{collection}' has not been setup. Call setup_collection() first."
178-
raise KeyError(msg)
179-
187+
collection_cache = self._get_collection_or_raise(collection)
180188
collection_cache.put(key=key, value=managed_entry)
181189

182190
@override
183191
async def _delete_managed_entry(self, *, key: str, collection: str) -> bool:
184-
collection_cache: MemoryCollection | None = self._cache.get(collection)
185-
186-
if collection_cache is None:
187-
msg = f"Collection '{collection}' has not been setup. Call setup_collection() first."
188-
raise KeyError(msg)
189-
192+
collection_cache = self._get_collection_or_raise(collection)
190193
return collection_cache.delete(key=key)
191194

192195
@override
193196
async def _get_collection_keys(self, *, collection: str, limit: int | None = None) -> list[str]:
194-
collection_cache: MemoryCollection | None = self._cache.get(collection)
195-
196-
if collection_cache is None:
197-
msg = f"Collection '{collection}' has not been setup. Call setup_collection() first."
198-
raise KeyError(msg)
199-
197+
collection_cache = self._get_collection_or_raise(collection)
200198
return collection_cache.keys(limit=limit)
201199

202200
@override

0 commit comments

Comments
 (0)