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: invalid error thrown when undefined setup variables (#7649)
Dylan Madisettib70293a7f077
diff --git a/marimo/_runtime/app/script_runner.py b/marimo/_runtime/app/script_runner.pyindex 5530864c20..37f2b35c05 100644--- a/marimo/_runtime/app/script_runner.py+++ b/marimo/_runtime/app/script_runner.py@@ -47,9 +47,13 @@ class AppScriptRunner:self.cells_cancelled: set[CellId_t] = set()self._glbls = glbls if glbls else {}- # Prune cells that define provided refs+ # Setup cell cannot be overridden, and it's possible that some+ # variables are not defined, so ignore it.pruned_execution_order = dataflow.prune_cells_for_overrides(- self.app.graph, self.app.execution_order, self._glbls+ self.app.graph,+ self.app.execution_order,+ self._glbls,+ excluded=CellId_t(SETUP_CELL_NAME),)self.cells_to_run = [diff --git a/marimo/_runtime/dataflow/__init__.py b/marimo/_runtime/dataflow/__init__.pyindex 1654e731ce..0897cd4048 100644--- a/marimo/_runtime/dataflow/__init__.py+++ b/marimo/_runtime/dataflow/__init__.py@@ -2,7 +2,7 @@from __future__ import annotationsfrom collections import deque-from typing import TYPE_CHECKING, Any, Callable+from typing import TYPE_CHECKING, Any, Callable, Optionalfrom marimo import _loggersfrom marimo._ast.cell import CellImpl@@ -136,6 +136,7 @@ def prune_cells_for_overrides(graph: DirectedGraph,execution_order: Collection[CellId_t],overrides: dict[str, Any],+ excluded: Optional[CellId_t] = None,) -> list[CellId_t]:"""Prune cells from execution when their definitions are overridden.@@ -148,6 +149,7 @@ def prune_cells_for_overrides(graph: The dataflow graph containing cell dependenciesexecution_order: Ordered collection of cells to executeoverrides: Dictionary mapping variable names to their override values+ excluded: CellId to ignore for closure determination.Returns:Filtered execution order excluding cells whose definitions are overridden@@ -178,6 +180,8 @@ def prune_cells_for_overrides(# Validate that all definitions from pruned cells are providedmissing_defs: set[str] = set()for cell_id in cells_to_prune:+ if cell_id == excluded:+ continuecell = graph.cells[cell_id]# Check all definitions this cell would have providedfor missing in cell.defs - refs:diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.pyindex 69df3babde..9e53b44ab1 100644--- a/tests/_ast/test_app.py+++ b/tests/_ast/test_app.py@@ -195,6 +195,29 @@ class TestApp:assert defs["normal_var"] == 100assert "setup_var" in defs # setup still ran+ @staticmethod+ def test_run_with_undefined_refs_in_setup_cell() -> None:+ """Test that overriding setup cell definitions raises IncompleteRefsError."""+ app = App()++ with app.setup:+ import os+ a = 1+ if a > 2:+ setup_var = "from_setup"++ @app.cell+ def use_setup(setup_var: str) -> tuple[str]:+ result = f"Used {setup_var}"+ return (result,)++ # Test: Trying to override setup cell variables should fail+ # Ensure this doesn't incorrectly raise a IncompleteRefsError+ with pytest.raises(NameError) as exc_info:+ app.run()+ assert "setup_var" in str(exc_info.value)++@staticmethoddef test_setup() -> None:app = App()