diff --git a/litellm/caching/in_memory_cache.py b/litellm/caching/in_memory_cache.py index 2aa2b868600..ca3d3546d72 100644 --- a/litellm/caching/in_memory_cache.py +++ b/litellm/caching/in_memory_cache.py @@ -15,7 +15,7 @@ import sys import threading import time from collections.abc import Callable -from typing import TYPE_CHECKING, Any, Final +from typing import TYPE_CHECKING, Any, Final, TypeVar, cast if TYPE_CHECKING: from litellm.types.caching import RedisPipelineIncrementOperation @@ -27,6 +27,7 @@ from litellm.constants import MAX_SIZE_PER_ITEM_IN_MEMORY_CACHE_IN_KB from .base_cache import BaseCache DEFAULT_MAX_SIZE_IN_MEMORY: Final = 200 +_T = TypeVar("_T") class InMemoryCache(BaseCache): @@ -210,7 +211,7 @@ class InMemoryCache(BaseCache): return False @staticmethod - def _copy_cached_value(value: object) -> object: + def _copy_cached_value(value: _T) -> _T: """Return a read-isolated value without making the cache brittle. In-memory cache entries can contain Pydantic models and mutable @@ -223,11 +224,11 @@ class InMemoryCache(BaseCache): if not isinstance(value, (dict, list, set, tuple, frozenset, bytearray, BaseModel)): return value try: - return copy.deepcopy(value) + return cast(_T, copy.deepcopy(value)) except Exception: # noqa: BLE001 - cache reads must tolerate non-copyable values return value - def _get_cache_value(self, key, *, copy_value: bool) -> object: + def _get_cache_value(self, key, *, copy_value: bool): if key in self.cache_dict: if self.evict_element_if_expired(key): return None