fix(cache): satisfy strict typing budget

This commit is contained in:
Oxygenesis 2026-09-29 23:13:38 +07:00
parent da8769284b
commit 332e2ece44
2 changed files with 15 additions and 2 deletions

View file

@ -210,7 +210,7 @@ class InMemoryCache(BaseCache):
return False
@staticmethod
def _copy_cached_value(value: Any) -> Any:
def _copy_cached_value(value: object) -> object:
"""Return a read-isolated value without making the cache brittle.
In-memory cache entries can contain Pydantic models and mutable
@ -227,7 +227,7 @@ class InMemoryCache(BaseCache):
except Exception: # noqa: BLE001 - cache reads must tolerate non-copyable values
return value
def _get_cache_value(self, key, *, copy_value: bool) -> Any:
def _get_cache_value(self, key, *, copy_value: bool) -> object:
if key in self.cache_dict:
if self.evict_element_if_expired(key):
return None

View file

@ -79,6 +79,19 @@ def test_in_memory_cache_returns_read_isolated_pydantic_models():
assert stored.metadata == {"region": "us"}
def test_in_memory_cache_preserves_non_copyable_mutable_values():
cache = InMemoryCache()
cache.set_cache("payload", ["unchanged"])
with patch(
"litellm.caching.in_memory_cache.copy.deepcopy",
side_effect=RuntimeError("not copyable"),
):
cached = cache.get_cache("payload")
assert cached is cache.cache_dict["payload"]
def test_in_memory_openai_obj_cache():
from openai import OpenAI