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: pass setup cell to toplevel resolution posthook (#4538)

Dylan Madisettie3f18006e12b
diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.py
index a12e86bff7..f562f3208f 100644
--- a/marimo/_ast/toplevel.py
+++ b/marimo/_ast/toplevel.py
@@ -41,7 +41,8 @@ TopLevelInvalidHints = Literal[
) = get_args(TopLevelInvalidHints)
TopLevelHints = Union[Literal["Valid"], TopLevelInvalidHints]
-HINT_VALID, *_ = get_args(TopLevelHints)
+# Fancy typing caused an issue, so just set the value explicitly.
+HINT_VALID: Literal["Valid"] = "Valid"
def has_trailing_comment(code: str) -> bool:
@@ -154,6 +155,7 @@ class TopLevelStatus:
dependent_refs = self._cell.refs - (allowed_refs | toplevel)
if not dependent_refs:
self.type = TopLevelType.TOPLEVEL
+ self.hint = HINT_VALID
return
defined_refs = dependent_refs - potential_refs
@@ -337,14 +339,13 @@ class TopLevelExtraction:
assert not self.unresolved
@classmethod
- def from_cells(cls, cells: list[CellImpl]) -> TopLevelExtraction:
+ def from_cells(
+ cls, cells: list[CellImpl], setup: Optional[CellImpl] = None
+ ) -> TopLevelExtraction:
codes = [cell.code for cell in cells]
- names = ["_" for cell in cells]
+ names = ["_" for _ in cells]
cell_configs = [cell.config for cell in cells]
- from marimo._ast.codegen import pop_setup_cell
-
- setup = pop_setup_cell(codes, names, cell_configs, True)
if setup:
return cls(codes, names, cell_configs, setup.defs)
return cls(codes, names, cell_configs, set())
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 7b4b4c14f3..1d0abb1d52 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -6,6 +6,7 @@ from typing import Callable
from marimo import _loggers
from marimo._ast.cell import CellImpl
+from marimo._ast.names import SETUP_CELL_NAME
from marimo._ast.toplevel import TopLevelExtraction
from marimo._data.get_datasets import (
get_datasets_from_variables,
@@ -42,7 +43,7 @@ from marimo._sql.get_engines import (
get_engines_from_variables,
)
from marimo._tracer import kernel_tracer
-from marimo._types.ids import VariableName
+from marimo._types.ids import CellId_t, VariableName
from marimo._utils.flatten import contains_instance
LOGGER = _loggers.marimo_logger()
@@ -338,14 +339,19 @@ def _render_toplevel_defs(
del run_result
variable = cell.toplevel_variable
if variable is not None:
- # TODO: Actually needs to run full extractor on graph extraction.
-
ancestors = runner.graph.ancestors(cell.cell_id)
+ deps = {cell.cell_id: cell} | {
+ cid: runner.graph.cells[cid] for cid in ancestors
+ }
+ setup_id = CellId_t(SETUP_CELL_NAME)
+ setup = runner.graph.cells.get(setup_id)
+ deps.pop(setup_id, None)
+
# TODO: Technically, order does matter incase there is a type definition
# or decorator.
- path = [cell] + [runner.graph.cells[cid] for cid in ancestors]
- extractor = TopLevelExtraction.from_cells(path)
- serialization = next(iter(extractor))
+ path = list(deps.values()) + [cell]
+ extractor = TopLevelExtraction.from_cells(path, setup=setup)
+ serialization = list(iter(extractor))[-1]
CellOp.broadcast_serialization(
serialization=serialization,
diff --git a/marimo/_schemas/serialization.py b/marimo/_schemas/serialization.py
index d60dc26135..7a0631d548 100644
--- a/marimo/_schemas/serialization.py
+++ b/marimo/_schemas/serialization.py
@@ -64,7 +64,8 @@ class CellDef(Node):
if self.end_col_offset == 0 and self._ast.end_col_offset
else self.end_col_offset
)
- self.name = getattr(self._ast, "name", DEFAULT_CELL_NAME)
+ if self.name == DEFAULT_CELL_NAME:
+ self.name = getattr(self._ast, "name", DEFAULT_CELL_NAME)
class SetupCell(CellDef): ...