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.

tests: Capture invalid setup cell behavior (#4928)

Dylan Madisettiaf59abdd7cd3
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index ee32ebade2..baf03979e4 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -111,20 +111,27 @@ class _SetupContext:
self._previous: dict[str, Any] = {}
def __enter__(self) -> None:
- with_frame = sys._getframe(1)
-
+ if maybe_frame := inspect.currentframe():
+ with_frame = maybe_frame.f_back
+ else:
+ raise SetupRootError("Unable to establish current frame.")
+ if "app" in self._cell.defs:
+ # Otherwise fail in say a script context.
+ raise SetupRootError("The setup cell cannot redefine 'app'")
if refs := self._cell.refs - BUILTINS:
# Otherwise fail in say a script context.
raise SetupRootError(
f"The setup cell cannot reference any additional variables: {refs}"
)
- self._frame = with_frame
- self._previous = {**with_frame.f_locals}
- # A reference to the key app must be maintained in the frame.
- # This may be a python quirk, so just remove refs to explicit defs
- for var in self._cell.defs:
- if var in self._previous:
- del self._frame.f_locals[var]
+
+ if with_frame is not None:
+ self._frame = with_frame
+ previous = {**with_frame.f_locals}
+ # A reference to the key app must be maintained in the frame.
+ # This may be a python quirk, so just remove refs to explicit defs
+ for var in self._cell.defs:
+ if var in previous:
+ del self._frame.f_locals[var]
def __exit__(
self,
@@ -135,24 +142,14 @@ class _SetupContext:
if exception is not None:
# Always should fail, since static loading still allows bad apps to
# load.
+ # But don't record the variables.
return False
- # Manually hold on to defs for injection into script mode.
if self._frame is not None:
+ # Collect new definitions
for var in self._cell.defs:
- self._glbls[var] = self._frame.f_locals.get(var, None)
-
- self._frame.f_locals.update(self._previous)
- # What was just run should take precident.
- self._frame.f_locals.update(self._glbls)
- # Previous after to ensure that app and other builtins are not
- # changed during import or execution.
- app = None
- if self._cell._app is not None:
- app = self._cell._app._app
- self._frame.f_locals["app"] = self._previous.get("app", app)
- self._previous = {}
-
+ if var in self._frame.f_locals:
+ self._glbls[var] = self._frame.f_locals.get(var)
return False
diff --git a/marimo/_utils/with_skip.py b/marimo/_utils/with_skip.py
index 588c40b8ae..f70660f29b 100644
--- a/marimo/_utils/with_skip.py
+++ b/marimo/_utils/with_skip.py
@@ -1,6 +1,7 @@
# Copyright 2025 Marimo. All rights reserved.
from __future__ import annotations
+import inspect
import sys
from abc import ABC, abstractmethod
from typing import (
@@ -30,7 +31,11 @@ class SkipContext(ABC):
def __enter__(self) -> Self:
self._sys_trace = sys.gettrace()
- frame = sys._getframe(1)
+ if maybe_frame := inspect.currentframe():
+ frame = maybe_frame.f_back
+ else:
+ raise RuntimeError("Unable to establish current frame.")
+ assert frame is not None
# Hold on to the previous trace.
self._old_trace = frame.f_trace
# Setting the frametrace, will cause the function to be run on _every_
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index c14877c54b..226b94b42b 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -18,6 +18,7 @@ from marimo._ast.errors import (
CycleError,
DeleteNonlocalError,
MultipleDefinitionError,
+ SetupRootError,
UnparsableError,
)
from marimo._dependencies.dependencies import DependencyManager
@@ -981,6 +982,40 @@ class TestAppComposition:
assert x.value == 2
assert y.value == 3
+ @staticmethod
+ def test_app_not_changed() -> None:
+ app = App()
+
+ with pytest.raises(SetupRootError):
+ with app.setup:
+ app = 1
+
+
+
+ @staticmethod
+ def test_setup_not_exposed() -> None:
+ app = App()
+
+ with pytest.raises(SetupRootError):
+ with app.setup:
+ try:
+ x = app is not None
+ except NameError:
+ x = False
+
+
+ @staticmethod
+ def test_setup_in_memory() -> None:
+ app = App()
+
+ with app.setup:
+ x = 0
+
+ assert x == 0
+ _, defs = app.run()
+ assert defs["x"] == 0
+ assert "app" not in defs
+
class TestAppKernelRunnerRegistry:
def test_get_runner(self, k: Kernel) -> None: