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: preseve top level names for name thrashing (#9695)

Dylan Madisettib9de1f2cfe75
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.py
index 88fb5b1b8e..88fc6c710e 100644
--- a/marimo/_ast/codegen.py
+++ b/marimo/_ast/codegen.py
@@ -14,13 +14,21 @@ from marimo import _loggers
from marimo._ast.app_config import _AppConfig
from marimo._ast.cell import CellConfig, CellImpl
from marimo._ast.compiler import compile_cell
-from marimo._ast.names import DEFAULT_CELL_NAME, SETUP_CELL_NAME
+from marimo._ast.names import (
+ DEFAULT_CELL_NAME,
+ SETUP_CELL_NAME,
+ TOPLEVEL_CELL_PREFIX,
+)
from marimo._ast.parse import ast_parse
from marimo._ast.toplevel import TopLevelExtraction, TopLevelStatus
from marimo._ast.variables import BUILTINS
from marimo._ast.visitor import Name, VariableData
from marimo._convert.converters import MarimoConvert
-from marimo._schemas.serialization import NotebookSerializationV1
+from marimo._schemas.serialization import (
+ ClassCell,
+ FunctionCell,
+ NotebookSerializationV1,
+)
from marimo._types.ids import CellId_t
from marimo._utils.marimo_path import MarimoPath
from marimo._version import __version__
@@ -495,9 +503,18 @@ def generate_filecontents_from_ir(ir: NotebookSerializationV1) -> str:
else False
)
header_comments = _extract_header_comments(ir)
+ # Mark toplevel-style cells with TOPLEVEL_CELL_PREFIX (mirroring
+ # ir_cell_factory in compiler.py) so the reversion logic in
+ # TopLevelExtraction can reset names to `_` if they are demoted.
+ names = [
+ f"{TOPLEVEL_CELL_PREFIX}{cell.name}"
+ if isinstance(cell, (FunctionCell, ClassCell))
+ else cell.name
+ for cell in ir.cells
+ ]
return generate_filecontents(
codes=[cell.code for cell in ir.cells],
- names=[cell.name for cell in ir.cells],
+ names=names,
cell_configs=[CellConfig.from_dict(cell.options) for cell in ir.cells],
config=_AppConfig.from_untrusted_dict(ir.app.options, silent=silent),
header_comments=header_comments,
diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.py
index 2b5c128a4a..5766cef26b 100644
--- a/tests/_ast/test_codegen.py
+++ b/tests/_ast/test_codegen.py
@@ -2125,6 +2125,114 @@ def default_formatter():
"""
)
+ @staticmethod
+ def test_demoted_function_cell_name_anonymized() -> None:
+ """A `@app.function` cell that gets demoted to a regular cell should
+ serialize with the canonical `_` name, not the original function
+ name. Regression test for `marimo check --fix` keeping the function
+ name in the cell signature.
+ """
+ from marimo._convert.converters import MarimoConvert
+
+ source = """\
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def deco(func):
+ return func
+
+
+def _():
+ x = 2
+ return (x,)
+
+
+@deco
+def foo():
+ return "Hello" * x
+
+
+def _(foo):
+ foo()
+ return
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ result = _strip_header_footer(MarimoConvert.from_py(source).to_py())
+ ast.parse(result)
+ # The demoted cell signature must be `def _(...)`, not `def foo(...)`.
+ assert "@app.cell\ndef foo(" not in result
+ assert result == snapshot(
+ """\
+def deco(func):
+ return func
+
+
+def _():
+ x = 2
+ return (x,)
+
+
+def _(x):
+ @deco
+ def foo():
+ return "Hello" * x
+
+ return (foo,)
+
+
+def _(foo):
+ foo()
+ return\
+"""
+ )
+
+ @staticmethod
+ def test_demoted_class_cell_name_anonymized() -> None:
+ """A `@app.class_definition` cell demoted because of cell-defined refs
+ should also serialize with `_`, not the original class name."""
+ from marimo._convert.converters import MarimoConvert
+
+ source = """\
+import marimo
+
+__generated_with = "0.0.0"
+app = marimo.App()
+
+
+def _():
+ base_value = 10
+ return (base_value,)
+
+
+class MyClass:
+ value = base_value
+
+
+if __name__ == "__main__":
+ app.run()
+"""
+ result = _strip_header_footer(MarimoConvert.from_py(source).to_py())
+ ast.parse(result)
+ # The demoted cell signature must be `def _(...)`, not `def MyClass(...)`.
+ assert "@app.cell\ndef MyClass(" not in result
+ assert "@app.cell\ndef _(" in result
+
def _strip_header_footer(source: str) -> str:
"""Strip up to app = marimo.App() and after if __name__ == "__main__":"""