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.

fix: preserve utility decorator type signatures (#11313)

Shahmir Varqhac88dfcc58e32
diff --git a/marimo/_utils/memoize.py b/marimo/_utils/memoize.py
index cd81d5cb39..5d37ccdf35 100644
--- a/marimo/_utils/memoize.py
+++ b/marimo/_utils/memoize.py
@@ -1,27 +1,28 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, Any, TypeVar, cast
+from typing import TYPE_CHECKING, ParamSpec, TypeVar, cast
if TYPE_CHECKING:
from collections.abc import Callable
T = TypeVar("T")
+P = ParamSpec("P")
sentinel = object() # Unique sentinel object
-def memoize_last_value(func: Callable[..., T]) -> Callable[..., T]:
+def memoize_last_value(func: Callable[P, T]) -> Callable[P, T]:
"""
This differs from functools.lru_cache in that is checks for
object identity for positional arguments instead of equality
which for functools requires the arguments to be hashable.
"""
- last_args: tuple[Any, ...] = ()
- last_kwargs: dict[str, Any] = {}
+ last_args: tuple[object, ...] = ()
+ last_kwargs: dict[str, object] = {}
last_output: T = cast(T, sentinel)
- def wrapper(*args: Any, **kwargs: Any) -> T:
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
nonlocal last_args, last_kwargs, last_output
if last_output is not sentinel:
diff --git a/marimo/_utils/timer.py b/marimo/_utils/timer.py
index 380b99747d..9ab5f7ed7f 100644
--- a/marimo/_utils/timer.py
+++ b/marimo/_utils/timer.py
@@ -3,13 +3,16 @@ from __future__ import annotations
import functools
import time
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING, ParamSpec, TypeVar
if TYPE_CHECKING:
from collections.abc import Callable
+P = ParamSpec("P")
+T = TypeVar("T")
-def timer(func: Callable[..., Any]) -> Callable[..., Any]:
+
+def timer(func: Callable[P, T]) -> Callable[P, T]:
"""
A decorator that measures and prints the execution time of a function.
@@ -17,7 +20,7 @@ def timer(func: Callable[..., Any]) -> Callable[..., Any]:
"""
@functools.wraps(func)
- def wrapper(*args: Any, **kwargs: Any) -> Any:
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()