Skip to content

fix: prevent NameError in marimo run when passing mo.md() between cells (#7994)#8692

Open
mscolnick wants to merge 1 commit intomainfrom
ms/name-error
Open

fix: prevent NameError in marimo run when passing mo.md() between cells (#7994)#8692
mscolnick wants to merge 1 commit intomainfrom
ms/name-error

Conversation

@mscolnick
Copy link
Contributor

Description

_extract_markdown in compiler.py incorrectly matched assignment statements like title = mo.md("# Hello World") as markdown-only cells, because both ast.Expr and ast.Assign nodes have a .value attribute. In marimo run mode, _handle_markdown_cells_on_instantiate would then skip executing these cells entirely, causing downstream NameErrors when other cells referenced the assigned variable.

The fix adds an isinstance(body, ast.Expr) check so only bare mo.md() expressions are treated as markdown cells.

…en cells (#7994)

## Description

`_extract_markdown` in `compiler.py` incorrectly matched assignment statements like `title = mo.md("# Hello World")` as markdown-only cells, because both `ast.Expr` and `ast.Assign` nodes have a `.value` attribute. In `marimo run` mode, `_handle_markdown_cells_on_instantiate` would then skip executing these cells entirely, causing downstream `NameError`s when other cells referenced the assigned variable.

The fix adds an `isinstance(body, ast.Expr)` check so only bare `mo.md()` expressions are treated as markdown cells.
Copilot AI review requested due to automatic review settings March 16, 2026 00:41
@mscolnick mscolnick requested a review from dmadisetti as a code owner March 16, 2026 00:41
@vercel
Copy link

vercel bot commented Mar 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Mar 16, 2026 0:42am

Request Review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where _extract_markdown in compiler.py incorrectly treated assignment statements like title = mo.md("# Hello World") as markdown-only cells. Both ast.Expr and ast.Assign have a .value attribute, so the existing code matched assignments too. In marimo run mode, such cells were skipped rather than executed, causing NameErrors in downstream cells referencing the assigned variable.

Changes:

  • Added an isinstance(body, ast.Expr) guard in _extract_markdown to only match bare mo.md() expressions
  • Added unit tests for various non-markdown mo.md() usages (assignment, annotated assignment, multi-statement)
  • Added an integration test and smoke test reproducing the original bug scenario

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
marimo/_ast/compiler.py Added isinstance(body, ast.Expr) check to prevent assignments from being treated as markdown cells
tests/_ast/test_compiler.py Added TestExtractMarkdown class with unit tests for the fix
tests/_ast/test_app.py Added integration test verifying mo.md() objects can be passed between cells in run mode
marimo/_smoke_tests/markdown/cross_cell_md.py Added smoke test notebook reproducing the bug scenario

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mscolnick mscolnick added the bug Something isn't working label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants