Skip to content

Commit ca31563

Browse files
committed
Address review: add type-level test, tidy docstrings and imports
- Add basedpyright type-level tests (TestDictionaryTyping) that fail on the old invariant dict[...] param and pass on the covariant Mapping[...] one, so the typing change is actually guarded (runtime tests can't enforce annotations) - Update the batch/dictionary docstrings that still referred to dict - Hoist the MappingProxyType / validate_and_clone imports to module scope - Move the validate_and_clone test next to the other batch tests
1 parent eb5d27b commit ca31563

5 files changed

Lines changed: 48 additions & 23 deletions

File tree

‎marimo/_plugins/ui/_impl/batch.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ class batch(_batch_base):
204204
205205
Attributes:
206206
value (dict): A dict of the batched elements' values.
207-
elements (dict): A dict of the batched elements (clones of the originals).
207+
elements (Mapping): A dict of the batched elements (clones of the originals).
208208
on_change (Optional[Callable]): Optional callback to run when this element's value changes.
209209
210210
Args:
211211
html (Html): A templated Html object.
212-
elements (dict[str, UIElement]): The UI elements to interpolate into the HTML template.
212+
elements (Mapping[str, UIElement]): The UI elements to interpolate into the HTML template.
213213
on_change (Optional[Callable[[Dict[str, object]], None]], optional): Optional callback
214214
to run when this element's value changes.
215215
"""

‎marimo/_plugins/ui/_impl/dictionary.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ class dictionary(_batch_base):
8686
Attributes:
8787
value (dict): A dict holding the values of the UI elements, keyed by
8888
their names.
89-
elements (dict): A dict of the wrapped elements (clones of the originals).
89+
elements (Mapping): A dict of the wrapped elements (clones of the originals).
9090
on_change (Optional[Callable[[dict[str, object]], None]]): Optional callback
9191
to run when this element's value changes.
9292
9393
Args:
94-
elements (dict[str, UIElement[Any, Any]]): A dict mapping names to UI
94+
elements (Mapping[str, UIElement[Any, Any]]): A dict mapping names to UI
9595
elements to include.
9696
label (str, optional): A descriptive name for the dictionary. Defaults to "".
9797
on_change (Callable[[dict[str, object]], None], optional): Optional callback

‎tests/_ast/test_app_typing.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,29 @@ def test_batch_accepts_slider_and_number(self) -> None:
242242
assert_type(parameters.value, dict[str, object])
243243
"""
244244
)
245+
246+
247+
class TestDictionaryTyping:
248+
def test_dictionary_accepts_covariant_dict(self) -> None:
249+
# dict is invariant, so a dict[str, checkbox] was rejected for the old
250+
# dict[str, UIElement] param; widening it to a covariant Mapping accepts
251+
# a dict of UIElement subclasses. Regression guard for #10653.
252+
_check_pyright(
253+
_PREAMBLE
254+
+ """
255+
checks: dict[str, mo.ui.checkbox] = {"a": mo.ui.checkbox()}
256+
d = mo.ui.dictionary(checks)
257+
assert_type(d.value, dict[str, object])
258+
"""
259+
)
260+
261+
def test_validate_and_clone_accepts_covariant_dict(self) -> None:
262+
_check_pyright(
263+
"""
264+
import marimo as mo
265+
from marimo._plugins.ui._impl.batch import validate_and_clone
266+
267+
checks: dict[str, mo.ui.checkbox] = {"a": mo.ui.checkbox()}
268+
_cloned = validate_and_clone(checks)
269+
"""
270+
)

‎tests/_plugins/ui/_impl/test_batch.py‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Copyright 2026 Marimo. All rights reserved.
22
from __future__ import annotations
33

4+
from types import MappingProxyType
5+
46
import pytest
57

68
from marimo import md
79
from marimo._output.hypertext import Html
810
from marimo._plugins import ui
11+
from marimo._plugins.ui._impl.batch import validate_and_clone
912

1013

1114
def test_batch_rejects_non_ui_elements() -> None:
@@ -26,23 +29,6 @@ def test_batch_rejects_non_ui_elements() -> None:
2629
md("Example {thing}").batch(thing={"key": "value"}) # type: ignore
2730

2831

29-
def test_validate_and_clone_accepts_readonly_mapping() -> None:
30-
"""`validate_and_clone` accepts any Mapping of UI elements, not just dict."""
31-
from types import MappingProxyType
32-
33-
from marimo._plugins.ui._impl.batch import validate_and_clone
34-
35-
a = ui.text(value="hello")
36-
elements = MappingProxyType({"a": a})
37-
38-
cloned = validate_and_clone(elements)
39-
40-
assert isinstance(cloned, dict)
41-
assert set(cloned) == {"a"}
42-
# returns clones, not the originals
43-
assert cloned["a"]._id != a._id
44-
45-
4632
def test_dictionary_rejects_non_ui_elements() -> None:
4733
"""Test that dictionary raises ValueError for non-UIElement arguments."""
4834
with pytest.raises(
@@ -61,6 +47,19 @@ def test_dictionary_rejects_non_ui_elements() -> None:
6147
ui.dictionary({"valid": ui.slider(1, 10), "invalid": "string"}) # type: ignore
6248

6349

50+
def test_validate_and_clone_accepts_readonly_mapping() -> None:
51+
"""`validate_and_clone` accepts any Mapping of UI elements, not just dict."""
52+
a = ui.text(value="hello")
53+
elements = MappingProxyType({"a": a})
54+
55+
cloned = validate_and_clone(elements)
56+
57+
assert isinstance(cloned, dict)
58+
assert set(cloned) == {"a"}
59+
# returns clones, not the originals
60+
assert cloned["a"]._id != a._id
61+
62+
6463
def test_update_on_frontend_value_change_only() -> None:
6564
b = ui.batch(
6665
Html("{a} {b}"),

‎tests/_plugins/ui/_impl/test_dictionary.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2026 Marimo. All rights reserved.
22
from __future__ import annotations
33

4+
from types import MappingProxyType
5+
46
import pytest
57

68
from marimo._plugins import ui
@@ -35,8 +37,6 @@ def func() -> None:
3537
def test_dictionary_accepts_readonly_mapping() -> None:
3638
# `elements` is typed as a covariant Mapping, so any read-only mapping of
3739
# UI elements (not just a plain dict) must be accepted and cloned.
38-
from types import MappingProxyType
39-
4040
a = ui.text(value="hello")
4141
b = ui.slider(1, 10, value=2)
4242
elements = MappingProxyType({"a": a, "b": b})

0 commit comments

Comments
 (0)