From f6821c2b68154b78408a4914ab841c0d6a5522ac Mon Sep 17 00:00:00 2001 From: Nathan Jessurun Date: Sun, 5 Jan 2025 08:59:25 -0600 Subject: [PATCH 1/2] Fixes incorrect type hints on memoize `key` #958 --- solara/cache.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/solara/cache.py b/solara/cache.py index 5ef4fd148..676c0c198 100644 --- a/solara/cache.py +++ b/solara/cache.py @@ -7,6 +7,7 @@ Callable, Dict, Generic, + Hashable, MutableMapping, Optional, TypeVar, @@ -64,7 +65,7 @@ def _default_key(*args, **kwargs): class MemoizedFunction(Generic[P, R]): - def __init__(self, function: Callable[P, R], key: Callable[P, R], storage: Optional[Storage], allow_nonlocals=False): + def __init__(self, function: Callable[P, R], key: Callable[P, Hashable], storage: Optional[Storage], allow_nonlocals=False): self.function = function f: Callable = self.function if not allow_nonlocals: @@ -170,7 +171,7 @@ def memoize( @overload def memoize( function: None = None, - key: Callable[P, R] = ..., + key: Callable[P, Hashable] = ..., storage: Optional[Storage] = None, allow_nonlocals=False, ) -> Callable[[Callable[P, R]], MemoizedFunction[P, R]]: ... @@ -187,7 +188,7 @@ def memoize( def memoize( function: Union[None, Callable[P, R]] = None, - key: Union[None, Callable[P, R]] = None, + key: Union[None, Callable[P, Hashable]] = None, storage: Optional[Storage] = None, allow_nonlocals: bool = False, ) -> Union[Callable[[Callable[P, R]], MemoizedFunction[P, R]], MemoizedFunction[P, R]]: @@ -249,7 +250,7 @@ def mean(df, column): def wrapper(func: Callable[P, R]) -> MemoizedFunction[P, R]: return MemoizedFunction[P, R]( func, - cast(Callable[P, R], key or _default_key), + cast(Callable[P, Hashable], key or _default_key), storage, allow_nonlocals, ) From e91e678fa2b285c8ad741392b3f684d705441527 Mon Sep 17 00:00:00 2001 From: Nathan Jessurun Date: Wed, 22 Jan 2025 18:44:42 -0600 Subject: [PATCH 2/2] hashable now comes from collections.abc --- solara/cache.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solara/cache.py b/solara/cache.py index 676c0c198..105406767 100644 --- a/solara/cache.py +++ b/solara/cache.py @@ -1,3 +1,4 @@ +from collections.abc import Hashable import hashlib import inspect import logging @@ -7,7 +8,6 @@ Callable, Dict, Generic, - Hashable, MutableMapping, Optional, TypeVar, @@ -51,6 +51,7 @@ class Memory(cachetools.LRUCache): def __init__(self, max_items=solara.settings.cache.memory_max_items): super().__init__(maxsize=max_items) + else: class Memory(dict): # type: ignore