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.

Avoid recursively scanning anywidget state (#10254)

Trevor Manz24ddbe7c221d
diff --git a/marimo/_output/formatters/anywidget_formatters.py b/marimo/_output/formatters/anywidget_formatters.py
index 90f5e6a9dd..2ef3752336 100644
--- a/marimo/_output/formatters/anywidget_formatters.py
+++ b/marimo/_output/formatters/anywidget_formatters.py
@@ -15,13 +15,6 @@ class AnyWidgetFormatter(FormatterFactory):
import anywidget # type: ignore [import-not-found]
from marimo._output import formatting
- from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
- )
-
- # Patch the comm library so anywidget's descriptor API
- # (MimeBundleDescriptor) creates comms that work in marimo.
- patch_comm_create()
@formatting.formatter(anywidget.AnyWidget)
def _from(lmap: anywidget.AnyWidget) -> tuple[KnownMimeType, str]:
diff --git a/marimo/_output/formatters/formatters.py b/marimo/_output/formatters/formatters.py
index ff4222dce8..1163a39628 100644
--- a/marimo/_output/formatters/formatters.py
+++ b/marimo/_output/formatters/formatters.py
@@ -195,6 +195,14 @@ def register_formatters(theme: Theme = "light") -> None:
case, the trade-off is worth it.
"""
+ # Descriptor-based anywidgets open their comm before formatting. Install
+ # the comm provider eagerly because local modules skip formatter hooks.
+ from marimo._plugins.ui._impl.anywidget.comm_provider import (
+ install_anywidget_comm_provider,
+ )
+
+ install_anywidget_comm_provider()
+
# For modules that are already imported, register their formatters
# immediately; their import hook wouldn't be triggered since they are
# already imported. This is relevant when executing as a script.
diff --git a/marimo/_plugins/ui/_impl/anywidget/comm_provider.py b/marimo/_plugins/ui/_impl/anywidget/comm_provider.py
index 3c99c0a895..9d55912857 100644
--- a/marimo/_plugins/ui/_impl/anywidget/comm_provider.py
+++ b/marimo/_plugins/ui/_impl/anywidget/comm_provider.py
@@ -8,7 +8,7 @@ marimo's notification system instead of being silent no-ops.
from __future__ import annotations
-from typing import Any
+from typing import TYPE_CHECKING, Any
from uuid import uuid4
from marimo._loggers import marimo_logger
@@ -22,6 +22,11 @@ from marimo._types.ids import WidgetModelId
LOGGER = marimo_logger()
+if TYPE_CHECKING:
+ from collections.abc import Callable
+
+_PATCHED_CREATE_COMM: Callable[..., Any] | None = None
+
def _is_anywidget_comm(target_name: str, data: dict[str, Any] | None) -> bool:
"""Check if this comm is being opened by anywidget.
@@ -37,8 +42,8 @@ def _is_anywidget_comm(target_name: str, data: dict[str, Any] | None) -> bool:
return bool(state.get("_model_module") == "anywidget")
-def patch_comm_create() -> None:
- """Replace `comm.create_comm` with a marimo-backed implementation.
+def install_anywidget_comm_provider() -> None:
+ """Install a marimo-backed comm provider for descriptor anywidgets.
Only intercepts comms created by anywidget (identified by
`target_name` and `_model_module`). All other comms fall
@@ -46,12 +51,17 @@ def patch_comm_create() -> None:
This is idempotent -- calling it multiple times is safe.
"""
+ global _PATCHED_CREATE_COMM
+
try:
import comm
from comm import DummyComm
except ImportError:
return
+ if comm.create_comm is _PATCHED_CREATE_COMM:
+ return
+
def _marimo_create_comm(
*,
target_name: str = "comm",
@@ -101,4 +111,5 @@ def patch_comm_create() -> None:
pass
return c
+ _PATCHED_CREATE_COMM = _marimo_create_comm
comm.create_comm = _marimo_create_comm # type: ignore[assignment]
diff --git a/marimo/_plugins/ui/_impl/anywidget/widget_ref.py b/marimo/_plugins/ui/_impl/anywidget/widget_ref.py
deleted file mode 100644
index 14ffb829bd..0000000000
--- a/marimo/_plugins/ui/_impl/anywidget/widget_ref.py
+++ /dev/null
@@ -1,102 +0,0 @@
-# Copyright 2026 Marimo. All rights reserved.
-from __future__ import annotations
-
-import sys
-from typing import Any
-
-_WIDGET_REF_PREFIX = "anywidget:"
-
-
-def _is_anywidget_state(value: Any) -> bool:
- """Return whether an opening model state belongs to an anywidget."""
- return isinstance(value, dict) and (
- "_esm" in value or value.get("_model_module") == "anywidget"
- )
-
-
-def _try_get_widget_model_id(value: Any) -> str | None:
- """Return the model id of a value if it looks like an anywidget.
-
- Detects ipywidgets-derived `AnyWidget` instances through `model_id` and
- protocol-based widgets through anywidget's `MimeBundleDescriptor`.
- Accessing the descriptor intentionally opens the child's comm when it has
- not been displayed independently.
- """
- model_id = getattr(value, "model_id", None)
- if isinstance(model_id, str) and model_id:
- return model_id
-
- # A descriptor-backed widget can only exist after this module has been
- # imported. Reuse it instead of importing an optional dependency while
- # inspecting arbitrary state values. A failed submodule import can leave a
- # partial `anywidget` module in `sys.modules`.
- descriptor_module = sys.modules.get("anywidget._descriptor")
- if descriptor_module is None:
- return None
- descriptor_type = getattr(descriptor_module, "MimeBundleDescriptor", None)
- repr_type = getattr(descriptor_module, "ReprMimeBundle", None)
- if not isinstance(descriptor_type, type) or not isinstance(
- repr_type, type
- ):
- return None
-
- bundle = getattr(value, "_repr_mimebundle_", None)
- if isinstance(bundle, descriptor_type):
- descriptor_get = getattr(bundle, "__get__", None)
- if not callable(descriptor_get):
- return None
- bundle = descriptor_get(value, type(value))
- if isinstance(bundle, repr_type):
- bundle_id = getattr(bundle, "model_id", None)
- if not isinstance(bundle_id, str) or not bundle_id:
- comm = getattr(bundle, "_comm", None)
- bundle_id = getattr(comm, "comm_id", None)
- if isinstance(bundle_id, str) and bundle_id:
- return bundle_id
- return None
-
-
-def _replace_widget_refs(value: Any) -> Any:
- """Recursively replace anywidget instances with wire-format references.
-
- Walks dictionaries, lists, and tuples. Returns a new container when a
- replacement occurs and otherwise preserves the original container's
- identity.
- """
- model_id = _try_get_widget_model_id(value)
- if model_id is not None:
- return f"{_WIDGET_REF_PREFIX}{model_id}"
- if isinstance(value, dict):
- replaced = {k: _replace_widget_refs(v) for k, v in value.items()}
- if all(replaced[k] is value[k] for k in value):
- return value
- return replaced
- if isinstance(value, list):
- replaced_list = [_replace_widget_refs(v) for v in value]
- if all(a is b for a, b in zip(replaced_list, value, strict=True)):
- return value
- return replaced_list
- if isinstance(value, tuple):
- replaced_tuple = tuple(_replace_widget_refs(v) for v in value)
- if all(a is b for a, b in zip(replaced_tuple, value, strict=True)):
- return value
- return replaced_tuple
- return value
-
-
-class AnyWidgetStateSerializer:
- """Serialize widget references for one anywidget comm.
-
- Classification happens once from the complete opening state. Subsequent
- updates are partial and therefore cannot reliably identify the model on
- their own.
- """
-
- def __init__(self, initial_state: Any) -> None:
- self._enabled = _is_anywidget_state(initial_state)
-
- def serialize(self, state: Any) -> Any:
- """Replace widget references when this comm belongs to an anywidget."""
- if not self._enabled:
- return state
- return _replace_widget_refs(state)
diff --git a/marimo/_plugins/ui/_impl/comm.py b/marimo/_plugins/ui/_impl/comm.py
index d27233dc38..f02bf6ac95 100644
--- a/marimo/_plugins/ui/_impl/comm.py
+++ b/marimo/_plugins/ui/_impl/comm.py
@@ -16,9 +16,6 @@ from marimo._messaging.notification import (
ModelUpdate,
)
from marimo._messaging.notification_utils import broadcast_notification
-from marimo._plugins.ui._impl.anywidget.widget_ref import (
- AnyWidgetStateSerializer,
-)
from marimo._runtime.commands import (
ModelCommand,
ModelUpdateMessage,
@@ -210,10 +207,6 @@ class MarimoComm:
LOGGER.debug("Opening comm %s", self.comm_id)
data = dict(data or {})
state = data.get("state", {})
- self._state_serializer = AnyWidgetStateSerializer(state)
- state = self._state_serializer.serialize(state)
- if "state" in data:
- data["state"] = state
esm = state.get("_esm")
self.esm_spec: EsmSpec | None = (
EsmSpec.from_esm(esm) if isinstance(esm, str) and esm else None
@@ -257,9 +250,6 @@ class MarimoComm:
LOGGER.debug("Sending comm message %s", self.comm_id)
data = dict(data or {})
state = data.get("state")
- state = self._state_serializer.serialize(state)
- if "state" in data:
- data["state"] = state
changed_spec: EsmSpec | None = None
if isinstance(state, dict) and "_esm" in state:
esm = state["_esm"]
diff --git a/marimo/_plugins/ui/_impl/from_anywidget.py b/marimo/_plugins/ui/_impl/from_anywidget.py
index 287f4e5a21..e175babed1 100644
--- a/marimo/_plugins/ui/_impl/from_anywidget.py
+++ b/marimo/_plugins/ui/_impl/from_anywidget.py
@@ -17,9 +17,6 @@ from marimo import _loggers
from marimo._messaging.mimetypes import KnownMimeType
from marimo._output.rich_help import mddoc
from marimo._plugins.ui._core.ui_element import InitializationArgs, UIElement
-from marimo._plugins.ui._impl.anywidget.widget_ref import (
- AnyWidgetStateSerializer,
-)
from marimo._plugins.ui._impl.comm import MarimoComm
from marimo._types.ids import WidgetModelId
from marimo._utils.methods import getcallable
@@ -139,20 +136,15 @@ def get_anywidget_state(widget: AnyWidget) -> AnyWidgetState:
}
state: dict[str, Any] = widget.get_state()
- serializer = AnyWidgetStateSerializer(state)
# Filter out system traits from the serialized state
# This should include the binary data,
# see marimo/_smoke_tests/issues/2366-anywidget-binary.py
filtered = {k: v for k, v in state.items() if k not in ignored_traits}
- # Replace nested AnyWidget values with `anywidget:<model_id>` strings
- # so the frontend can resolve them via `host.getWidget(ref)`. Children
- # already have their comms opened (ipywidgets'
- # `Widget.on_widget_constructed` fires `init_marimo_widget` on
- # construction), so the frontend has the model registered by the time
- # the parent's state arrives.
- return cast(AnyWidgetState, serializer.serialize(filtered))
+ # Trait serializers, such as anywidget's `WidgetTrait`, are responsible
+ # for converting Python values to their wire representation.
+ return cast(AnyWidgetState, filtered)
def get_anywidget_model_id(widget: AnyWidget) -> WidgetModelId:
diff --git a/tests/_plugins/ui/_impl/anywidget/test_comm_provider.py b/tests/_plugins/ui/_impl/anywidget/test_comm_provider.py
index cd0484080f..50a4295c09 100644
--- a/tests/_plugins/ui/_impl/anywidget/test_comm_provider.py
+++ b/tests/_plugins/ui/_impl/anywidget/test_comm_provider.py
@@ -1,6 +1,8 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import sys
+
import pytest
from marimo._dependencies.dependencies import DependencyManager
@@ -15,26 +17,28 @@ class TestPatchCommCreate:
import comm
from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
+ install_anywidget_comm_provider,
)
original = comm.create_comm
- patch_comm_create()
- assert comm.create_comm is not original
- # Restore
- comm.create_comm = original
+ comm.create_comm = comm.DummyComm
+ try:
+ install_anywidget_comm_provider()
+ assert comm.create_comm is not comm.DummyComm
+ finally:
+ comm.create_comm = original
@staticmethod
def test_anywidget_comm_returns_marimo_comm() -> None:
import comm
from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
+ install_anywidget_comm_provider,
)
from marimo._plugins.ui._impl.comm import MarimoComm
original = comm.create_comm
- patch_comm_create()
+ install_anywidget_comm_provider()
try:
c = comm.create_comm(
target_name="jupyter.widget",
@@ -64,11 +68,11 @@ class TestPatchCommCreate:
from comm import DummyComm
from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
+ install_anywidget_comm_provider,
)
original = comm.create_comm
- patch_comm_create()
+ install_anywidget_comm_provider()
try:
c = comm.create_comm(
target_name="some.other.target",
@@ -83,18 +87,36 @@ class TestPatchCommCreate:
import comm
from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
+ install_anywidget_comm_provider,
)
original = comm.create_comm
- patch_comm_create()
- first = comm.create_comm
- patch_comm_create()
- second = comm.create_comm
- # Second patch wraps the first — both should work
- assert first is not original
- assert second is not original
- comm.create_comm = original
+ comm.create_comm = comm.DummyComm
+ try:
+ install_anywidget_comm_provider()
+ first = comm.create_comm
+ install_anywidget_comm_provider()
+ assert comm.create_comm is first
+ finally:
+ comm.create_comm = original
+
+ @staticmethod
+ def test_register_formatters_installs_provider_without_anywidget() -> None:
+ import comm
+
+ from marimo._output.formatters.formatters import register_formatters
+
+ original = comm.create_comm
+ anywidget = sys.modules.pop("anywidget", None)
+ comm.create_comm = comm.DummyComm
+ try:
+ register_formatters()
+ assert comm.create_comm is not comm.DummyComm
+ assert "anywidget" not in sys.modules
+ finally:
+ comm.create_comm = original
+ if anywidget is not None:
+ sys.modules["anywidget"] = anywidget
@pytest.mark.skipif(not HAS_DEPS, reason="anywidget/comm not installed")
@@ -107,11 +129,11 @@ class TestMaybeAsAnywidgetHtml:
_maybe_as_anywidget_html,
)
from marimo._plugins.ui._impl.anywidget.comm_provider import (
- patch_comm_create,
+ install_anywidget_comm_provider,
)
original = comm.create_comm
- patch_comm_create()
+ install_anywidget_comm_provider()
try:
esm = (
"export default { render({ el }) { el.textContent = 'hi'; } }"
diff --git a/tests/_plugins/ui/_impl/test_anywidget.py b/tests/_plugins/ui/_impl/test_anywidget.py
index 6355993478..24ae3edf9c 100644
--- a/tests/_plugins/ui/_impl/test_anywidget.py
+++ b/tests/_plugins/ui/_impl/test_anywidget.py
@@ -559,20 +559,22 @@ x = as_marimo_element.count
assert "_view_name" not in state
@staticmethod
- async def test_nested_widget_serializes_as_anywidget_ref() -> None:
- """A widget-valued trait should produce `anywidget:<id>` on the wire.
-
- Composition (RFC 0001): the parent's frontend `render` calls
- `host.getWidget(model.get("child"))` which expects this format.
- """
+ async def test_trait_serialization_is_preserved() -> None:
+ """Widget references are serialized by the trait, not marimo."""
class Child(_anywidget.AnyWidget):
_esm = "export default { render() {} }"
value = traitlets.Int(0).tag(sync=True)
+ def serialize_child(child: Child, _parent: object) -> str:
+ return f"anywidget:{child._model_id}"
+
class Parent(_anywidget.AnyWidget):
_esm = "export default { render() {} }"
- child = traitlets.Instance(Child, allow_none=True).tag(sync=True)
+ child = traitlets.Instance(Child).tag(
+ sync=True,
+ to_json=serialize_child,
+ )
child = Child(value=7)
parent = Parent(child=child)
@@ -580,51 +582,9 @@ x = as_marimo_element.count
state = get_anywidget_state(parent)
assert state["child"] == f"anywidget:{child._model_id}"
- @staticmethod
- async def test_nested_widget_in_dict_serializes_as_ref() -> None:
- class Child(_anywidget.AnyWidget):
- _esm = "export default { render() {} }"
-
- class Parent(_anywidget.AnyWidget):
- _esm = "export default { render() {} }"
- # Avoid the name `layout` — it's a reserved ipywidgets system
- # trait that `get_anywidget_state` filters out.
- slots = traitlets.Dict().tag(sync=True)
-
- left = Child()
- right = Child()
- parent = Parent(slots={"left": left, "right": right, "gap": 8})
-
- state = get_anywidget_state(parent)
- assert state["slots"] == {
- "left": f"anywidget:{left._model_id}",
- "right": f"anywidget:{right._model_id}",
- "gap": 8,
- }
-
- @staticmethod
- async def test_nested_widget_in_list_serializes_as_ref() -> None:
- class Child(_anywidget.AnyWidget):
- _esm = "export default { render() {} }"
-
- class Parent(_anywidget.AnyWidget):
- _esm = "export default { render() {} }"
- items = traitlets.List().tag(sync=True)
-
- c1 = Child()
- c2 = Child()
- parent = Parent(items=[c1, "spacer", c2])
-
- state = get_anywidget_state(parent)
- assert state["items"] == [
- f"anywidget:{c1._model_id}",
- "spacer",
- f"anywidget:{c2._model_id}",
- ]
-
@staticmethod
async def test_state_without_widgets_passes_through() -> None:
- """Walker should not change values when nothing is a widget."""
+ """Plain state should pass through without another traversal."""
class Plain(_anywidget.AnyWidget):
_esm = "export default { render() {} }"
@@ -634,43 +594,6 @@ x = as_marimo_element.count
state = get_anywidget_state(widget)
assert state["data"] == {"a": 1, "b": [2, 3], "c": {"d": "x"}}
- @staticmethod
- async def test_protocol_widget_serializes_as_anywidget_ref() -> None:
- """RFC 0001 protocol widgets — plain classes with a
- `MimeBundleDescriptor` for `_repr_mimebundle_` — should be detected
- and serialized the same way as ipywidgets-derived AnyWidget."""
- from dataclasses import dataclass
-
- from anywidget._descriptor import MimeBundleDescriptor
-
- @dataclass
- class ProtocolChild:
- value: int = 0
- # Dataclass autodetect lets the descriptor expose `value` as
- # synced state without a manual `_get_anywidget_state` method.
- _repr_mimebundle_ = MimeBundleDescriptor(_esm="")
-
- class Parent(_anywidget.AnyWidget):
- _esm = "export default { render() {} }"
- child = traitlets.Any(allow_none=True).tag(sync=True)
-
- protocol_child = ProtocolChild(value=7)
- # Important: do NOT touch `_repr_mimebundle_` first — the walker
- # must trigger comm creation itself. Otherwise the test isn't
- # exercising the lazy-init path that protocol widgets rely on.
- parent = Parent(child=protocol_child)
- state = get_anywidget_state(parent)
-
- ref = state["child"]
- assert isinstance(ref, str)
- assert ref.startswith("anywidget:")
- # The id baked into the ref should match the live bundle's id.
- # `model_id` lands on `ReprMimeBundle` in anywidget>=0.11; older
- # versions only expose the underlying comm.
- bundle = protocol_child._repr_mimebundle_
- expected_id = getattr(bundle, "model_id", None) or bundle._comm.comm_id
- assert ref == f"anywidget:{expected_id}"
-
@staticmethod
async def test_partial_state_updates() -> None:
class MultiTraitWidget(_anywidget.AnyWidget):
diff --git a/tests/_plugins/ui/_impl/test_comm.py b/tests/_plugins/ui/_impl/test_comm.py
index ca82e8896d..0cf1224a21 100644
--- a/tests/_plugins/ui/_impl/test_comm.py
+++ b/tests/_plugins/ui/_impl/test_comm.py
@@ -2,15 +2,11 @@ from __future__ import annotations
import sys
import threading
-from dataclasses import dataclass
from unittest.mock import MagicMock, patch
import pytest
from marimo._plugins.ui._impl.anywidget.init import CommLifecycleItem
-from marimo._plugins.ui._impl.anywidget.widget_ref import (
- AnyWidgetStateSerializer,
-)
from marimo._plugins.ui._impl.comm import (
MarimoComm,
MarimoCommManager,
@@ -440,32 +436,10 @@ class TestEsmSpecMinting:
assert result.message.esm_spec == notification.message.esm_spec
-@dataclass(frozen=True)
-class _ModelIdWidget:
- """Minimal ipywidgets-backed anywidget shape for wire transport tests."""
-
- model_id: str
-
-
class TestAnywidgetWireReferences:
ESM = "export default { render() {} }"
- def test_plain_state_does_not_import_optional_anywidget(
- self, monkeypatch: pytest.MonkeyPatch
- ) -> None:
- for module_name in list(sys.modules):
- if module_name == "anywidget" or module_name.startswith(
- "anywidget."
- ):
- monkeypatch.delitem(sys.modules, module_name)
-
- serializer = AnyWidgetStateSerializer({"_esm": self.ESM})
- state = {"count": 1, "label": "counter"}
-
- assert serializer.serialize(state) is state
- assert "anywidget" not in sys.modules
-
- def test_model_open_replaces_direct_and_nested_widget_refs(
+ def test_model_open_preserves_serialized_widget_refs(
self, comm_manager: MarimoCommManager
) -> None:
from marimo._messaging.serde import (
@@ -473,7 +447,7 @@ class TestAnywidgetWireReferences:
serialize_kernel_message,
)
- child = _ModelIdWidget("child-open")
+ child = "anywidget:child-open"
with patch(
"marimo._plugins.ui._impl.comm.broadcast_notification"
) as mock_broadcast:
@@ -510,10 +484,10 @@ class TestAnywidgetWireReferences:
)
assert roundtripped.message.state == expected_state
- def test_model_update_replaces_reassigned_widget_ref(
+ def test_model_update_preserves_serialized_widget_ref(
self, comm_manager: MarimoCommManager
) -> None:
- replacement = _ModelIdWidget("child-update")
+ replacement = "anywidget:child-update"
with patch(
"marimo._plugins.ui._impl.comm.broadcast_notification"
) as mock_broadcast: