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(ui): raise a helpful error for invalid radio/multiselect option names (#10498)

Chinmayrawat15bf9b2fc268ba
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 8699398a86..d9cc8ba994 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -702,7 +702,10 @@ class radio(UIElement[str | None, Any]):
return radio(options=options, label=label, **kwargs)
def _convert_value(self, value: str | None) -> Any:
- return self.options[value] if value is not None else None
+ if value is None:
+ return None
+ _validate_option_name(value, self.options)
+ return self.options[value]
@mddoc
@@ -948,6 +951,17 @@ def _to_option_name(option: Any) -> str:
return repr(option)
+def _validate_option_name(option_name: str, options: dict[str, Any]) -> None:
+ """Raise a helpful error if `option_name` isn't one of `options`."""
+ if option_name not in options:
+ raise ValueError(
+ f"The option name '{option_name}' "
+ "is not a valid option. "
+ "Please use one of the following options: "
+ f"{list(options.keys())}"
+ )
+
+
@mddoc
class dropdown(UIElement[list[str], Any]):
"""A dropdown selector.
@@ -1076,13 +1090,7 @@ class dropdown(UIElement[list[str], Any]):
if value:
assert len(value) == 1, "Dropdowns only support a single value"
self._selected_key = value[0]
- if self._selected_key not in self.options:
- raise ValueError(
- f"The option name '{self._selected_key}' "
- "is not a valid option. "
- "Please use one of the following options: "
- f"{list(self.options.keys())}"
- )
+ _validate_option_name(self._selected_key, self.options)
return self.options[value[0]]
else:
self._selected_key = None
@@ -1195,6 +1203,8 @@ class multiselect(UIElement[list[str], list[object]]):
return multiselect(options=options, label=label, **kwargs)
def _convert_value(self, value: list[str]) -> list[object]:
+ for v in value:
+ _validate_option_name(v, self.options)
return [self.options[v] for v in value]
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 07287367ce..56d47e5e6a 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -393,6 +393,21 @@ def test_radio_from_dataframe() -> None:
assert radio.value == "b"
+def test_radio_invalid_value() -> None:
+ with pytest.raises(ValueError) as e:
+ ui.radio(options=["1", "2", "3"], value="4")
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+ radio = ui.radio(options=["1", "2", "3"], value="1")
+ with pytest.raises(ValueError) as e:
+ radio._update("4")
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+
def test_dropdown() -> None:
dd = ui.dropdown(options=["1", "2", "3"])
assert dd.value is None
@@ -466,6 +481,21 @@ def test_dropdown_lots_of_options() -> None:
assert dropdown._component_args["searchable"] is True
+def test_dropdown_invalid_value() -> None:
+ with pytest.raises(ValueError) as e:
+ ui.dropdown(options=["1", "2", "3"], value="4")
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+ dd = ui.dropdown(options=["1", "2", "3"], value="1")
+ with pytest.raises(ValueError) as e:
+ dd._update(["4"])
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+
def test_dropdown_disabled() -> None:
dd = ui.dropdown(options=["1", "2", "3"])
assert dd._component_args["disabled"] is False
@@ -595,6 +625,21 @@ def test_multiselect_too_many_options() -> None:
assert "maximum number" in str(e.value)
+def test_multiselect_invalid_value() -> None:
+ with pytest.raises(ValueError) as e:
+ ui.multiselect(options=["1", "2", "3"], value=["4"])
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+ ms = ui.multiselect(options=["1", "2", "3"], value=["1"])
+ with pytest.raises(ValueError) as e:
+ ms._update(["1", "4"])
+
+ assert "The option name '4' is not a valid option" in str(e.value)
+ assert "['1', '2', '3']" in str(e.value)
+
+
def test_multiselect_disabled() -> None:
ms = ui.multiselect(options=["1", "2", "3"])
assert ms._component_args["disabled"] is False