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 trailing comments in pure functions, setup cell (#7879)

Akshay Agrawala69b2e57fc1a
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index 84b4a1376d..6f6f83fd32 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -176,6 +176,59 @@ class Extractor:
_none_to_0(node.end_lineno) - 1,
_none_to_0(node.end_col_offset),
)
+
+ # Capture trailing comments after the AST's end position for app.function
+ # and the setup cell (which is in a with block).
+ #
+ # The AST doesn't include comments, so we look for lines after
+ # node.end_lineno that are at the body indentation level.
+ # We append trailing code BEFORE dedenting so both get dedented
+ # by the same amount, preserving relative indentation.
+ if (
+ isinstance(
+ node,
+ (
+ ast.AsyncFunctionDef,
+ ast.FunctionDef,
+ ast.ClassDef,
+ ast.With,
+ ast.AsyncWith,
+ ),
+ )
+ and len(node.body) > 0
+ ):
+ end_lineno = _none_to_0(node.end_lineno)
+ # Extract actual indentation from the first body line
+ # (handles both tabs and spaces)
+ first_body_line = self.lines[node.body[0].lineno - 1]
+ body_indent = first_body_line[
+ : len(first_body_line) - len(first_body_line.lstrip())
+ ]
+ # Skip if body is on the same line as the definition (e.g.,
+ # `class Foo: ...` or `def f(): pass`) - empty indent would
+ # match all lines incorrectly.
+ if not body_indent:
+ return ParseResult(fixed_dedent(code), violations=violations)
+
+ # Find the extent of the body by consuming lines until we find
+ # a line that is not indented at the body level.
+ for line_idx in range(end_lineno, len(self.lines)):
+ line = self.lines[line_idx]
+ # Empty lines or lines at body indentation are included
+ if not line.strip() or line.startswith(body_indent):
+ continue
+ # Found a line not at the right indentation
+ break
+ else:
+ line_idx = len(self.lines)
+
+ if line_idx > end_lineno:
+ trailing_code = "\n".join(
+ self.lines[end_lineno:line_idx]
+ ).rstrip()
+ if trailing_code:
+ code = code + "\n" + trailing_code
+
return ParseResult(fixed_dedent(code), violations=violations)
def to_cell_def(
@@ -312,7 +365,7 @@ class Extractor:
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())
+ code = code_result.unwrap()
if code.endswith("\npass"):
code = code[: -len("\npass")]
return ParseResult(
@@ -363,11 +416,11 @@ class Extractor:
}
cell_type = cell_types.get(attribute, None)
if cell_type is not None:
- code = self.extract_from_code(node)
- violations.extend(code.violations)
+ code_result = self.extract_from_code(node)
+ violations.extend(code_result.violations)
return ParseResult(
cell_type(
- code=code.unwrap(),
+ code=code_result.unwrap(),
_ast=node,
options=kwargs,
),
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index 5d3f5473f9..ca0d429cc6 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -236,6 +236,255 @@ if __name__ == "__main__":
assert "Expected string constant" in notebook.violations[0].description
+class TestTrailingComments:
+ """Tests for trailing comment preservation in @app.function and @app.class_definition."""
+
+ @staticmethod
+ def test_function_with_trailing_comment() -> None:
+ """@app.function should preserve trailing comments at body indentation."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def f():
+ # start
+ pass
+ # end
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert "# end" in notebook.cells[0].code
+
+ @staticmethod
+ def test_function_with_trailing_comment_and_empty_line() -> None:
+ """@app.function should preserve trailing comments at body indentation."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def f():
+ # start
+ pass
+
+ # end
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert "# end" in notebook.cells[0].code
+
+ @staticmethod
+ def test_two_functions() -> None:
+ """@app.function should preserve trailing comments at body indentation."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def f():
+ # start
+ pass
+
+ # end
+
+def g():
+ # start 1
+ pass
+ # end 1
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 2
+ assert (
+ notebook.cells[0].code
+ == """def f():
+ # start
+ pass
+
+ # end"""
+ )
+
+ assert (
+ notebook.cells[1].code
+ == """def g():
+ # start 1
+ pass
+ # end 1"""
+ )
+
+ @staticmethod
+ def test_class_definition_with_trailing_comment() -> None:
+ """@app.class_definition should preserve trailing comments at body indentation."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+class MyClass:
+ def method(self):
+ pass
+ # trailing class comment
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert "# trailing class comment" in notebook.cells[0].code
+
+ @staticmethod
+ def test_function_with_multiple_trailing_comments() -> None:
+ """@app.function should preserve multiple trailing comments."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def g():
+ x = 1
+ # comment 1
+ # comment 2
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert "# comment 1" in notebook.cells[0].code
+ assert "# comment 2" in notebook.cells[0].code
+
+ @staticmethod
+ def test_function_without_trailing_comment() -> None:
+ """@app.function without trailing comments should work as before."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def h():
+ return 42
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert notebook.cells[0].code == "def h():\n return 42"
+
+ @staticmethod
+ def test_function_with_tab_indentation() -> None:
+ """@app.function should preserve trailing comments with tab indentation."""
+ # Use explicit tab characters
+ code = 'import marimo\n\n__generated_with = "0.0.0"\napp = marimo.App()\n\n\[email protected]\ndef f():\n\tpass\n\t# trailing comment\n\n\nif __name__ == "__main__":\n\tapp.run()\n'
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 1
+ assert "# trailing comment" in notebook.cells[0].code
+
+ @staticmethod
+ def test_setup_cell_with_trailing_comment() -> None:
+ """Setup cells should preserve trailing comments."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+with app.setup:
+ import os
+ # trailing setup comment
+
+def _():
+ pass
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 2
+ assert notebook.cells[0].name == "setup"
+ assert notebook.cells[0].code == "import os\n# trailing setup comment"
+
+ @staticmethod
+ def test_single_line_function_no_trailing_capture() -> None:
+ """Single-line functions should not capture content from next cell."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def f(): pass
+
+
+def g():
+ return 42
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 2
+ assert notebook.cells[0].code == "def f(): pass"
+ assert notebook.cells[1].code == "def g():\n return 42"
+
+ @staticmethod
+ def test_single_line_class_no_trailing_capture() -> None:
+ """Single-line classes should not capture content from next cell."""
+ code = """import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+class Foo: ...
+
+
+def _():
+ x = 1
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ notebook = parse_notebook(code)
+ assert len(notebook.cells) == 2
+ assert notebook.cells[0].code == "class Foo: ..."
+ assert notebook.cells[1].code == "x = 1"
+
+
def test_fixed_dedent() -> None:
# Basic dedent
assert fixed_dedent(" x = 1\n y = 2") == snapshot("""\