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: allow invalid decorator permutations + tests (#6357)

Dylan Madisetticf248a8b2a7d
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index 27345de274..c7b5ef77a5 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -104,10 +104,11 @@ class Extractor:
]
)
- def extract_from_code(self, node: Node) -> str:
+ def extract_from_code(self, node: Node) -> ParseResult[str]:
# NB. Ast line reference and col index is on a 1-indexed basis.
lineno = node.lineno
col_offset = node.col_offset
+ violations: list[Violation] = []
if hasattr(node, "decorator_list"):
# From the ast, having a decorator list means we are either a
@@ -117,10 +118,24 @@ class Extractor:
)
# Scrub past the decorator + 1, lineno 1 index -1
- if (
- len(node.decorator_list)
- and (decorator := get_valid_decorator(node)) # type: ignore
+ decorator: Optional[ast.expr]
+ if len(node.decorator_list) and (
+ decorator := get_valid_decorator(node)
):
+ # We may have a decorator between cell decorator and function.
+ # This is invalid serialization, but still possible.
+ if is_cell_decorator(decorator, allowed=("cell",)):
+ # We just take the last decorator in this case, which will
+ # be removed on serialization.
+ violations.append(
+ Violation(
+ "Multiple decorators found, only @app.cell is valid.",
+ lineno=decorator.lineno,
+ col_offset=decorator.col_offset,
+ )
+ )
+
+ decorator = node.decorator_list[-1]
lineno = _none_to_0(decorator.end_lineno)
col_offset = decorator.col_offset - 1
else:
@@ -132,15 +147,20 @@ class Extractor:
_none_to_0(node.end_lineno) - 1,
_none_to_0(node.end_col_offset),
)
- return fixed_dedent(code)
+ return ParseResult(fixed_dedent(code), violations=violations)
- def to_cell_def(self, node: FnNode, kwargs: dict[str, Any]) -> CellDef:
+ def to_cell_def(
+ self, node: FnNode, kwargs: dict[str, Any]
+ ) -> ParseResult[CellDef]:
# A general note on the apparent brittleness of this code:
# - Ast line reference and col index is on a 1-indexed basis
# - Multiline statements need to be accounted for
# - Painstaking testing can be found in test/_ast/test_{load, parse}
- function_code = self.extract_from_code(node)
+ function_code_reult = self.extract_from_code(node)
+ violations = function_code_reult.violations
+ function_code = function_code_reult.unwrap()
+
lineno_offset, col_offset = extract_offsets_post_colon(
function_code,
block_start="def",
@@ -184,14 +204,17 @@ class Extractor:
# If we are on the same line as the return statement,
# just return a blank cell.
if start_lineno == node.body[0].lineno:
- return CellDef(
- code="",
- options=kwargs,
- lineno=start_lineno,
- col_offset=node.col_offset + col_offset,
- end_lineno=start_lineno,
- end_col_offset=len(self.lines[-1]),
- name=getattr(node, "name", DEFAULT_CELL_NAME),
+ return ParseResult(
+ CellDef(
+ code="",
+ options=kwargs,
+ lineno=start_lineno,
+ col_offset=node.col_offset + col_offset,
+ end_lineno=start_lineno,
+ end_col_offset=len(self.lines[-1]),
+ name=getattr(node, "name", DEFAULT_CELL_NAME),
+ ),
+ violations=violations,
)
else:
end_lineno = node.body[-1].lineno - 1
@@ -242,20 +265,25 @@ class Extractor:
# Line positioning here is still consequential for correct stack tracing
# produced in _ast.compiler.
- return CellDef(
- code=fixed_dedent(cell_code),
- options=kwargs,
- lineno=start_lineno - 1,
- col_offset=node.col_offset + col_offset,
- end_lineno=_none_to_0(node.end_lineno) + end_lineno,
- end_col_offset=_none_to_0(node.end_col_offset) + end_col_offset,
- name=getattr(node, "name", DEFAULT_CELL_NAME),
+ return ParseResult(
+ CellDef(
+ code=fixed_dedent(cell_code),
+ options=kwargs,
+ lineno=start_lineno - 1,
+ col_offset=node.col_offset + col_offset,
+ end_lineno=_none_to_0(node.end_lineno) + end_lineno,
+ end_col_offset=_none_to_0(node.end_col_offset)
+ + end_col_offset,
+ name=getattr(node, "name", DEFAULT_CELL_NAME),
+ ),
+ violations=violations,
)
def to_setup_cell(self, node: Node) -> SetupCell:
kwargs, _violations = _maybe_kwargs(node.items[0].context_expr) # type: ignore
- code = self.extract_from_code(node)
- code = fixed_dedent(code)
+ 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(
@@ -270,6 +298,7 @@ class Extractor:
def to_cell(self, node: Node, attribute: Optional[str] = None) -> CellDef:
"""Convert an AST node to a CellDef."""
+ # TODO: Handle violations
if isinstance(
node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)
):
@@ -290,15 +319,17 @@ class Extractor:
assert isinstance(
node, (ast.FunctionDef, ast.AsyncFunctionDef)
), "@app.cell cannot be used on classes."
- return self.to_cell_def(node, kwargs)
+ cell_result = self.to_cell_def(node, kwargs)
+ return cell_result.unwrap()
cell_types: dict[Optional[str], type[CellDef]] = {
"function": FunctionCell,
"class_definition": ClassCell,
}
cell_type = cell_types.get(attribute, None)
if cell_type is not None:
+ code = self.extract_from_code(node)
return cell_type(
- code=self.extract_from_code(node),
+ code=code.unwrap(),
_ast=node,
options=kwargs,
)
@@ -729,7 +760,7 @@ def get_valid_decorator(
for decorator in node.decorator_list:
if (
isinstance(decorator, ast.Call)
- and decorator.func.attr in valid_decorators # type: ignore
+ and getattr(decorator.func, "attr", None) in valid_decorators
) or (
isinstance(decorator, ast.Attribute)
and decorator.attr in valid_decorators
@@ -783,12 +814,15 @@ def is_app_def(node: Node, import_alias: str = "marimo") -> bool:
)
-def is_cell_decorator(decorator: ast.expr) -> bool:
+def is_cell_decorator(
+ decorator: ast.expr,
+ allowed: tuple[str, ...] = ("cell", "function", "class_definition"),
+) -> bool:
if isinstance(decorator, ast.Attribute):
return (
isinstance(decorator.value, ast.Name)
and decorator.value.id == "app"
- and decorator.attr in ("cell", "function", "class_definition")
+ and decorator.attr in allowed
)
elif isinstance(decorator, ast.Call):
return is_cell_decorator(decorator.func)
diff --git a/tests/_ast/codegen_data/test_decorators.py b/tests/_ast/codegen_data/test_decorators.py
new file mode 100644
index 0000000000..dbe67f5040
--- /dev/null
+++ b/tests/_ast/codegen_data/test_decorators.py
@@ -0,0 +1,89 @@
+import marimo
+
+__generated_with = "0.15.3"
+app = marimo.App(width="medium")
+
+with app.setup():
+ def wrapper(fn):
+ return fn
+
+ # AST is slightly different when decorator is called
+ def called_wrapper():
+ return wrapper
+
+
+@wrapper
+def my_wrapped():
+ pass
+
+
+@wrapper
+def inv_wrapped():
+ pass
+
+
+@wrapper
+def my_cell():
+ pass
+
+
+# NOTE: This is an invalid case. A cell should never be decorated via codegen,
+# However, we capture the case because internal errors should never happen from
+# user code.
+@wrapper
+def inv_cell():
+ pass
+
+
+@wrapper
+class MyClass:
+ pass
+
+
+@wrapper
+class InvClass:
+ pass
+
+
+@called_wrapper()
+def my_wrapped_called():
+ pass
+
+@called_wrapper()
+def inv_wrapped_called():
+ pass
+
+
+@called_wrapper()
+def my_cell_called():
+ pass
+
+@called_wrapper()
+def inv_cell_called():
+ pass
+
+
+@called_wrapper()
+class MyClassCalled:
+ pass
+
+
+@called_wrapper()
+class InvClassCalled:
+ pass
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index 38901565b5..3cece74494 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -108,3 +108,11 @@ class TestParser:
assert "generated" in notebook.violations[0].description
assert "statement" in notebook.violations[1].description
assert "run guard" in notebook.violations[2].description
+
+ @staticmethod
+ def test_parse_decorator_permutations() -> None:
+ notebook = parse_notebook(get_filepath("test_decorators").read_text())
+ assert notebook
+ # Valid currently
+ # TODO: Propagate decorators violations.
+ assert len(notebook.violations) == 0