Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: refine type inference for cache decorators (#8629)

Daizu3bc54515db92
diff --git a/marimo/_save/save.py b/marimo/_save/save.py
index e5309fa601..a079216e47 100644
--- a/marimo/_save/save.py
+++ b/marimo/_save/save.py
@@ -20,6 +20,7 @@ from typing import (
TYPE_CHECKING,
Any,
Callable,
+ Concatenate,
Generic,
Optional,
ParamSpec,
@@ -67,13 +68,18 @@ from marimo._types.ids import CellId_t
from marimo._utils.with_skip import SkipContext
if TYPE_CHECKING:
+ from collections.abc import Coroutine
from types import FrameType, TracebackType
+ from typing_extensions import Self
+
from marimo._runtime.dataflow import DirectedGraph
from marimo._save.stores import Store
P = ParamSpec("P")
R = TypeVar("R")
+Q = ParamSpec("Q")
+S = TypeVar("S")
class _cache_call(CacheContext, Generic[P, R]):
@@ -376,9 +382,19 @@ class _cache_call(CacheContext, Generic[P, R]):
"""Return the last computed hash for this cache call."""
return self._last_hash
+ @overload
+ def __get__(
+ self, instance: None, owner: Optional[type] = None
+ ) -> Self: ...
+
+ @overload
def __get__(
- self, instance: Any, _owner: Optional[type] = None
- ) -> _cache_call[P, R]:
+ self: _cache_call[Concatenate[Any, Q], R],
+ instance: Any,
+ owner: Optional[type] = None,
+ ) -> _cache_call[Q, R]: ...
+
+ def __get__(self, instance: Any, _owner: Optional[type] = None) -> Any: # type: ignore[misc]
"""Enable @cache as a method decorator.
__get__ is invoked on instance access;
@@ -420,7 +436,23 @@ class _cache_call(CacheContext, Generic[P, R]):
return copy
return self
- def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
+ @overload
+ def __call__(
+ self, fn: Callable[Q, Coroutine[Any, Any, S]]
+ ) -> _cache_call_async[Q, S]: ...
+
+ @overload
+ def __call__(
+ self,
+ fn: Callable[Q, S],
+ ) -> _cache_call[Q, S]: ...
+
+ @overload
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
+
+ def __call__(
+ self, *args: Any, **kwargs: Any
+ ) -> _cache_call[Any, Any] | _cache_call_async[Any, Any] | R:
# Capture the deferred call case
if self.__wrapped__ is None:
if len(args) != 1:
@@ -491,6 +523,21 @@ class _cache_call_async(_cache_call[P, R]):
] = weakref.WeakKeyDictionary()
_pending_lock = threading.Lock()
+ @overload
+ def __get__(
+ self, instance: None, owner: Optional[type] = None
+ ) -> Self: ...
+
+ @overload
+ def __get__(
+ self: _cache_call_async[Concatenate[Any, Q], R],
+ instance: Any,
+ owner: Optional[type] = None,
+ ) -> _cache_call_async[Q, R]: ...
+
+ def __get__(self, instance: Any, _owner: Optional[type] = None) -> Any: # type: ignore[misc]
+ return super().__get__(instance, _owner)
+
async def __call__( # type: ignore[override]
self, *args: P.args, **kwargs: P.kwargs
) -> R:
@@ -895,6 +942,14 @@ def _invoke_context(
return _cache_context(name, loader, *args, **kwargs)
+@overload
+def cache(
+ fn: Callable[P, Coroutine[Any, Any, R]],
+ pin_modules: bool = False,
+ loader: LoaderPartial | LoaderType = MemoryLoader,
+) -> _cache_call_async[P, R]: ...
+
+
@overload
def cache(
fn: Callable[P, R],
@@ -908,7 +963,7 @@ def cache(
fn: None = None,
pin_modules: bool = False,
loader: LoaderPartial | LoaderType = MemoryLoader,
-) -> Callable[[Callable[P, R]], _cache_call[P, R]]: ...
+) -> _cache_call[Any, Any]: ...
@overload
@@ -927,7 +982,7 @@ def cache( # type: ignore[misc]
_frame_offset: int = 1,
_internal_interface_not_for_external_use: None = None,
**kwargs: Any,
-) -> Union[_cache_call[..., Any], _cache_context]:
+) -> Union[_cache_call[Any, Any], _cache_call_async[Any, Any], _cache_context]:
"""## Cache the value of a function based on args and closed-over variables.
Decorating a function with `@mo.cache` will cache its value based on
@@ -1012,6 +1067,14 @@ def cache( # type: ignore[misc]
)
+@overload
+def lru_cache(
+ fn: Callable[P, Coroutine[Any, Any, R]],
+ maxsize: int = 128,
+ pin_modules: bool = False,
+) -> _cache_call_async[P, R]: ...
+
+
@overload
def lru_cache(
fn: Callable[P, R],
@@ -1025,7 +1088,7 @@ def lru_cache(
fn: None = None,
maxsize: int = 128,
pin_modules: bool = False,
-) -> Callable[[Callable[P, R]], _cache_call[P, R]]: ...
+) -> _cache_call[Any, Any]: ...
@overload
@@ -1043,7 +1106,7 @@ def lru_cache( # type: ignore[misc]
pin_modules: bool = False,
_internal_interface_not_for_external_use: None = None,
**kwargs: Any,
-) -> Union[_cache_call[..., Any], _cache_context]:
+) -> Union[_cache_call[Any, Any], _cache_call_async[Any, Any], _cache_context]:
"""Decorator for LRU caching the return value of a function.
`mo.lru_cache` is a version of `mo.cache` with a bounded cache size. As an
@@ -1088,7 +1151,9 @@ def lru_cache( # type: ignore[misc]
)
return cast(
- Union[_cache_call[..., Any], _cache_context],
+ Union[
+ _cache_call[Any, Any], _cache_call_async[Any, Any], _cache_context
+ ],
cache( # type: ignore[call-overload]
arg,
*args,
@@ -1102,20 +1167,38 @@ def lru_cache( # type: ignore[misc]
@overload
def persistent_cache(
- name: str,
+ fn: Callable[P, Coroutine[Any, Any, R]],
save_path: str | None = None,
method: LoaderKey = "pickle",
pin_modules: bool = False,
-) -> _cache_context: ...
+) -> _cache_call_async[P, R]: ...
@overload
def persistent_cache(
- fn: Optional[Callable[..., Any]] = None,
+ fn: Callable[P, R],
save_path: str | None = None,
method: LoaderKey = "pickle",
pin_modules: bool = False,
-) -> _cache_call[..., Any]: ...
+) -> _cache_call[P, R]: ...
+
+
+@overload
+def persistent_cache(
+ fn: None = None,
+ save_path: str | None = None,
+ method: LoaderKey = "pickle",
+ pin_modules: bool = False,
+) -> _cache_call[Any, Any]: ...
+
+
+@overload
+def persistent_cache(
+ name: str,
+ save_path: str | None = None,
+ method: LoaderKey = "pickle",
+ pin_modules: bool = False,
+) -> _cache_context: ...
def persistent_cache( # type: ignore[misc]
@@ -1128,7 +1211,7 @@ def persistent_cache( # type: ignore[misc]
pin_modules: bool = False,
_internal_interface_not_for_external_use: None = None,
**kwargs: Any,
-) -> Union[_cache_call[..., Any], _cache_context]:
+) -> Union[_cache_call[Any, Any], _cache_call_async[Any, Any], _cache_context]:
"""## Context manager to save variables to disk and restore them thereafter.
The `mo.persistent_cache` context manager lets you delimit a block of code
@@ -1237,7 +1320,9 @@ def persistent_cache( # type: ignore[misc]
raise TypeError("Do not use fn directly, use positional arguments.")
return cast(
- Union[_cache_call[..., Any], _cache_context],
+ Union[
+ _cache_call[Any, Any], _cache_call_async[Any, Any], _cache_context
+ ],
cache( # type: ignore[call-overload]
arg,
*args,
diff --git a/tests/_ast/test_app_typing.py b/tests/_ast/test_app_typing.py
index ba8e9c125c..995919543c 100644
--- a/tests/_ast/test_app_typing.py
+++ b/tests/_ast/test_app_typing.py
@@ -41,11 +41,8 @@ def _check_pyright(code: str) -> None:
_PREAMBLE = """
- from typing_extensions import TypeGuard, TypeIs, assert_type
+ from typing_extensions import assert_type
import marimo as mo
-
- def _int_or_str() -> int | str:
- return 0
"""
@@ -103,12 +100,17 @@ class TestAppFunctionTyping:
""")
[email protected](params=["cache", "lru_cache", "persistent_cache"])
+def cache_func(request: pytest.FixtureRequest) -> str:
+ return request.param
+
+
class TestCacheTyping:
- def test_cache_preserves_return_type(self) -> None:
+ def test_cache_preserves_return_type(self, cache_func: str) -> None:
_check_pyright(
_PREAMBLE
- + """
- @mo.cache
+ + f"""
+ @mo.{cache_func}
def compute(x: int) -> int:
return x * 2
@@ -116,26 +118,67 @@ class TestCacheTyping:
"""
)
- def test_cache_exposes_cache_info(self) -> None:
+ def test_cache_async_preserves_return_type(self, cache_func: str) -> None:
_check_pyright(
_PREAMBLE
- + """
- @mo.cache
- def compute(x: int) -> int:
+ + f"""
+ @mo.{cache_func}
+ async def compute(x: int) -> int:
return x * 2
- info = compute.cache_info()
- compute.cache_clear()
+ async def _main(): # pyright: ignore[reportUnusedFunction]
+ assert_type(await compute(1), int)
"""
)
+ def test_cache_method_preserves_return_type(self, cache_func: str) -> None:
+ _check_pyright(
+ _PREAMBLE
+ + f"""
+ class A:
+ @mo.{cache_func}
+ def method(self, x: int) -> int:
+ return x
+
+ assert_type(A().method(1), int)
+"""
+ )
+
+ def test_cache_async_method_preserves_return_type(
+ self, cache_func: str
+ ) -> None:
+ _check_pyright(
+ _PREAMBLE
+ + f"""
+ class A:
+ @mo.{cache_func}
+ async def method(self, x: int) -> int:
+ return x
+
+ async def _main(): # pyright: ignore[reportUnusedFunction]
+ assert_type(await A().method(1), int)
+"""
+ )
+
+ def test_cache_exposes_cache_info(self) -> None:
+ _check_pyright("""
+ import marimo as mo
+
+ @mo.cache
+ def compute(x: int) -> int:
+ return x * 2
+
+ _info = compute.cache_info()
+ compute.cache_clear()
+ """)
+
class TestLruCacheTyping:
- def test_lru_cache_preserves_return_type(self) -> None:
+ def test_lru_cache_parameterized_preserves_return_type(self) -> None:
_check_pyright(
_PREAMBLE
+ """
- @mo.lru_cache
+ @mo.lru_cache(maxsize=128)
def compute(x: int) -> int:
return x * 2
@@ -144,14 +187,42 @@ class TestLruCacheTyping:
)
def test_lru_cache_exposes_cache_info(self) -> None:
+ _check_pyright("""
+ import marimo as mo
+
+ @mo.lru_cache
+ def compute(x: int) -> int:
+ return x * 2
+
+ _info = compute.cache_info()
+ compute.cache_clear()
+ """)
+
+
+class TestPersistentCacheTyping:
+ def test_persistent_cache_parameterized_preserves_return_type(
+ self,
+ ) -> None:
_check_pyright(
_PREAMBLE
+ """
- @mo.lru_cache
+ @mo.persistent_cache(save_path="cache")
def compute(x: int) -> int:
return x * 2
- info = compute.cache_info()
- compute.cache_clear()
+ assert_type(compute(1), int)
+"""
+ )
+
+
+class TestCacheContext:
+ def test_cache_context_return_type(self, cache_func: str) -> None:
+ _check_pyright(
+ _PREAMBLE
+ + f"""
+ from marimo._save.save import (
+ _cache_context, # pyright: ignore[reportPrivateUsage]
+ )
+ assert_type(mo.{cache_func}("cache"), _cache_context)
"""
)