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.

fix(document): own cell config on ingest instead of aliasing the caller (#10021)

Kiran Gadhave924b013828a0
diff --git a/marimo/_ast/cell_manager.py b/marimo/_ast/cell_manager.py
index 37c3b2d328..e3c02bcbb8 100644
--- a/marimo/_ast/cell_manager.py
+++ b/marimo/_ast/cell_manager.py
@@ -370,6 +370,10 @@ class CellManager:
def configs(self) -> Iterable[CellConfig]:
"""Get an iterator over all cell configurations.
+ The returned configs are owned by the document; treat them as read
+ only and change a cell's config with a SetConfig transaction rather
+ than mutating in place.
+
Returns:
Iterable[CellConfig]: Iterator yielding each cell's configuration
"""
@@ -433,6 +437,10 @@ class CellManager:
def config_map(self) -> dict[CellId_t, CellConfig]:
"""Get a mapping of cell IDs to their configurations.
+ The returned configs are owned by the document; treat them as read
+ only and change a cell's config with a SetConfig transaction rather
+ than mutating in place.
+
Returns:
dict[CellId_t, CellConfig]: Dictionary mapping cell IDs to their configurations
"""
diff --git a/marimo/_messaging/notebook/document.py b/marimo/_messaging/notebook/document.py
index 87c4620c48..2f0f792cd6 100644
--- a/marimo/_messaging/notebook/document.py
+++ b/marimo/_messaging/notebook/document.py
@@ -171,7 +171,9 @@ class NotebookDocument:
id=change.cell_id,
code=change.code,
name=change.name,
- config=change.config,
+ # Own the config: copy on ingest so no two documents (or a
+ # document and its caller) ever share a mutable CellConfig.
+ config=structs_replace(change.config),
)
if change.after is not None:
idx = self._find_index(change.after)
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index bfa0fc1516..f7f2a55d4b 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -995,6 +995,35 @@ class TestApp:
assert cloned_impl.config.disabled is True
assert original_impl.config.disabled is False
+ def test_app_clone_document_configs_are_independent(self) -> None:
+ """clone() must not alias cell configs between the two documents.
+
+ The cloned document owns its own CellConfig; mutating the original
+ document's config must not leak into the clone.
+ """
+ app = App()
+
+ @app.cell(disabled=True)
+ def __():
+ x = 1
+ return (x,)
+
+ clone = app.clone()
+
+ original_cm = InternalApp(app).cell_manager
+ cloned_cm = InternalApp(clone).cell_manager
+
+ original_id = list(original_cm.cell_ids())[0]
+ cloned_id = list(cloned_cm.cell_ids())[0]
+
+ original_config = original_cm.document.get_cell(original_id).config
+ cloned_config = cloned_cm.document.get_cell(cloned_id).config
+
+ assert original_config is not cloned_config
+
+ original_config.configure({"disabled": False})
+ assert cloned_cm.document.get_cell(cloned_id).config.disabled is True
+
def test_to_py(self) -> None:
"""Test that InternalApp.to_py() returns the Python code representation."""
app = App()
diff --git a/tests/_messaging/notebook/test_document.py b/tests/_messaging/notebook/test_document.py
index 0aa7e78d68..f2feebd01e 100644
--- a/tests/_messaging/notebook/test_document.py
+++ b/tests/_messaging/notebook/test_document.py
@@ -181,6 +181,42 @@ class TestCreateCell:
assert cell.config.hide_code is True
assert cell.config.disabled is True
+ def test_copies_config_on_ingest(self) -> None:
+ """CreateCell stores a copy of the config, not the caller's instance.
+
+ Two documents built from the same CellConfig must not share it, and
+ mutating one document's stored config must not affect the other.
+ """
+ shared = CellConfig(disabled=True)
+
+ def build() -> NotebookDocument:
+ doc = NotebookDocument()
+ doc.apply(
+ _tx(
+ CreateCell(
+ cell_id=CellId_t("a"),
+ code="x = 1",
+ name="__",
+ config=shared,
+ )
+ )
+ )
+ return doc
+
+ doc_a = build()
+ doc_b = build()
+
+ config_a = doc_a.get_cell(CellId_t("a")).config
+ config_b = doc_b.get_cell(CellId_t("a")).config
+
+ assert config_a is not shared
+ assert config_b is not shared
+ assert config_a is not config_b
+
+ config_a.configure({"disabled": False})
+ assert doc_b.get_cell(CellId_t("a")).config.disabled is True
+ assert shared.disabled is True
+
# ------------------------------------------------------------------
# DeleteCell