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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: preserve pending dependencies across code-mode edits (#11121)
Shahmir Varqha9ae9c355ea3b
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.pyindex d4635342ce..84f133851e 100644--- a/marimo/_code_mode/_context.py+++ b/marimo/_code_mode/_context.py@@ -1781,12 +1781,11 @@ class AsyncCodeModeContext:if e.code is not None]requested_ids = {request.cell_id for request in execution_requests}- # Requested cells may exist only in the document. Register them in- # the same mutation so dependency sorting sees the whole run batch.+ # Include every run target so preparation can discover pending+ # ancestors using the complete batch's source.for entry in plan:if (entry.cell_id in _run_set- and entry.cell_id not in self.graph.cellsand entry.cell_id not in requested_ids):execution_requests.append(@@ -1795,15 +1794,21 @@ class AsyncCodeModeContext:code=existing_code[entry.cell_id],))- resolved_configs.setdefault(- entry.cell_id,- self._document.get_cell(entry.cell_id).config,- )deletion_requests = [DeleteCellCommand(cell_id=cid)for cid in existing_id_set - plan_ids- if cid in self.graph.cells]+ execution_requests = self._kernel.prepare_execution_requests(+ execution_requests,+ run_cell_ids=_run_set,+ deletion_requests=deletion_requests,+ )+ for request in execution_requests:+ if request.cell_id in existing_id_set:+ resolved_configs.setdefault(+ request.cell_id,+ self._current_config(request.cell_id),+ )cells_to_run = self._kernel.mutate_graph(execution_requests, deletion_requests)diff --git a/marimo/_runtime/dataflow/__init__.py b/marimo/_runtime/dataflow/__init__.pyindex 24147a39bc..73a0da13cc 100644--- a/marimo/_runtime/dataflow/__init__.py+++ b/marimo/_runtime/dataflow/__init__.py@@ -6,13 +6,18 @@ from typing import TYPE_CHECKING, Anyfrom marimo import _loggersfrom marimo._ast.cell import CellImpl+from marimo._runtime.dataflow import edges+from marimo._runtime.dataflow.definitions import DefinitionRegistryfrom marimo._runtime.dataflow.graph import DirectedGraph-from marimo._runtime.dataflow.topology import GraphTopology+from marimo._runtime.dataflow.topology import (+ GraphTopology,+ MutableGraphTopology,+)from marimo._runtime.dataflow.types import Edge, EdgeWithVarfrom marimo._types.ids import CellId_tif TYPE_CHECKING:- from collections.abc import Callable, Collection+ from collections.abc import Callable, Collection, MappingLOGGER = _loggers.marimo_logger()@@ -67,6 +72,37 @@ def transitive_closure(return result+def get_ancestors_from_cells(+ cells: Mapping[CellId_t, CellImpl], cell_ids: set[CellId_t]+) -> set[CellId_t]:+ """Find ancestors from compiled cells without modifying runtime state.++ Cells are indexed in mapping order, matching ordinary graph registration.+ Unknown IDs and the roots themselves are excluded from the result.+ """+ roots = cell_ids & cells.keys()+ if not roots:+ return set()++ topology = MutableGraphTopology()+ definitions = DefinitionRegistry()+ for cid, cell in cells.items():+ parents, children = edges.register_cell_dependencies(+ cid, cell, topology, definitions+ )+ for parent in parents:+ topology.add_edge(parent, cid)+ for child in children:+ topology.add_edge(cid, child)++ return transitive_closure(+ topology,+ roots,+ children=False,+ inclusive=False,+ )++def induced_subgraph(graph: GraphTopology, cell_ids: Collection[CellId_t]) -> tuple[dict[CellId_t, set[CellId_t]], dict[CellId_t, set[CellId_t]]]:diff --git a/marimo/_runtime/dataflow/edges.py b/marimo/_runtime/dataflow/edges.pyindex 5cb57208f0..3778d9afa4 100644--- a/marimo/_runtime/dataflow/edges.py+++ b/marimo/_runtime/dataflow/edges.py@@ -12,7 +12,10 @@ if TYPE_CHECKING:from marimo._ast.sql_visitor import SQLReffrom marimo._ast.visitor import Namefrom marimo._runtime.dataflow.definitions import DefinitionRegistry- from marimo._runtime.dataflow.topology import GraphTopology+ from marimo._runtime.dataflow.topology import (+ GraphTopology,+ MutableGraphTopology,+ )from marimo._types.ids import CellId_tfrom marimo._ast.sql_visitor import SQLTypes@@ -20,6 +23,24 @@ from marimo._ast.sql_visitor import SQLTypesLOGGER = _loggers.marimo_logger()+def register_cell_dependencies(+ cell_id: CellId_t,+ cell: CellImpl,+ topology: MutableGraphTopology,+ definitions: DefinitionRegistry,+) -> tuple[set[CellId_t], set[CellId_t]]:+ """Add a compiled cell to the dependency indexes and resolve its edges.++ The caller applies the returned parent/child edges. Cell runtime state+ is never modified.+ """+ topology.add_node(cell_id, cell)+ # Definitions must be indexed before resolving references to this cell.+ for name, variable_data in cell.variable_data.items():+ definitions.register_definition(cell_id, name, variable_data)+ return compute_edges_for_cell(cell_id, cell, topology, definitions)++def get_referring_cells(name: Name,language: Literal["python", "sql"],diff --git a/marimo/_runtime/dataflow/graph.py b/marimo/_runtime/dataflow/graph.pyindex 8ec90cb0d5..942e46ad0f 100644--- a/marimo/_runtime/dataflow/graph.py+++ b/marimo/_runtime/dataflow/graph.py@@ -101,18 +101,7 @@ class DirectedGraph(GraphTopology):LOGGER.debug("Acquired graph lock.")assert cell_id not in self.topology.cells- # Add the cell to topology- self.topology.add_node(cell_id, cell)-- # Process definitions and build sibling relationships FIRST- # This must happen before computing edges because edge computation- # needs to look up definitions- for name, variable_data in cell.variable_data.items():- self.definition_registry.register_definition(- cell_id, name, variable_data- )- # Now compute edges (which can now find the definitions)- parents, children = edges.compute_edges_for_cell(+ parents, children = edges.register_cell_dependencies(cell_id, cell, self.topology, self.definition_registry)diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex c3aab16e11..93287989f8 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -1249,8 +1249,16 @@ class Kernel:# Register and delete cellsfor er in execution_requests:+ # Edit-only batches must also retire source that a later run+ # could otherwise replay from the pending initialization cache.+ pending = self._uninstantiated_execution_requests.pop(+ er.cell_id, None+ )old_children, error = self._maybe_register_cell(- er.cell_id, er.code, stale=er.cell_id in cells_starting_stale+ er.cell_id,+ er.code,+ stale=er.cell_id in cells_starting_stale+ or pending is not None,)cells_that_were_children_of_mutated_cells |= old_childrenif error is None:@@ -1259,7 +1267,9 @@ class Kernel:syntax_errors[er.cell_id] = errorfor dr in deletion_requests:+ self._uninstantiated_execution_requests.pop(dr.cell_id, None)if dr.cell_id not in cells_before_mutation:+ self.cell_metadata.pop(dr.cell_id, None)continuecells_that_were_children_of_mutated_cells |= self._delete_cell(dr.cell_id@@ -1684,6 +1694,69 @@ class Kernel:self.mutate_graph(execution_requests, deletion_requests)await self.run(execution_requests)+ def prepare_execution_requests(+ self,+ execution_requests: Sequence[ExecuteCellCommand],+ *,+ run_cell_ids: set[CellId_t] | None = None,+ deletion_requests: Sequence[DeleteCellCommand] = (),+ ) -> list[ExecuteCellCommand]:+ """Include pending ancestors before graph mutation, without executing.++ Mutation batches may register cells without running them. Only the+ requested run targets contribute implicit ancestors; edits and+ deletions override pending source. Pending requests are consumed by+ `mutate_graph`, once their replacement is applied.+ """+ requests = list(execution_requests)+ if (+ not requests+ or not self._uninstantiated_execution_requests+ or (run_cell_ids is not None and not run_cell_ids)+ ):+ return requests++ requested_ids = {request.cell_id for request in requests}+ deleted_ids = {request.cell_id for request in deletion_requests}+ pending = {+ cid: request+ for cid, request in self._uninstantiated_execution_requests.items()+ if cid not in requested_ids and cid not in deleted_ids+ }+ if not pending:+ return requests+ # Registered but unrun cells can bridge a run target to pending+ # ancestors. Reuse compiled cells for read-only dependency analysis;+ # replacements and deletions supersede old source.+ cells = {+ cid: cell+ for cid, cell in self.graph.cells.items()+ if cid not in requested_ids and cid not in deleted_ids+ }+ # SQL edge resolution is order-sensitive. Analyze the same+ # request-before-ancestor order that mutate_graph will receive.+ source_requests = {+ request.cell_id: request+ for request in [*requests, *pending.values()]+ }+ for request in source_requests.values():+ if request.cell_id in cells:+ continue+ cell = self.graph.cells.get(request.cell_id)+ if cell is None or cell.code != request.code:+ try:+ cell = compile_cell(request.code, cell_id=request.cell_id)+ except Exception: # noqa: S112+ # Graph mutation reports compilation failures.+ continue+ cells[request.cell_id] = cell+ roots = requested_ids if run_cell_ids is None else run_cell_ids+ ancestors = dataflow.get_ancestors_from_cells(cells, roots)+ requests.extend(+ request for cid, request in pending.items() if cid in ancestors+ )+ return requests+@kernel_tracer.start_as_current_span("run")async def run(self, execution_requests: Sequence[ExecuteCellCommand]@@ -1702,63 +1775,11 @@ class Kernel:async def _run_with_uninstantiated_requests(execution_requests: Sequence[ExecuteCellCommand],) -> None:- if not self._uninstantiated_execution_requests:- await self._run_cells(- self.mutate_graph(execution_requests, deletion_requests=[])- )- return-- execution_requests_cell_ids = {- er.cell_id for er in execution_requests- }- graph = dataflow.DirectedGraph()- # cells in execution_requests that should be initially marked as- # stale:- cells_starting_stale: set[CellId_t] = set()- for cid, er in list(- self._uninstantiated_execution_requests.items()- ):- if cid in execution_requests_cell_ids:- # Running a previously uninstantiated cell; just remove- # it from our cache of uninstantiated execution requests.- cells_starting_stale.add(cid)- del self._uninstantiated_execution_requests[cid]- continue-- try:- cell = compile_cell(er.code, cell_id=er.cell_id)- except Exception: # noqa: S112- # The cell was not parsable.- continue- graph.register_cell(cell_id=cid, cell=cell)-- # Collect uninstantiated ancestors- ancestors: set[CellId_t] = set()- for er in execution_requests:- try:- cell = compile_cell(er.code, cell_id=er.cell_id)- # Unparsable requests cannot contribute graph ancestors.- except Exception: # noqa: S112- continue- graph.register_cell(cell_id=er.cell_id, cell=cell)- ancestors |= graph.ancestors(er.cell_id)-- # We run all uninstantiated ancestors of the requested cells- previously_uninstantiated_requests: list[ExecuteCellCommand] = []- for ancestor_cid in ancestors:- if ancestor_cid in self._uninstantiated_execution_requests:- previously_uninstantiated_requests.append(- self._uninstantiated_execution_requests[ancestor_cid]- )- cells_starting_stale.add(ancestor_cid)- del self._uninstantiated_execution_requests[ancestor_cid]-+ requests = self.prepare_execution_requests(execution_requests)await self._run_cells(self.mutate_graph(- list(execution_requests)- + previously_uninstantiated_requests,+ requests,deletion_requests=[],- cells_starting_stale=cells_starting_stale,))diff --git a/tests/_code_mode/test_code_mode_context.py b/tests/_code_mode/test_code_mode_context.pyindex 30ef436ffa..311cc99f7d 100644--- a/tests/_code_mode/test_code_mode_context.py+++ b/tests/_code_mode/test_code_mode_context.py@@ -14,7 +14,9 @@ from inline_snapshot import snapshotfrom marimo._ast.cell import CellConfigfrom marimo._ast.cell_id import CellIdGenerator+from marimo._ast.compiler import compile_cellfrom marimo._code_mode._context import AsyncCodeModeContext+from marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.notebook.document import (NotebookCell,NotebookDocument,@@ -25,9 +27,15 @@ from marimo._messaging.notification import (NotebookDocumentTransactionNotification,OperationRunning,)-from marimo._runtime.commands import ExecuteCellCommand+from marimo._runtime import dataflow+from marimo._runtime.commands import (+ CreateNotebookCommand,+ DeleteCellCommand,+ ExecuteCellCommand,+ UpdateUIElementCommand,+)from marimo._runtime.packages.package_manager import PackageDescription-from marimo._runtime.runtime import Kernel+from marimo._runtime.runtime import CellMetadata, Kernelfrom marimo._types.ids import CellId_t@@ -1439,6 +1447,272 @@ class TestDocumentKernelDivergence:assert new_id not in {c.id for c in doc_only}+class TestPendingExecutionRequests:+ @pytest.mark.skipif(+ not DependencyManager.duckdb.has(), reason="requires duckdb"+ )+ async def test_preparation_preserves_sql_mutation_order(+ self, k: Kernel+ ) -> None:+ import duckdb++ queries = {+ "schema": "CREATE SCHEMA schema1",+ "table": "CREATE TABLE schema1.t1 (i INTEGER)",+ "select": "SELECT * FROM schema1.t1",+ }+ k._uninstantiated_execution_requests = {+ CellId_t(cid): ExecuteCellCommand(+ cell_id=CellId_t(cid), code=f"mo.sql({query!r})"+ )+ for cid, query in queries.items()+ }+ k._uninstantiated_execution_requests[CellId_t("unrelated")] = (+ ExecuteCellCommand(+ cell_id=CellId_t("unrelated"),+ code="raise RuntimeError('must not run')",+ )+ )+ request = ExecuteCellCommand(+ cell_id=CellId_t("select"),+ code='mo.sql("SELECT i FROM schema1.t1")',+ )+ queries["select"] = "SELECT i FROM schema1.t1"+ prepared = k.prepare_execution_requests([request])+ assert [r.cell_id for r in prepared] == ["select", "schema", "table"]++ cells_to_run = k.mutate_graph(prepared, deletion_requests=[])+ assert k.graph.ancestors(CellId_t("select")) == {"schema", "table"}+ assert set(k._uninstantiated_execution_requests) == {"unrelated"}+ # Execute SQL in the scheduler's order to catch an omitted schema or+ # a table scheduled before its schema, without shared database state.+ with duckdb.connect() as connection:+ for cid in dataflow.topological_sort(k.graph, cells_to_run):+ connection.execute(queries[cid])+ assert (+ connection.execute("SELECT * FROM schema1.t1").fetchall() == []+ )++ @staticmethod+ async def _open_notebook(k: Kernel) -> list[NotebookCell]:+ cells = [+ NotebookCell(+ id=CellId_t(cid), code=code, name="", config=CellConfig()+ )+ for cid, code in (+ ("root", "x = 1"),+ ("child", "y = x + 1"),+ ("unrelated", "raise RuntimeError('must not run')"),+ )+ ]+ await k.instantiate(+ CreateNotebookCommand(+ execution_requests=tuple(+ ExecuteCellCommand(cell_id=c.id, code=c.code)+ for c in cells+ ),+ cell_ids=tuple(c.id for c in cells),+ set_ui_element_value_request=UpdateUIElementCommand.from_ids_and_values(+ []+ ),+ auto_run=False,+ )+ )+ return cells++ @pytest.mark.parametrize("existing_target", [False, True])+ async def test_preparation_reuses_compiled_cells_without_runtime_changes(+ self, k: Kernel, existing_target: bool+ ) -> None:+ await self._open_notebook(k)+ await k.run(+ [ExecuteCellCommand(cell_id=CellId_t("child"), code="y = x + 1")]+ )+ root = k.graph.cells["root"]+ root.configure({"disabled": True})+ root.set_stale(True, broadcast=False)+ state_before = {+ cid: (cell.runtime_state, cell.stale, cell.config.asdict())+ for cid, cell in k.graph.cells.items()+ }+ notifications_before = list(k.stream.operations)+ pending_before = dict(k._uninstantiated_execution_requests)+ request = ExecuteCellCommand(+ cell_id=CellId_t("child" if existing_target else "final"),+ code="y = x + 1" if existing_target else "z = y + 1",+ )+ with patch(+ "marimo._runtime.runtime.compile_cell", wraps=compile_cell+ ) as compile_spy:+ assert k.prepare_execution_requests([request]) == [request]+ compiled_ids = [+ call.kwargs["cell_id"] for call in compile_spy.call_args_list+ ]+ expected_ids = ["unrelated"]+ if not existing_target:+ expected_ids.append("final")+ assert sorted(compiled_ids) == sorted(expected_ids)+ assert {+ cid: (cell.runtime_state, cell.stale, cell.config.asdict())+ for cid, cell in k.graph.cells.items()+ } == state_before+ assert k.stream.operations == notifications_before+ assert k._uninstantiated_execution_requests == pending_before++ @pytest.mark.parametrize(+ "bridge_change", ["unchanged", "replace", "invalid", "delete"]+ )+ async def test_preparation_uses_effective_batch_source(+ self, k: Kernel, bridge_change: str+ ) -> None:+ await self._open_notebook(k)+ k.mutate_graph(+ [ExecuteCellCommand(cell_id=CellId_t("child"), code="y = x + 2")],+ deletion_requests=[],+ )+ requests = [+ ExecuteCellCommand(cell_id=CellId_t("final"), code="z = y + 1")+ ]+ deletions: list[DeleteCellCommand] = []+ if bridge_change in ("replace", "invalid"):+ requests.append(+ ExecuteCellCommand(+ cell_id=CellId_t("child"),+ code="y = 5" if bridge_change == "replace" else "y =",+ )+ )+ elif bridge_change == "delete":+ deletions.append(DeleteCellCommand(cell_id=CellId_t("child")))++ pending_before = dict(k._uninstantiated_execution_requests)+ prepared = k.prepare_execution_requests(+ requests,+ run_cell_ids={CellId_t("final")},+ deletion_requests=deletions,+ )+ expected = list(requests)+ if bridge_change == "unchanged":+ expected.append(pending_before[CellId_t("root")])+ assert prepared == expected+ assert k._uninstantiated_execution_requests == pending_before+ assert _graph_codes(k) == {"child": "y = x + 2"}++ @pytest.mark.parametrize("execution_path", ["ui", "code_mode"])+ async def test_pending_ancestor_behind_registered_unrun_cell(+ self, any_kernel: Kernel, execution_path: str+ ) -> None:+ k = any_kernel+ cells = await self._open_notebook(k)+ with _ctx(k, extra_doc_cells=cells) as ctx:+ async with ctx as nb:+ nb.edit_cell("child", code="y = x + 2")+ final_id = nb.create_cell("z = y + 1")+ assert "y" not in k.globals++ if execution_path == "ui":+ await k.run(+ [ExecuteCellCommand(cell_id=final_id, code="z = y + 1")]+ )+ else:+ pending_cells = [c for c in cells if c.id not in k.graph.cells]+ with _ctx(k, extra_doc_cells=pending_cells) as ctx:+ async with ctx as nb:+ nb.edit_cell(str(final_id), code="z = y + 2")+ nb.run_cell(str(final_id))++ assert {name: k.globals[name] for name in ("x", "y", "z")} == {+ "x": 1,+ "y": 3,+ "z": 4 if execution_path == "ui" else 5,+ }+ assert set(k._uninstantiated_execution_requests) == {"unrelated"}++ @pytest.mark.parametrize("explicit_ancestor", [False, True])+ @pytest.mark.parametrize("disabled", [False, True])+ async def test_run_preserves_newer_ancestor_metadata(+ self, any_kernel: Kernel, explicit_ancestor: bool, disabled: bool+ ) -> None:+ k = any_kernel+ cells = await self._open_notebook(k)+ config = CellConfig(+ disabled=disabled, hide_code=True, expand_output=True, column=2+ )+ k.cell_metadata[CellId_t("root")] = CellMetadata(config=config)+ with _ctx(k, extra_doc_cells=cells) as ctx:+ async with ctx as nb:+ nb.edit_cell("child", code="y = x + 2")+ nb.run_cell("child")+ if explicit_ancestor:+ nb.run_cell("root")++ assert k.cell_metadata["root"].config == config+ assert k.graph.cells["root"].config == config+ if disabled:+ assert "x" not in k.globals+ assert "y" not in k.globals+ else:+ assert k.globals["y"] == 3++ async def test_mixed_run_discovers_pending_ancestor(+ self, any_kernel: Kernel+ ) -> None:+ k = any_kernel+ cells = await self._open_notebook(k)+ with _ctx(k, extra_doc_cells=cells) as ctx:+ async with ctx as nb:+ nb.edit_cell("child", code="y = x + 2")+ nb.run_cell("child")+ assert k.globals["y"] == 3+ assert set(k._uninstantiated_execution_requests) == {"unrelated"}+ assert "unrelated" not in k.graph.cells++ @pytest.mark.parametrize("run_edited_cell", [False, True])+ async def test_later_run_does_not_replay_old_source(+ self, any_kernel: Kernel, run_edited_cell: bool+ ) -> None:+ k = any_kernel+ cells = await self._open_notebook(k)+ with _ctx(k, extra_doc_cells=cells) as ctx:+ async with ctx as nb:+ nb.edit_cell("root", code="x = 10")+ if run_edited_cell:+ nb.run_cell("root")+ nb.run_cell("child")+ if not run_edited_cell:+ assert "x" not in k.globals+ await k.run(+ [ExecuteCellCommand(cell_id=CellId_t("child"), code="y = x + 1")]+ )+ assert k.globals["x"] == 10+ assert k.globals["y"] == 11+ assert k.graph.cells["root"].code == "x = 10"+ assert set(k._uninstantiated_execution_requests) == {"unrelated"}++ async def test_later_run_does_not_restore_deleted_pending_cell(+ self, any_kernel: Kernel+ ) -> None:+ k = any_kernel+ cells = await self._open_notebook(k)+ k.cell_metadata[CellId_t("root")] = CellMetadata(+ config=CellConfig(disabled=True, hide_code=True, column=2)+ )+ with _ctx(k, extra_doc_cells=cells) as ctx:+ async with ctx as nb:+ nb.delete_cell("root")+ assert "root" not in k.cell_metadata+ await k.run(+ [ExecuteCellCommand(cell_id=CellId_t("child"), code="y = x + 1")]+ )+ assert "root" not in k.graph.cells+ assert "x" not in k.globals+ assert "y" not in k.globals+ await k.run(+ [ExecuteCellCommand(cell_id=CellId_t("root"), code="x = 42")]+ )+ assert k.cell_metadata["root"].config == CellConfig()+ assert k.globals["x"] == 42++class TestErrorReporting:async def test_print_summary_does_not_duplicate_errors(self, k: Kernel, capsys: pytest.CaptureFixture[str]diff --git a/tests/_runtime/test_dataflow.py b/tests/_runtime/test_dataflow.pyindex 7ddb050187..aa86c70647 100644--- a/tests/_runtime/test_dataflow.py+++ b/tests/_runtime/test_dataflow.py@@ -19,6 +19,103 @@ parse_cell = partial(compiler.compile_cell, cell_id="0")HAS_DUCKDB = DependencyManager.duckdb.has()+ ("sources", "root_indices", "expected_indices"),+ [+ (["x = 1", "y = x + 1", "z = y + 1"], [2], {0, 1}),+ (["x = 1", "y = x + 1", "z = y + 1"], [0, 2], {1}),+ (["x = 1", "x = 2", "y = x"], [2], {0, 1}),+ (["x = 1", "y = x", "del x"], [2], {0, 1}),+ (["x = y", "y = x"], [0], {1}),+ (["x = x + 1"], [0], set()),+ pytest.param(+ [+ 'mo.sql("CREATE TABLE schema1.t1 (i INTEGER)")',+ 'mo.sql("SELECT * FROM schema1.t1")',+ ],+ [1],+ {0},+ marks=pytest.mark.skipif(not HAS_DUCKDB, reason="requires duckdb"),+ ),+ pytest.param(+ ['mo.sql("CREATE TABLE t1 (i INTEGER)")', "print(t1)"],+ [1],+ set(),+ marks=pytest.mark.skipif(not HAS_DUCKDB, reason="requires duckdb"),+ ),+ ],+)[email protected]("reverse_order", [False, True])+def test_ancestors_from_compiled_cells(+ sources: list[str],+ root_indices: list[int],+ expected_indices: set[int],+ reverse_order: bool,+) -> None:+ cells = {+ CellId_t(str(i)): compiler.compile_cell(code, cell_id=CellId_t(str(i)))+ for i, code in enumerate(sources)+ }+ if reverse_order:+ cells = dict(reversed(cells.items()))+ roots = {CellId_t(str(i)) for i in root_indices}+ expected = {CellId_t(str(i)) for i in expected_indices}+ assert dataflow.get_ancestors_from_cells(cells, roots) == expected++ graph = dataflow.DirectedGraph()+ for cid, cell in cells.items():+ graph.register_cell(cid, cell)+ assert (+ set().union(*(graph.ancestors(cid) for cid in roots)) - roots+ == expected+ )++[email protected](not HAS_DUCKDB, reason="requires duckdb")[email protected]("reverse_order", [False, True])+def test_ancestor_analysis_preserves_sql_registration_order(+ reverse_order: bool,+) -> None:+ sources = [+ 'mo.sql("CREATE SCHEMA schema1")',+ 'mo.sql("CREATE TABLE schema1.t1 (i INTEGER)")',+ 'mo.sql("SELECT * FROM schema1.t1")',+ ]+ cells = {+ CellId_t(str(i)): compiler.compile_cell(code, cell_id=CellId_t(str(i)))+ for i, code in enumerate(sources)+ }+ if reverse_order:+ cells = dict(reversed(cells.items()))+ graph = dataflow.DirectedGraph()+ for cid, cell in cells.items():+ graph.register_cell(cid, cell)+ expected = {"0", "1"} if reverse_order else {"1"}+ assert graph.ancestors(CellId_t("2")) == expected+ assert (+ dataflow.get_ancestors_from_cells(cells, {CellId_t("2")}) == expected+ )+++def test_ancestor_analysis_ignores_unknown_roots() -> None:+ cells = {+ CellId_t(cid): compiler.compile_cell(code, cell_id=CellId_t(cid))+ for cid, code in (+ ("root", "x = 1"),+ ("target", "y = x + 1"),+ ("unrelated", "z = 2"),+ )+ }+ assert dataflow.get_ancestors_from_cells(+ cells, {CellId_t("target"), CellId_t("missing")}+ ) == {"root"}+ assert dataflow.get_ancestors_from_cells(cells, set()) == set()+ assert (+ dataflow.get_ancestors_from_cells(cells, {CellId_t("missing")})+ == set()+ )++def test_graph_single_node() -> None:code = "x = 0"graph = dataflow.DirectedGraph()