Skip to content

Commit 73ff622

Browse files
authored
fix: widen Html.batch() type to accept all UIElement subclasses (#9163) (#9176)
`UIElement` type params are invariant, so `UIElement[Numeric, Numeric]` (slider) wasn't assignable to `UIElement[JSONType, object]`. Changed to `UIElement[Any, Any]`, matching `batch.__init__` and `array.__init__`.
1 parent 386cba6 commit 73ff622

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

marimo/_output/hypertext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
if TYPE_CHECKING:
1717
from collections.abc import Iterator
1818

19-
from marimo._plugins.core.web_component import JSONType
2019
from marimo._plugins.ui._core.ui_element import UIElement
2120
from marimo._plugins.ui._impl.batch import batch as batch_plugin
2221

@@ -164,7 +163,7 @@ def __format__(self, spec: str) -> str:
164163
)
165164

166165
@mddoc
167-
def batch(self, **elements: UIElement[JSONType, object]) -> batch_plugin:
166+
def batch(self, **elements: UIElement[Any, Any]) -> batch_plugin:
168167
"""Convert an HTML object with templated text into a UI element.
169168
170169
This method lets you create custom UI elements that are represented

tests/_ast/test_app_typing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,19 @@ def test_cache_context_return_type(self, cache_func: str) -> None:
226226
assert_type(mo.{cache_func}("cache"), _cache_context)
227227
"""
228228
)
229+
230+
231+
class TestBatchTyping:
232+
def test_batch_accepts_slider_and_number(self) -> None:
233+
_check_pyright(
234+
_PREAMBLE
235+
+ """
236+
parameters = mo.md(
237+
"slider: {a} number: {b}"
238+
).batch(
239+
a=mo.ui.slider(start=1, stop=50, step=1, value=10),
240+
b=mo.ui.number(value=0.2),
241+
)
242+
assert_type(parameters.value, dict[str, object])
243+
"""
244+
)

0 commit comments

Comments
 (0)