Skip to content

Commit d35c2a8

Browse files
fix(cache): cast cached values to correct type in cache retrieval
- Cast cached values to the expected type to ensure type safety - Improve handling of cache retrieval in cold miss scenarios
1 parent 412d4d1 commit d35c2a8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

api/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import logging
1111
import time
1212
from collections.abc import Awaitable, Callable
13-
from typing import Any, TypeVar
13+
from typing import Any, TypeVar, cast
1414

1515
from cachetools import TTLCache
1616

@@ -223,14 +223,14 @@ async def get_or_set_cache(
223223
age = cache_age(key)
224224
if age is not None and age > refresh_after:
225225
_schedule_refresh(key, refresh_factory or factory)
226-
return cached
226+
return cast(T, cached)
227227

228228
# Cold miss — must await. Lock prevents stampede.
229229
async with _get_lock(key):
230230
# Double-check after acquiring lock
231231
cached = get_cache(key)
232232
if cached is not None:
233-
return cached
233+
return cast(T, cached)
234234
result = await factory()
235235
set_cache(key, result)
236236
return result

0 commit comments

Comments
 (0)