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: prevent `NameError` in `marimo run` when passing `mo.md()` between cells (#7994) (#8692)

Myles Scolnick61e9b395553c
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index de9c73474c..1db3396bfa 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -170,7 +170,7 @@ def fix_source_position(node: Any, source_position: SourcePosition) -> Any:
return node
-def _extract_const_string(args: list[ast.stmt]) -> str:
+def _extract_const_string(args: list[ast.expr]) -> str:
(inner,) = args
# Various string types may need to be unpacked
if isinstance(inner, ast.JoinedStr) or (
@@ -194,11 +194,18 @@ def _extract_markdown(tree: ast.Module) -> Optional[str]:
# Wish there was a more compact to ignore ignore[attr-defined] for all.
try:
(body,) = tree.body
+ # Only match bare expressions like `mo.md("...")`, not assignments
+ # like `title = mo.md("...")` — both ast.Expr and ast.Assign have a
+ # `.value` attribute, so we must check the node type explicitly.
+ if not isinstance(body, ast.Expr):
+ return None
if body.value.func.attr == "md": # type: ignore[attr-defined, union-attr]
value = body.value # type: ignore[attr-defined, union-attr]
else:
return None
- assert value.func.value.id == "mo"
+ if not isinstance(value, ast.Call):
+ return None
+ assert value.func.value.id == "mo" # type: ignore[attr-defined]
if not value.args: # Handle mo.md() with no arguments
return None
md_lines = _extract_const_string(value.args).split("\n")
diff --git a/marimo/_smoke_tests/markdown/cross_cell_md.py b/marimo/_smoke_tests/markdown/cross_cell_md.py
new file mode 100644
index 0000000000..4d9f5d5b2b
--- /dev/null
+++ b/marimo/_smoke_tests/markdown/cross_cell_md.py
@@ -0,0 +1,29 @@
+import marimo
+
+__generated_with = "0.19.6"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
+def _(mo):
+ title = mo.md("# Hello World")
+ return (title,)
+
+
+def _(mo, title):
+ mo.vstack([
+ title,
+ mo.md("This should display above this text."),
+ ])
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index 6687c54440..87f798a9d9 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -966,6 +966,37 @@ class TestApp:
assert "cell_one" in python_code
assert "cell_two" in python_code
+ @staticmethod
+ def test_run_md_object_passed_between_cells() -> None:
+ """Regression test for #7994: passing mo.md() object between cells
+ should work in script/run mode."""
+ app = App()
+
+ @app.cell
+ def _():
+ import marimo as mo
+
+ return (mo,)
+
+ @app.cell
+ def _(mo):
+ title = mo.md("# Hello World")
+ return (title,)
+
+ @app.cell
+ def _(mo, title):
+ mo.vstack(
+ [
+ title,
+ mo.md("This should display above this text."),
+ ]
+ )
+ return
+
+ outputs, defs = app.run()
+ assert "title" in defs
+ assert defs["title"] is not None
+
class TestInvalidSetup:
@staticmethod
diff --git a/tests/_ast/test_compiler.py b/tests/_ast/test_compiler.py
index 3ae12b2bf1..e8b2ae8c7d 100644
--- a/tests/_ast/test_compiler.py
+++ b/tests/_ast/test_compiler.py
@@ -323,6 +323,31 @@ class TestParseSQLCell:
assert not cell.variable_data
+class TestExtractMarkdown:
+ @staticmethod
+ def test_bare_md_expression() -> None:
+ cell = compile_cell('mo.md("# Hello World")')
+ assert cell.markdown == "# Hello World"
+
+ @staticmethod
+ def test_assignment_not_treated_as_markdown() -> None:
+ """Regression test for #7994: `title = mo.md(...)` was incorrectly
+ identified as a markdown cell, causing NameError in run mode."""
+ cell = compile_cell('title = mo.md("# Hello World")')
+ assert cell.markdown is None
+ assert cell.defs == {"title"}
+
+ @staticmethod
+ def test_annotated_assignment_not_treated_as_markdown() -> None:
+ cell = compile_cell('title: Any = mo.md("# Hello")')
+ assert cell.markdown is None
+
+ @staticmethod
+ def test_multi_statement_not_treated_as_markdown() -> None:
+ cell = compile_cell('x = 1\nmo.md("# Hello")')
+ assert cell.markdown is None
+
+
class TestCellFactory:
@staticmethod
def test_defs() -> None: