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: regression added in #4538 and relevant test (#4549)

Dylan Madisetti03e5d35d5c52
diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.py
index f562f3208f..c5fe9b0481 100644
--- a/marimo/_ast/toplevel.py
+++ b/marimo/_ast/toplevel.py
@@ -11,7 +11,9 @@ from typing import TYPE_CHECKING, Literal, Optional, Union, get_args
from marimo._ast.app import InternalApp
from marimo._ast.cell import CellConfig, CellImpl
from marimo._ast.compiler import compile_cell
+from marimo._ast.names import SETUP_CELL_NAME
from marimo._ast.visitor import Name
+from marimo._runtime.dataflow import DirectedGraph
from marimo._types.ids import CellId_t
if TYPE_CHECKING:
@@ -338,6 +340,24 @@ class TopLevelExtraction:
_ = resolve(name)
assert not self.unresolved
+ @classmethod
+ def from_graph(
+ cls,
+ cell: CellImpl,
+ graph: DirectedGraph,
+ ) -> TopLevelExtraction:
+ ancestors = graph.ancestors(cell.cell_id)
+ deps = {cid: graph.cells[cid] for cid in ancestors}
+ setup_id = CellId_t(SETUP_CELL_NAME)
+ setup = graph.cells.get(setup_id)
+ setup = graph.cells.get(setup_id)
+ deps.pop(setup_id, None)
+
+ # TODO: Technically, order does matter incase there is a type definition
+ # or decorator.
+ path = list(deps.values()) + [cell]
+ return cls.from_cells(path, setup=setup)
+
@classmethod
def from_cells(
cls, cells: list[CellImpl], setup: Optional[CellImpl] = None
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 1d0abb1d52..0a04336b2a 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -6,7 +6,6 @@ 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,
@@ -43,7 +42,7 @@ from marimo._sql.get_engines import (
get_engines_from_variables,
)
from marimo._tracer import kernel_tracer
-from marimo._types.ids import CellId_t, VariableName
+from marimo._types.ids import VariableName
from marimo._utils.flatten import contains_instance
LOGGER = _loggers.marimo_logger()
@@ -339,20 +338,8 @@ def _render_toplevel_defs(
del run_result
variable = cell.toplevel_variable
if variable is not None:
- 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 = list(deps.values()) + [cell]
- extractor = TopLevelExtraction.from_cells(path, setup=setup)
+ extractor = TopLevelExtraction.from_graph(cell, runner.graph)
serialization = list(iter(extractor))[-1]
-
CellOp.broadcast_serialization(
serialization=serialization,
cell_id=cell.cell_id,
diff --git a/tests/_ast/test_toplevel.py b/tests/_ast/test_toplevel.py
index b489480b94..526b6a8cec 100644
--- a/tests/_ast/test_toplevel.py
+++ b/tests/_ast/test_toplevel.py
@@ -1,4 +1,4 @@
-# Copyright 2024 Marimo. All rights reserved.
+# Copyright 2025 Marimo. All rights reserved.
from __future__ import annotations
@@ -453,3 +453,39 @@ class TestTopLevelClasses:
TopLevelType.CELL,
TopLevelType.TOPLEVEL,
] == [s.type for s in extraction], [s.hint for s in extraction]
+
+ @staticmethod
+ def test_class_invocation(app) -> None:
+ @app.class_definition
+ class Example: ...
+
+ @app.function
+ def f():
+ return Example()
+
+ extraction = TopLevelExtraction.from_app(InternalApp(app))
+ assert [
+ TopLevelType.TOPLEVEL,
+ TopLevelType.TOPLEVEL,
+ ] == [s.type for s in extraction], [s.hint for s in extraction]
+
+
+class TestTopLevelHook:
+ @staticmethod
+ def test_toplevel_hook(app) -> None:
+ @app.class_definition
+ class Example: ...
+
+ @app.cell()
+ def f():
+ def f():
+ return Example()
+
+ extraction = TopLevelExtraction.from_graph(
+ f._cell,
+ InternalApp(app).graph,
+ )
+ assert [
+ TopLevelType.TOPLEVEL,
+ TopLevelType.TOPLEVEL,
+ ] == [s.type for s in extraction], [s.hint for s in extraction]