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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: variable kwargs did not invalidate cache (#5613)
Dylan Madisetti979e6a9b4dab
diff --git a/marimo/_save/save.py b/marimo/_save/save.pyindex f17dd48899..e64cf1b31d 100644--- a/marimo/_save/save.py+++ b/marimo/_save/save.py@@ -74,6 +74,8 @@ class _cache_call:cell_id: strmodule: ast.Module_args: list[str]+ _var_arg: Optional[str] = None+ _var_kwarg: Optional[str] = None_loader: Optional[State[Loader]] = None_loader_partial: LoaderPartialname: str@@ -116,7 +118,33 @@ class _cache_call:)self.fn = fn- self._args = list(self.fn.__code__.co_varnames)+ sig = inspect.signature(fn)+ self._args = [+ param.name+ for param in sig.parameters.values()+ if param.kind+ in (+ inspect.Parameter.POSITIONAL_OR_KEYWORD,+ inspect.Parameter.POSITIONAL_ONLY,+ )+ ]+ self._var_arg = next(+ (+ param.name+ for param in sig.parameters.values()+ if param.kind == inspect.Parameter.VAR_POSITIONAL+ ),+ None,+ )+ self._var_kwarg = next(+ (+ param.name+ for param in sig.parameters.values()+ if param.kind == inspect.Parameter.VAR_KEYWORD+ ),+ None,+ )+# Retrieving frame from the stack: frame is## 0 _set_context ->@@ -194,6 +222,13 @@ class _cache_call:# Rewrite scoped args to prevent shadowed variablesarg_dict = {f"{ARG_PREFIX}{k}": v for (k, v) in zip(self._args, args)}kwargs_copy = {f"{ARG_PREFIX}{k}": v for (k, v) in kwargs.items()}+ # If the function has varargs, we need to capture them as well.+ if self._var_arg is not None:+ arg_dict[f"{ARG_PREFIX}{self._var_arg}"] = args[len(self._args) :]+ if self._var_kwarg is not None:+ # NB: kwargs are always a dict, so we can just copy them.+ arg_dict[f"{ARG_PREFIX}{self._var_kwarg}"] = kwargs.copy()+# Capture the call casectx = get_context()scope = {diff --git a/tests/_save/test_cache.py b/tests/_save/test_cache.pyindex c9634a932d..8d5c646b3a 100644--- a/tests/_save/test_cache.py+++ b/tests/_save/test_cache.py@@ -859,6 +859,442 @@ class TestCacheDecorator:assert k.globals["a"] == 5assert k.globals["b"] == 55+ async def test_cache_decorator_with_kwargs(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def my_cached_func(*args, **kwargs):+ return sum(args) + sum(kwargs.values())++ # First call with specific kwargs+ result1 = my_cached_func(1, 2, some_kw_arg=3)+ hash1 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Second call with different kwargs - should be cache miss+ result2 = my_cached_func(1, 2, some_kw_arg=4)+ hash2 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Third call with same kwargs as first - should be cache hit+ result3 = my_cached_func(1, 2, some_kw_arg=3)+ hash3 = my_cached_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 6 # 1 + 2 + 3+ assert k.globals["result2"] == 7 # 1 + 2 + 4+ assert k.globals["result3"] == 6 # 1 + 2 + 3++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, "Cache key should change when kwargs change"+ assert hash1 == hash3, (+ "Cache key should be same for identical args/kwargs"+ )++ # Verify cache hits+ assert k.globals["my_cached_func"].hits == 1++ async def test_cache_decorator_kwargs_expansion(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def my_cached_func(*args, **kwargs):+ return sum(args) + sum(kwargs.values())++ # Test with kwargs expansion+ _kw = {"some_kw_arg": 1}+ result1 = my_cached_func(1, 2, **_kw)+ hash1 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different kwargs expansion+ _kw = {"some_kw_arg": 2}+ result2 = my_cached_func(1, 2, **_kw)+ hash2 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same kwargs expansion - should hit cache+ _kw = {"some_kw_arg": 1}+ result3 = my_cached_func(1, 2, **_kw)+ hash3 = my_cached_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 4 # 1 + 2 + 1+ assert k.globals["result2"] == 5 # 1 + 2 + 2+ assert k.globals["result3"] == 4 # 1 + 2 + 1++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, (+ "Cache key should change when kwargs expansion changes"+ )+ assert hash1 == hash3, (+ "Cache key should be same for identical kwargs expansion"+ )++ # Verify cache hits+ assert k.globals["my_cached_func"].hits == 1++ async def test_cache_decorator_varargs(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def my_cached_func(*args, **kwargs):+ return sum(args) + sum(kwargs.values())++ # Test with different varargs+ result1 = my_cached_func(1, 2, 3)+ hash1 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different varargs - should be cache miss+ result2 = my_cached_func(1, 2, 4)+ hash2 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same varargs - should hit cache+ result3 = my_cached_func(1, 2, 3)+ hash3 = my_cached_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 6 # 1 + 2 + 3+ assert k.globals["result2"] == 7 # 1 + 2 + 4+ assert k.globals["result3"] == 6 # 1 + 2 + 3++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, "Cache key should change when varargs change"+ assert hash1 == hash3, "Cache key should be same for identical varargs"++ # Verify cache hits+ assert k.globals["my_cached_func"].hits == 1++ async def test_cache_decorator_varargs_expansion(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def my_cached_func(*args, **kwargs):+ return sum(args) + sum(kwargs.values())++ # Test with varargs expansion+ _args = [1, 2, 3]+ result1 = my_cached_func(*_args)+ hash1 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different varargs expansion+ _args = [1, 2, 4]+ result2 = my_cached_func(*_args)+ hash2 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same varargs expansion - should hit cache+ _args = [1, 2, 3]+ result3 = my_cached_func(*_args)+ hash3 = my_cached_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 6 # 1 + 2 + 3+ assert k.globals["result2"] == 7 # 1 + 2 + 4+ assert k.globals["result3"] == 6 # 1 + 2 + 3++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, (+ "Cache key should change when varargs expansion changes"+ )+ assert hash1 == hash3, (+ "Cache key should be same for identical varargs expansion"+ )++ # Verify cache hits+ assert k.globals["my_cached_func"].hits == 1++ async def test_cache_decorator_varargs_count(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def my_cached_func(*args, **kwargs):+ return sum(args) + sum(kwargs.values())++ _args1 = [1, 2, 3]+ result1 = my_cached_func(*_args1)+ hash1 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ _args2 = [1, 2]+ result2 = my_cached_func(*_args2)+ hash2 = my_cached_func._last_hash+ """+ ),+ exec_req.get(+ """+ _args1 = [1, 2, 3, 4]+ result3 = my_cached_func(*_args1)+ hash3 = my_cached_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 6 # 1 + 2 + 3+ assert k.globals["result2"] == 3 # 1 + 2+ assert k.globals["result3"] == 10 # 1 + 2 + 3 + 4++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ # The cache key should be the same for identical values regardless of variable name+ assert len({hash1, hash2, hash3}) == 3, (+ "Cache key should be same for identical values with different variable names"+ )++ # Verify cache hits+ assert k.globals["my_cached_func"].hits == 0++ async def test_persistent_cache_decorator_mixed_signature(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ # Sanity check that the same code path is captured.+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import persistent_cache+ from marimo._save.loaders import MemoryLoader++ @persistent_cache(_loader=MemoryLoader)+ def mixed_func(arg, *vargs, kw=None, **kwargs):+ return arg + sum(vargs) + (kw or 0) + sum(kwargs.values())++ # Test with mixed arguments+ result1 = mixed_func(1, 2, 3, kw=4, extra=5)+ hash1 = mixed_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different mixed arguments - should be cache miss+ result2 = mixed_func(1, 2, 3, kw=4, extra=7)+ hash2 = mixed_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same mixed arguments - should hit cache+ result3 = mixed_func(1, 2, 3, kw=4, extra=5)+ hash3 = mixed_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 15 # 1 + 2 + 3 + 4 + 5+ assert k.globals["result2"] == 17 # 1 + 2 + 3 + 4 + 7+ assert k.globals["result3"] == 15 # 1 + 2 + 3 + 4 + 5++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, "Cache key should change when kwargs change"+ assert hash1 == hash3, (+ "Cache key should be same for identical mixed arguments"+ )++ # Verify cache hits+ assert k.globals["mixed_func"].hits == 1++ async def test_cache_decorator_mixed_signature(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def mixed_func(arg, *vargs, kw=None, **kwargs):+ return arg + sum(vargs) + (kw or 0) + sum(kwargs.values())++ # Test with mixed arguments+ result1 = mixed_func(1, 2, 3, kw=4, extra=5)+ hash1 = mixed_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different mixed arguments - should be cache miss+ result2 = mixed_func(1, 2, 3, kw=4, extra=7)+ hash2 = mixed_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same mixed arguments - should hit cache+ result3 = mixed_func(1, 2, 3, kw=4, extra=5)+ hash3 = mixed_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 15 # 1 + 2 + 3 + 4 + 5+ assert k.globals["result2"] == 17 # 1 + 2 + 3 + 4 + 7+ assert k.globals["result3"] == 15 # 1 + 2 + 3 + 4 + 5++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, "Cache key should change when kwargs change"+ assert hash1 == hash3, (+ "Cache key should be same for identical mixed arguments"+ )++ # Verify cache hits+ assert k.globals["mixed_func"].hits == 1++ async def test_cache_decorator_positional_only(+ self, k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ await k.run(+ [+ exec_req.get(+ """+ from marimo._save.save import cache++ @cache+ def pos_only_func(pos1, pos2, /, kw1=None, *, kwonly):+ return pos1 + pos2 + (kw1 or 0) + kwonly++ # Test with positional-only arguments+ result1 = pos_only_func(1, 2, kw1=3, kwonly=4)+ hash1 = pos_only_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with different positional arguments - should be cache miss+ result2 = pos_only_func(1, 3, kw1=3, kwonly=4)+ hash2 = pos_only_func._last_hash+ """+ ),+ exec_req.get(+ """+ # Test with same arguments - should hit cache+ result3 = pos_only_func(1, 2, kw1=3, kwonly=4)+ hash3 = pos_only_func._last_hash+ """+ ),+ ]+ )++ # Verify results+ assert k.globals["result1"] == 10 # 1 + 2 + 3 + 4+ assert k.globals["result2"] == 11 # 1 + 3 + 3 + 4+ assert k.globals["result3"] == 10 # 1 + 2 + 3 + 4++ # Verify cache keys+ hash1 = k.globals["hash1"]+ hash2 = k.globals["hash2"]+ hash3 = k.globals["hash3"]++ assert hash1 != hash2, (+ "Cache key should change when positional arguments change"+ )+ assert hash1 == hash3, (+ "Cache key should be same for identical arguments"+ )++ # Verify cache hits+ assert k.globals["pos_only_func"].hits == 1+async def test_cross_cell_cache(self, k: Kernel, exec_req: ExecReqProvider) -> None: