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.

refactor: migrate UIElementId to use NewType (#3843)

devin-ai-integration[bot]ebc52d3a13ec
diff --git a/marimo/_plugins/ui/_core/registry.py b/marimo/_plugins/ui/_core/registry.py
index 10acf292b6..e26858a759 100644
--- a/marimo/_plugins/ui/_core/registry.py
+++ b/marimo/_plugins/ui/_core/registry.py
@@ -15,9 +15,7 @@ else:
from marimo._ast.app import _Namespace
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._runtime.context import get_context
-from marimo._types.ids import CellId_t
-
-UIElementId = str
+from marimo._types.ids import CellId_t, UIElementId
T = TypeVar("T")
@@ -136,7 +134,7 @@ class UIElementRegistry:
def resolve_lens(
self, object_id: UIElementId, value: LensValue[T]
- ) -> tuple[str, LensValue[T]]:
+ ) -> tuple[UIElementId, LensValue[T]]:
"""Resolve a lens, if any, to an object id and value update
Returns (resolved object id, resolved value)
diff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.py
index fa91dbf62a..b1742c3fc6 100644
--- a/marimo/_plugins/ui/_core/ui_element.py
+++ b/marimo/_plugins/ui/_core/ui_element.py
@@ -34,6 +34,7 @@ from marimo._runtime.context import (
get_context,
)
from marimo._runtime.functions import Function
+from marimo._types.ids import UIElementId
if TYPE_CHECKING:
from marimo._plugins.ui._impl.input import form as form_plugin
@@ -64,7 +65,7 @@ class Lens:
id of its parent UI element, and the key at which its parent stores it.
"""
- parent_id: str
+ parent_id: UIElementId
key: str
@@ -214,9 +215,9 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):
# created by each cell is deterministic; this fact is used to
# optionally override the element's initial value.
try:
- self._id = get_context().take_id()
+ self._id = UIElementId(get_context().take_id())
except (ids.NoIDProviderException, ContextNotInitializedError):
- self._id = self._random_id
+ self._id = UIElementId(self._random_id)
self._ctx: RuntimeContext | None
try:
diff --git a/marimo/_runtime/requests.py b/marimo/_runtime/requests.py
index ce93549abb..4f4faf54a4 100644
--- a/marimo/_runtime/requests.py
+++ b/marimo/_runtime/requests.py
@@ -20,13 +20,12 @@ from uuid import uuid4
from marimo._config.config import MarimoConfig
from marimo._data.models import DataTableSource
-from marimo._types.ids import CellId_t
+from marimo._types.ids import CellId_t, UIElementId
if TYPE_CHECKING:
from starlette.datastructures import URL
from starlette.requests import HTTPConnection
-UIElementId = str
CompletionRequestId = str
FunctionCallId = str
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 24d7a40994..c1d4e30895 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -136,7 +136,7 @@ from marimo._runtime.win32_interrupt_handler import Win32InterruptHandler
from marimo._server.model import SessionMode
from marimo._server.types import QueueType
from marimo._tracer import kernel_tracer
-from marimo._types.ids import CellId_t
+from marimo._types.ids import CellId_t, UIElementId
from marimo._utils.assert_never import assert_never
from marimo._utils.platform import is_pyodide
from marimo._utils.signals import restore_signals
@@ -1614,7 +1614,7 @@ class Kernel:
# interacting with a view triggers reactive execution through the
# source (parent).
ctx = get_context()
- resolved_requests: dict[str, Any] = {}
+ resolved_requests: dict[UIElementId, Any] = {}
referring_cells: set[CellId_t] = set()
ui_element_registry = ctx.ui_element_registry
for object_id, value in request.ids_and_values:
diff --git a/marimo/_runtime/utils/set_ui_element_request_manager.py b/marimo/_runtime/utils/set_ui_element_request_manager.py
index 6f315a92fc..0eebb1fc29 100644
--- a/marimo/_runtime/utils/set_ui_element_request_manager.py
+++ b/marimo/_runtime/utils/set_ui_element_request_manager.py
@@ -3,9 +3,9 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any
-from marimo._plugins.ui._core.registry import UIElementId
from marimo._runtime.requests import SetUIElementValueRequest
from marimo._server.types import QueueType
+from marimo._types.ids import UIElementId
if TYPE_CHECKING:
import asyncio
diff --git a/marimo/_types/ids.py b/marimo/_types/ids.py
index 08493ec738..9329f8b208 100644
--- a/marimo/_types/ids.py
+++ b/marimo/_types/ids.py
@@ -6,8 +6,7 @@ from typing import NewType
# CellId_t = NewType("CellId_t", str)
CellId_t = str
-# UIElementId = NewType("UIElementId", str)
-UIElementId = str
+UIElementId = NewType("UIElementId", str)
SessionId = NewType("SessionId", str)