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: strict execution single cell reference error (#1686)
Dylan Madisetti89bc10549297
diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsxindex 2a757f462f..53841db7ff 100644--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx@@ -157,7 +157,7 @@ export const MarimoErrorOutput = ({<Tip>Ensure that <CellLinkError cellId={error.blamed_cell as CellId} />- defines the variable {error.ref}, or turn off strict+ defines the variable `{error.ref}`, or turn off strictexecution.</Tip></div>@@ -166,7 +166,12 @@ export const MarimoErrorOutput = ({titleContents = "Ancestor prevented from running";alertVariant = "default";textColor = "text-secondary-foreground";- return (+ return error.blamed_cell == null ? (+ <div key={idx}>+ {error.msg}+ (<CellLinkError cellId={error.raising_cell as CellId} />)+ </div>+ ) : (<div key={idx}>{error.msg}(<CellLinkError cellId={error.raising_cell as CellId} />diff --git a/marimo/_messaging/errors.py b/marimo/_messaging/errors.pyindex 5151b775ae..b39468fcc5 100644--- a/marimo/_messaging/errors.py+++ b/marimo/_messaging/errors.py@@ -37,7 +37,7 @@ class MarimoInterruptionError:class MarimoAncestorPreventedError:msg: strraising_cell: CellId_t- blamed_cell: CellId_t+ blamed_cell: Optional[CellId_t]type: Literal["ancestor-prevented"] = "ancestor-prevented"@@ -73,7 +73,7 @@ class UnknownError:class MarimoStrictExecutionError:msg: strref: str- blamed_cell: CellId_t+ blamed_cell: Optional[CellId_t]type: Literal["strict-exception"] = "strict-exception"diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.pyindex 8f77afb30b..44b8ccaad6 100644--- a/marimo/_runtime/dataflow.py+++ b/marimo/_runtime/dataflow.py@@ -362,7 +362,7 @@ class DirectedGraph:processed.add(maybe_private)if inclusive:- return processed+ return processed | refsreturn processed - refsdiff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.pyindex 2a0b0115c5..f6b9db1ac9 100644--- a/marimo/_runtime/runner/cell_runner.py+++ b/marimo/_runtime/runner/cell_runner.py@@ -350,8 +350,8 @@ class Runner:try:(blamed_cell, *_) = self.graph.get_defining_cells(e.ref)except KeyError:- # This should never happen, but just in case- blamed_cell = e.ref+ # The reference is not found anywhere else in the graph+ blamed_cell = Noneoutput = MarimoStrictExecutionError(f"marimo was unable to resolve "diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 2c9fd08cca..45772d744c 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -26,7 +26,12 @@ from marimo._ast.visitor import Namefrom marimo._config.config import ExecutionType, MarimoConfig, OnCellChangeTypefrom marimo._data.preview_column import get_column_previewfrom marimo._messaging.cell_output import CellChannel-from marimo._messaging.errors import Error, MarimoSyntaxError, UnknownError+from marimo._messaging.errors import (+ Error,+ MarimoStrictExecutionError,+ MarimoSyntaxError,+ UnknownError,+)from marimo._messaging.ops import (Alert,CellOp,@@ -932,6 +937,13 @@ class Kernel:isolated=is_python_isolated(),).broadcast()+ def propagate_kernel_errors(+ runner: cell_runner.Runner,+ ) -> None:+ for cell_id, error in runner.exceptions.items():+ if isinstance(error, MarimoStrictExecutionError):+ self.errors[cell_id] = (error,)+runner = cell_runner.Runner(roots=roots,graph=self.graph,@@ -944,7 +956,8 @@ class Kernel:preparation_hooks=PREPARATION_HOOKS + [invalidate_state],pre_execution_hooks=PRE_EXECUTION_HOOKS,post_execution_hooks=POST_EXECUTION_HOOKS,- on_finish_hooks=ON_FINISH_HOOKS + [broadcast_missing_packages],+ on_finish_hooks=ON_FINISH_HOOKS+ + [broadcast_missing_packages, propagate_kernel_errors],)# I/Odiff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 7a92c317b1..868858ffd5 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -2,6 +2,7 @@from __future__ import annotationsimport sys+import textwrapfrom typing import TYPE_CHECKING, Sequenceimport pytest@@ -11,6 +12,7 @@ from marimo._messaging.errors import (CycleError,DeleteNonlocalError,Error,+ MarimoStrictExecutionError,MultipleDefinitionError,)from marimo._messaging.ops import CellOp@@ -428,9 +430,16 @@ class TestExecution:)assert "x" not in k.globalsassert "y" not in k.globals- assert set(k.errors.keys()) == {"0", "1"}+ if k.execution_type == "strict":+ # 2 isn't valid in strict mode because it will refuse to execute+ # due to missing x.+ assert set(k.errors.keys()) == {"0", "1", "2"}+ else:+ assert set(k.errors.keys()) == {"0", "1"}assert len(k.errors["0"]) == 1assert len(k.errors["1"]) == 1+ if k.execution_type == "strict":+ assert len(k.errors["2"]) == 1_check_edges(k.errors["0"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])_check_edges(k.errors["1"][0], [("0", ["x"], "1"), ("1", ["y"], "0")])@@ -467,7 +476,12 @@ class TestExecution:# break cycle by deleting cellawait k.delete(DeleteCellRequest(cell_id="1"))- assert not k.errors+ if k.execution_type == "strict":+ # Still invalid in strict mode because y is missing.+ assert set(k.errors.keys()) == {"0"}+ assert isinstance(k.errors["0"][0], MarimoStrictExecutionError)+ else:+ assert not k.errorsasync def test_delete_nonlocal_error(self, any_kernel: Kernel) -> None:k = any_kernel@@ -480,7 +494,10 @@ class TestExecution:)assert "y" not in k.globalsassert "z" not in k.globals- assert set(k.errors.keys()) == {"1"}+ if k.execution_type == "strict":+ assert set(k.errors.keys()) == {"1", "2"}+ else:+ assert set(k.errors.keys()) == {"1"}assert k.errors["1"] == (DeleteNonlocalError("x", ("0",)),)# fix cell 1, should run cell 1 and 2@@ -510,7 +527,8 @@ class TestExecution:# Delete the cell that defines x. There shouldn't be any more errors# because x no longer exists.await k.delete(DeleteCellRequest(cell_id="0"))- assert not k.errors+ if k.execution_type != "strict":+ assert not k.errors# Add x back in.await k.run([ExecutionRequest(cell_id="2", code="x=0")])@@ -994,6 +1012,35 @@ class TestStrictExecution:else:assert k.globals["V1"] == 11+ @staticmethod+ async def test_wont_execute_bad_ref(execution_kernel: Kernel) -> None:+ k = execution_kernel+ await k.run(+ [+ ExecutionRequest(+ cell_id="0",+ code=textwrap.dedent("""+ try:+ missing+ except:+ pass+ x = 1+ """),+ ),+ ]+ )++ if k.execution_type == "strict":+ assert "x" not in k.globals+ assert set(k.errors.keys()) == {"0"}+ assert len(k.errors["0"]) == 1+ assert isinstance(k.errors["0"][0], MarimoStrictExecutionError)+ assert k.errors["0"][0].ref == "missing"+ # Check that normal execution still runs the block+ else:+ assert "x" in k.globals+ assert not k.errors+class TestStoredOutput:async def test_ui_element_in_output_stored(