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: resolve decorators on parse correctly (preventing dropped decorators) (#7119)

Dylan Madisetti2f24091e6577
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index 4655305326..5c1ed7bb3a 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -580,7 +580,7 @@ def cell_factory(
extractor = parse.Extractor(contents=function_code)
func_ast = parse.ast_parse(function_code).body[0]
- cell_def = extractor.to_cell(func_ast, attribute="cell")
+ cell_def = extractor.to_cell(func_ast, attribute="cell").unwrap()
# anonymous file is required for deterministic testing.
source_position = None
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index e23622e86b..9cda622364 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -136,7 +136,9 @@ class Extractor:
):
# We may have a decorator between cell decorator and function.
# This is invalid serialization, but still possible.
- if is_cell_decorator(decorator, allowed=("cell",)):
+ if len(node.decorator_list) > 1 and is_cell_decorator(
+ decorator, allowed=("cell",)
+ ):
# We just take the last decorator in this case, which will
# be removed on serialization.
violations.append(
@@ -291,31 +293,37 @@ class Extractor:
violations=violations,
)
- def to_setup_cell(self, node: Node) -> SetupCell:
+ def to_setup_cell(self, node: Node) -> ParseResult[SetupCell]:
kwargs, _violations = _maybe_kwargs(node.items[0].context_expr) # type: ignore
code_result = self.extract_from_code(node)
_violations.extend(code_result.violations)
code = fixed_dedent(code_result.unwrap())
if code.endswith("\npass"):
code = code[: -len("\npass")]
- return SetupCell(
- code=code,
- options=kwargs,
- lineno=node.lineno,
- col_offset=node.col_offset,
- end_lineno=max(node.lineno, _none_to_0(node.end_lineno)),
- end_col_offset=_none_to_0(node.end_col_offset),
- name=SETUP_CELL_NAME,
+ return ParseResult(
+ SetupCell(
+ code=code,
+ options=kwargs,
+ lineno=node.lineno,
+ col_offset=node.col_offset,
+ end_lineno=max(node.lineno, _none_to_0(node.end_lineno)),
+ end_col_offset=_none_to_0(node.end_col_offset),
+ name=SETUP_CELL_NAME,
+ ),
+ violations=_violations,
)
- def to_cell(self, node: Node, attribute: Optional[str] = None) -> CellDef:
+ def to_cell(
+ self, node: Node, attribute: Optional[str] = None
+ ) -> ParseResult[CellDef]:
"""Convert an AST node to a CellDef."""
- # TODO: Handle violations
+ violations: list[Violation] = []
if isinstance(
node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)
):
decorator = get_valid_decorator(node)
kwargs, _violations = _maybe_kwargs(decorator)
+ violations.extend(_violations)
if attribute is None and decorator is not None:
if isinstance(decorator, ast.Call):
if not hasattr(decorator.func, "attr"):
@@ -332,7 +340,8 @@ class Extractor:
node, (ast.FunctionDef, ast.AsyncFunctionDef)
), "@app.cell cannot be used on classes."
cell_result = self.to_cell_def(node, kwargs)
- return cell_result.unwrap()
+ violations.extend(cell_result.violations)
+ return ParseResult(cell_result.unwrap(), violations=violations)
cell_types: dict[Optional[str], type[CellDef]] = {
"function": FunctionCell,
"class_definition": ClassCell,
@@ -340,10 +349,14 @@ class Extractor:
cell_type = cell_types.get(attribute, None)
if cell_type is not None:
code = self.extract_from_code(node)
- return cell_type(
- code=code.unwrap(),
- _ast=node,
- options=kwargs,
+ violations.extend(code.violations)
+ return ParseResult(
+ cell_type(
+ code=code.unwrap(),
+ _ast=node,
+ options=kwargs,
+ ),
+ violations=violations,
)
raise MarimoFileError(f"Unsupported cell type. {attribute}")
@@ -351,10 +364,14 @@ class Extractor:
# These are all captured by is_unparsable_cell
# but mypy is struggling.
kwargs, _violations = _eval_kwargs(node.value.keywords) # type: ignore
- return UnparsableCell(
- code=node.value.args[0].value, # type: ignore
- options=kwargs,
- _ast=node,
+ violations.extend(_violations)
+ return ParseResult(
+ UnparsableCell(
+ code=node.value.args[0].value, # type: ignore
+ options=kwargs,
+ _ast=node,
+ ),
+ violations=violations,
)
raise MarimoFileError(
@@ -541,8 +558,10 @@ class Parser:
if maybe_setup and is_setup_cell(maybe_setup):
next(body)
+ setup_result = self.extractor.to_setup_cell(maybe_setup)
+ violations.extend(setup_result.violations)
return ParseResult(
- self.extractor.to_setup_cell(maybe_setup),
+ setup_result.unwrap(),
violations=violations,
)
return ParseResult(violations=violations)
@@ -554,7 +573,9 @@ class Parser:
while node := next(body):
if is_body_cell(node):
- cells.append(self.extractor.to_cell(node))
+ cell_result = self.extractor.to_cell(node)
+ violations.extend(cell_result.violations)
+ cells.append(cell_result.unwrap())
elif is_run_guard(node):
break
else:
@@ -851,7 +872,8 @@ def is_app_def(node: Node, import_alias: str = "marimo") -> bool:
def is_cell_decorator(
decorator: ast.expr,
- allowed: tuple[str, ...] = ("cell", "function", "class_definition"),
+ *,
+ allowed: tuple[str, ...],
) -> bool:
if isinstance(decorator, ast.Attribute):
return (
@@ -860,7 +882,7 @@ def is_cell_decorator(
and decorator.attr in allowed
)
elif isinstance(decorator, ast.Call):
- return is_cell_decorator(decorator.func)
+ return is_cell_decorator(decorator.func, allowed=allowed)
return False
@@ -881,7 +903,9 @@ def is_body_cell(node: Node) -> bool:
return (
isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef, ast.ClassDef))
and (decorator := get_valid_decorator(node))
- and is_cell_decorator(decorator)
+ and is_cell_decorator(
+ decorator, allowed=("cell", "function", "class_definition")
+ )
) or is_unparsable_cell(node)
diff --git a/tests/_ast/codegen_data/test_function_decorator_call.py b/tests/_ast/codegen_data/test_function_decorator_call.py
new file mode 100644
index 0000000000..fc4acfce0d
--- /dev/null
+++ b/tests/_ast/codegen_data/test_function_decorator_call.py
@@ -0,0 +1,19 @@
+import marimo
+
+__generated_with = "0.15.3"
+app = marimo.App()
+
+with app.setup():
+ def my_decorator(fn):
+ return fn
+
+
+@my_decorator
+def my_function():
+ x = 1
+ return x
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_load.py b/tests/_ast/test_load.py
index c004e97931..8cfab78b2a 100644
--- a/tests/_ast/test_load.py
+++ b/tests/_ast/test_load.py
@@ -318,7 +318,6 @@ class TestGetStatus:
("test_generate_filecontents_single_cell", "valid"),
("test_generate_filecontents_toplevel", "valid"),
("test_generate_filecontents_toplevel_pytest", "valid"),
- ("test_decorators", "valid"),
("test_get_codes_multiline_string", "valid"),
("test_get_codes_messy", "valid"),
("test_get_codes_single_line_fn", "valid"),
@@ -365,6 +364,8 @@ class TestGetStatus:
), # no body
("test_generate_filecontents_empty", "has_errors"), # no body
("test_app_with_no_cells", "has_errors"), # No body is an error
+ # Invalid decorator order creates an error.
+ ("test_decorators", "has_errors"),
# Syntax errors in code
("_test_not_parsable", "broken"),
("_test_parse_error_in_notebook", "broken"),
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index 4fbff36b18..74788ffc5c 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -105,10 +105,11 @@ class TestParser:
)
assert notebook
# unexpected statements and a missing run guard
- assert len(notebook.violations) == 3
+ # + bad decorator
+ assert len(notebook.violations) == 4
assert "generated" in notebook.violations[0].description
assert "statement" in notebook.violations[1].description
- assert "run guard" in notebook.violations[2].description
+ assert "run guard" in notebook.violations[-1].description
@staticmethod
def test_parse_syntax_errors() -> None:
@@ -117,7 +118,6 @@ class TestParser:
)
assert notebook
# Valid currently
- # TODO: Propagate decorators violations.
assert len(notebook.violations) == 0
assert [cell.name for cell in notebook.cells] == [
"global_error",
@@ -129,8 +129,7 @@ class TestParser:
notebook = parse_notebook(get_filepath("test_decorators").read_text())
assert notebook
# Valid currently
- # TODO: Propagate decorators violations.
- assert len(notebook.violations) == 0
+ assert len(notebook.violations) == 4
@staticmethod
def test_eval_kwargs_with_list_constants() -> None:
@@ -197,3 +196,16 @@ class TestParser:
MarimoFileError, match="`marimo.App` definition expected."
):
parse_notebook(get_filepath("test_non_marimo").read_text())
+
+ @staticmethod
+ def test_function_decorator_call_preserves_other_decorators() -> None:
+ notebook = parse_notebook(
+ get_filepath("test_function_decorator_call").read_text()
+ )
+ assert notebook is not None
+ assert len(notebook.cells) == 2 # setup + my_function
+
+ function_cell = notebook.cells[1]
+ assert "@my_decorator" in function_cell.code, (
+ f"Expected @my_decorator in extracted code, but got: {function_cell.code!r}"
+ )