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 StateRegistry retain_active_states (#5435)
David Buckley9ec52a69a793
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.pyindex 8e7076adb2..68ab0cc475 100644--- a/marimo/_runtime/dataflow.py+++ b/marimo/_runtime/dataflow.py@@ -80,7 +80,10 @@ class DirectedGraph:This is a singleton for well-formed graphs."""- return self.definitions[name]+ if name in self.definitions:+ return self.definitions[name]+ else:+ return set()def get_referring_cells(self, name: Name, language: Literal["python", "sql"]diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.pyindex 2f8c516a0e..2def940a9a 100644--- a/marimo/_runtime/runner/cell_runner.py+++ b/marimo/_runtime/runner/cell_runner.py@@ -551,9 +551,11 @@ class Runner:) -> tuple[str, Optional[CellId_t]]:ref = e.refblamed_cell = None- try:- (blamed_cell, *_) = self.graph.get_defining_cells(ref)- except KeyError:++ definitions = self.graph.get_defining_cells(ref)+ if definitions:+ (blamed_cell, *_) = definitions+ else:# The reference is not found anywhere else in the graph# but it might be privateref, var_cell_id = unmangle_local(ref)diff --git a/marimo/_runtime/state.py b/marimo/_runtime/state.pyindex 45e80124a1..016a1676f6 100644--- a/marimo/_runtime/state.py+++ b/marimo/_runtime/state.py@@ -116,7 +116,7 @@ class StateRegistry:del self._inv_states[id_key]del self._states[state_key]else:- active_state_ids.add(id(self._states[state_key]))+ active_state_ids.add(self._states[state_key].id)# Remove all non-active states by idfor state_id in list(self._inv_states.keys()):diff --git a/tests/_runtime/test_state.py b/tests/_runtime/test_state.pyindex 6c92c8fd4e..687a44347e 100644--- a/tests/_runtime/test_state.py+++ b/tests/_runtime/test_state.py@@ -1,5 +1,6 @@# Copyright 2024 Marimo. All rights reserved.from marimo._runtime.runtime import Kernel+from marimo._runtime.state import State, StateRegistryfrom tests.conftest import ExecReqProvider@@ -218,3 +219,17 @@ async def test_set_state_not_strict_copied(assert id(k.globals["a"]) == id(k.globals["state"])assert id(k.globals["b"]) == id(k.globals["set_state"])+++def test_retain_active_states() -> None:+ state_registry = StateRegistry()+ state = State(None)+ state_registry.register(state, "state")++ assert state_registry.lookup("state")+ assert state_registry.bound_names(state) == {"state"}++ state_registry.retain_active_states({"state"})++ assert state_registry.lookup("state")+ assert state_registry.bound_names(state) == {"state"}