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: decorator invalidation for caching (#8171)
Dylan Madisettia02f8a06a06b
diff --git a/marimo/_ast/transformers.py b/marimo/_ast/transformers.pyindex 3e0b487ab5..a573de5d09 100644--- a/marimo/_ast/transformers.py+++ b/marimo/_ast/transformers.py@@ -81,7 +81,20 @@ def clean_to_modules(return (compiled_ast(pre_block), compiled_ast(block.body))-def strip_function(fn: Callable[..., Any]) -> ast.Module:+def _get_decorator_name(node: ast.expr) -> Optional[str]:+ """Extract the name from a decorator node."""+ if isinstance(node, ast.Call):+ node = node.func+ if isinstance(node, ast.Name):+ return node.id+ if isinstance(node, ast.Attribute):+ return node.attr+ return None+++def get_hashable_ast(+ fn: Callable[..., Any], skip_decorators: Optional[set[str]] = None+) -> ast.Module:code, _ = inspect.getsourcelines(fn)args = set(fn.__code__.co_varnames)function_ast = ast_parse(textwrap.dedent("".join(code)))@@ -89,7 +102,29 @@ def strip_function(fn: Callable[..., Any]) -> ast.Module:assert isinstance(body, (ast.FunctionDef, ast.AsyncFunctionDef)), ("Expected a function definition")- extracted = ast.Module(body.body, type_ignores=[])++ # Insert decorator calls as expression statements so they affect the hash.+ # This ensures changes to decorator parameters invalidate the cache.+ # Optionally skip specific decorators (e.g., @cache to avoid self-reference).+ # Only decorators AFTER (inner to) the skipped one should affect the hash.+ # In Python's AST, decorator_list is ordered outermost to innermost:+ # @outer # decorator_list[0] - should NOT affect hash+ # @cache # decorator_list[1] - skip this+ # @inner # decorator_list[2] - SHOULD affect hash+ # def fn(): ...+ decorator_stmts: list[ast.stmt] = []+ skip_idx = 0+ if skip_decorators:+ for i, dec in enumerate(body.decorator_list):+ if _get_decorator_name(dec) in skip_decorators:+ skip_idx = i + 1 # start from the index after+ break+ decorator_stmts = [+ ast.Expr(value=dec) for dec in body.decorator_list[skip_idx:]+ ]++ extracted = ast.Module(decorator_stmts + body.body, type_ignores=[])+ ast.fix_missing_locations(extracted)module = RemoveReturns().visit(extracted)module = MangleArguments(args).visit(module)assert isinstance(module, ast.Module), "Expected a module"diff --git a/marimo/_save/hash.py b/marimo/_save/hash.pyindex 4ee620e27b..5a05dfd068 100644--- a/marimo/_save/hash.py+++ b/marimo/_save/hash.py@@ -11,7 +11,7 @@ import sysimport typesfrom typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional-from marimo._ast.transformers import DeprivateVisitor, strip_function+from marimo._ast.transformers import DeprivateVisitor, get_hashable_astfrom marimo._ast.variables import (get_cell_from_local,if_local_then_mangle,@@ -141,7 +141,7 @@ def hash_function(fn: Callable[..., Any], hash_type: str = DEFAULT_HASH) -> bytes:return hash_raw_module(- DeprivateVisitor().visit(strip_function(fn)), hash_type+ DeprivateVisitor().visit(get_hashable_ast(fn)), hash_type)diff --git a/marimo/_save/save.py b/marimo/_save/save.pyindex d13516ee54..3c9ee9645c 100644--- a/marimo/_save/save.py+++ b/marimo/_save/save.py@@ -31,7 +31,7 @@ from marimo._ast.load import find_cellfrom marimo._ast.transformers import (ARG_PREFIX,CacheExtractWithBlock,- strip_function,+ get_hashable_ast,)from marimo._ast.variables import is_mangled_local, unmangle_localfrom marimo._messaging.tracebacks import write_traceback@@ -245,7 +245,9 @@ class _cache_call(CacheContext):def _build_base_block(self, fn: Callable[..., Any], graph: DirectedGraph, cell_id: CellId_t) -> BlockHasher:- module = strip_function(fn)+ module = get_hashable_ast(+ fn, skip_decorators={"cache", "persistent_cache"}+ )return BlockHasher(module=module,diff --git a/tests/_ast/test_transformers.py b/tests/_ast/test_transformers.pyindex 5ca8bb295b..c26cdc209f 100644--- a/tests/_ast/test_transformers.py+++ b/tests/_ast/test_transformers.py@@ -17,7 +17,7 @@ from marimo._ast.transformers import (RemoveReturns,clean_to_modules,compiled_ast,- strip_function,+ get_hashable_ast,)@@ -253,14 +253,14 @@ def test_clean_to_modules_no_optional_vars() -> None:assert len(with_module.body) == 1-def test_strip_function() -> None:- """Test the strip_function function."""+def test_get_hashable_ast() -> None:+ """Test the get_hashable_ast function."""def test_function(x: int, y: str) -> int:result = x + len(y)return result- module = strip_function(test_function)+ module = get_hashable_ast(test_function)assert isinstance(module, ast.Module)# The function should be stripped of returns and arguments mangled@@ -271,6 +271,49 @@ def test_strip_function() -> None:assert f"{ARG_PREFIX}y" in code+def test_get_hashable_ast_with_decorators() -> None:+ """Test that get_hashable_ast includes decorator AST and can skip specific ones.++ When skip_decorator is provided, that decorator is filtered out while+ all others are included so their parameters affect the hash.+ """++ def my_cache(fn):+ return fn++ def other_decorator(param: str):+ del param++ def wrapper(fn):+ return fn++ return wrapper++ @other_decorator(param="before")+ @my_cache+ @other_decorator(param="after")+ def decorated_function():+ return 42++ # Test 1: With skip_decorators, that decorator is filtered out+ module = get_hashable_ast(decorated_function, skip_decorators={"my_cache"})+ code = ast.unparse(module)++ assert "my_cache" not in code+ assert "other_decorator" in code+ # Only decorators AFTER (inner to) the skipped one should be included+ assert 'param="before"' not in code+ assert "param='before'" not in code+ assert 'param="after"' in code or "param='after'" in code++ # Test 2: Without skip_decorator, all decorators are included+ module_all = get_hashable_ast(decorated_function)+ code_all = ast.unparse(module_all)++ assert "my_cache" in code_all+ assert "other_decorator" in code_all++def test_mangle_arguments() -> None:"""Test the MangleArguments transformer."""code = """diff --git a/tests/_save/test_hash.py b/tests/_save/test_hash.pyindex aaccb7bfcb..de9f3bb069 100644--- a/tests/_save/test_hash.py+++ b/tests/_save/test_hash.py@@ -2282,3 +2282,157 @@ va_hits = varargs.hitsassert k.globals["va_hits"] == 1, (f"Expected 1 hit, got {k.globals['va_hits']}")+++def test_decorator_params_affect_hash() -> None:+ """Verify that changing decorator parameters changes the function hash.++ When using @mo.cache with nested decorators like:+ @mo.cache+ @spl(schema={"a": "int"})+ def query(): ...++ Changing the schema should invalidate the cache.+ """+ from marimo._ast.transformers import get_hashable_ast+ from marimo._save.hash import hash_raw_module++ def cache(fn):+ return fn++ def decorator_with_param(param: str):+ del param++ def wrapper(fn):+ return fn++ return wrapper++ # Helper to hash with cache skipped (mimics mo.cache behavior)+ def hash_fn(fn):+ return hash_raw_module(get_hashable_ast(fn, skip_decorators={"cache"}))++ # Test 1: Decorator AFTER @cache - param change should affect hash+ @cache+ @decorator_with_param(param="value1")+ def fn():+ return 42++ hash1 = hash_fn(fn)++ @cache+ @decorator_with_param(param="value2")+ def fn():+ return 42++ hash2 = hash_fn(fn)++ assert hash1 != hash2, (+ "Decorator param change AFTER @cache should invalidate hash"+ )++ # Test 2: Decorator BEFORE @cache - param change should NOT affect hash+ @decorator_with_param(param="value1")+ @cache+ def fn():+ return 42++ hash3 = hash_fn(fn)++ @decorator_with_param(param="value2")+ @cache+ def fn():+ return 42++ hash4 = hash_fn(fn)++ assert hash3 == hash4, (+ "Decorator param change BEFORE @cache should NOT affect hash"+ )+++async def test_decorator_ref_tracking_e2e(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ """End-to-end test that refs in decorator params affect cache behavior.++ When a decorator parameter references a variable:+ schema = {"a": "int"}+ @mo.cache+ @my_decorator(schema=schema)+ def query(): ...++ Changing the variable's value should produce different cache hashes.+ """++ cell_id = "test_cell"++ # First execution with schema_config = "v1"+ await k.run(+ [+ exec_req.get_with_id(+ cell_id,+ """+import marimo as mo++schema_config = "v1"++def my_decorator(schema):+ def wrapper(fn):+ fn._schema = schema+ return fn+ return wrapper++@my_decorator(schema=schema_config)+def cached_query():+ return "result"++result = cached_query()+hash_val = cached_query._last_hash+ """,+ )+ ]+ )++ assert not k.stderr.messages, k.stderr+ assert k.globals["result"] == "result"+ first_hash = k.globals["hash_val"]++ # Second execution with schema_config = "v2"+ await k.run(+ [+ exec_req.get_with_id(+ cell_id,+ """+import marimo as mo++schema_config = "v2"++def my_decorator(schema):+ def wrapper(fn):+ fn._schema = schema+ return fn+ return wrapper++@my_decorator(schema=schema_config)+def cached_query():+ return "result"++result = cached_query()+hash_val = cached_query._last_hash+ """,+ )+ ]+ )++ assert not k.stderr.messages, k.stderr+ assert k.globals["result"] == "result"+ second_hash = k.globals["hash_val"]++ # The hashes should be different because the decorator param changed+ assert first_hash != second_hash, (+ f"Expected different hashes when decorator ref changes, "+ f"got {first_hash} == {second_hash}"+ )