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 Python callback scheduling types (#11487)

Shahmir Varqha5cfa101e379e
diff --git a/marimo/_utils/debounce.py b/marimo/_utils/debounce.py
index 7c9fb33ecb..25dee5ffce 100644
--- a/marimo/_utils/debounce.py
+++ b/marimo/_utils/debounce.py
@@ -2,30 +2,34 @@
from __future__ import annotations
import time
-from collections.abc import Callable
from functools import wraps
-from typing import Any, TypeVar, cast
+from typing import TYPE_CHECKING, ParamSpec
-F = TypeVar("F", bound=Callable[..., None])
+if TYPE_CHECKING:
+ from collections.abc import Callable
+P = ParamSpec("P")
-def debounce(wait_time: float) -> Callable[[F], F]:
+
+def debounce(
+ wait_time: float,
+) -> Callable[[Callable[P, None]], Callable[P, None]]:
"""
Decorator to prevent a function from being called more than once every
wait_time seconds.
"""
- def decorator(func: F) -> F:
+ def decorator(func: Callable[P, None]) -> Callable[P, None]:
last_called: float = 0
@wraps(func)
- def wrapped(*args: Any, **kwargs: Any) -> None:
+ def wrapped(*args: P.args, **kwargs: P.kwargs) -> None:
nonlocal last_called
current_time = time.time()
if current_time - last_called >= wait_time:
last_called = current_time
func(*args, **kwargs)
- return cast(F, wrapped)
+ return wrapped
return decorator
diff --git a/marimo/_utils/distributor.py b/marimo/_utils/distributor.py
index 1c4d27977d..068c0b1563 100644
--- a/marimo/_utils/distributor.py
+++ b/marimo/_utils/distributor.py
@@ -5,7 +5,7 @@ import asyncio
import threading
import time
from collections.abc import Callable
-from typing import Any, Generic, Protocol, TypeVar
+from typing import Generic, Protocol, TypeVar
from marimo import _loggers
from marimo._session.queue import QueueType
@@ -27,7 +27,7 @@ class Distributor(Protocol, Generic[T_co]):
"""Add a consumer to the distributor."""
...
- def start(self) -> Any:
+ def start(self) -> Disposable | threading.Thread:
"""Start the distributor."""
...