Skip to content

Commit b70293a

Browse files
authored
fix: invalid error thrown when undefined setup variables (#7649)
## 📝 Summary The following fails in script mode: ```python with app.setup: if False: value = ... ``` Since we naively expect `value` to be assigned. This PR makes an exception for setup cells in script mode, enabling this.
1 parent e2c4dd2 commit b70293a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

marimo/_runtime/app/script_runner.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def __init__(
4747
self.cells_cancelled: set[CellId_t] = set()
4848
self._glbls = glbls if glbls else {}
4949

50-
# Prune cells that define provided refs
50+
# Setup cell cannot be overridden, and it's possible that some
51+
# variables are not defined, so ignore it.
5152
pruned_execution_order = dataflow.prune_cells_for_overrides(
52-
self.app.graph, self.app.execution_order, self._glbls
53+
self.app.graph,
54+
self.app.execution_order,
55+
self._glbls,
56+
excluded=CellId_t(SETUP_CELL_NAME),
5357
)
5458

5559
self.cells_to_run = [

marimo/_runtime/dataflow/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from __future__ import annotations
33

44
from collections import deque
5-
from typing import TYPE_CHECKING, Any, Callable
5+
from typing import TYPE_CHECKING, Any, Callable, Optional
66

77
from marimo import _loggers
88
from marimo._ast.cell import CellImpl
@@ -136,6 +136,7 @@ def prune_cells_for_overrides(
136136
graph: DirectedGraph,
137137
execution_order: Collection[CellId_t],
138138
overrides: dict[str, Any],
139+
excluded: Optional[CellId_t] = None,
139140
) -> list[CellId_t]:
140141
"""Prune cells from execution when their definitions are overridden.
141142
@@ -148,6 +149,7 @@ def prune_cells_for_overrides(
148149
graph: The dataflow graph containing cell dependencies
149150
execution_order: Ordered collection of cells to execute
150151
overrides: Dictionary mapping variable names to their override values
152+
excluded: CellId to ignore for closure determination.
151153
152154
Returns:
153155
Filtered execution order excluding cells whose definitions are overridden
@@ -178,6 +180,8 @@ def prune_cells_for_overrides(
178180
# Validate that all definitions from pruned cells are provided
179181
missing_defs: set[str] = set()
180182
for cell_id in cells_to_prune:
183+
if cell_id == excluded:
184+
continue
181185
cell = graph.cells[cell_id]
182186
# Check all definitions this cell would have provided
183187
for missing in cell.defs - refs:

tests/_ast/test_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ def normal_cell() -> tuple[int]:
195195
assert defs["normal_var"] == 100
196196
assert "setup_var" in defs # setup still ran
197197

198+
@staticmethod
199+
def test_run_with_undefined_refs_in_setup_cell() -> None:
200+
"""Test that overriding setup cell definitions raises IncompleteRefsError."""
201+
app = App()
202+
203+
with app.setup:
204+
import os
205+
a = 1
206+
if a > 2:
207+
setup_var = "from_setup"
208+
209+
@app.cell
210+
def use_setup(setup_var: str) -> tuple[str]:
211+
result = f"Used {setup_var}"
212+
return (result,)
213+
214+
# Test: Trying to override setup cell variables should fail
215+
# Ensure this doesn't incorrectly raise a IncompleteRefsError
216+
with pytest.raises(NameError) as exc_info:
217+
app.run()
218+
assert "setup_var" in str(exc_info.value)
219+
220+
198221
@staticmethod
199222
def test_setup() -> None:
200223
app = App()

0 commit comments

Comments
 (0)