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 on colliding option names in dropdown/multiselect (#10511)

Abdul Rehmana819640901a3
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index d9cc8ba994..ca75c81961 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -5,7 +5,7 @@ import base64
import dataclasses
import sys
import traceback
-from collections.abc import Callable, Sequence
+from collections.abc import Callable, Iterable, Sequence
from dataclasses import dataclass
from typing import (
Any,
@@ -962,6 +962,29 @@ def _validate_option_name(option_name: str, options: dict[str, Any]) -> None:
)
+def _build_option_map(options: Iterable[Any]) -> dict[str, Any]:
+ """Build the `{display name: option}` map, raising if two names collide.
+
+ `dropdown` and `multiselect` key their option map on `_to_option_name(...)`.
+ Without this check, colliding names would silently overwrite each other and
+ drop options (e.g. `["a", "a"]`, or `[1, "1"]` where an `int` and a `str`
+ both map to `"1"`). Validating and building in a single pass avoids
+ consuming a single-use iterable twice and calls `_to_option_name` once per
+ option.
+ """
+ result: dict[str, Any] = {}
+ for option in options:
+ name = _to_option_name(option)
+ if name in result:
+ raise ValueError(
+ f"Duplicate option name '{name}': options {result[name]!r} and "
+ f"{option!r} both map to the same name. "
+ "Option names must be unique."
+ )
+ result[name] = option
+ return result
+
+
@mddoc
class dropdown(UIElement[list[str], Any]):
"""A dropdown selector.
@@ -1043,7 +1066,7 @@ class dropdown(UIElement[list[str], Any]):
searchable = True
if not isinstance(options, dict):
- options = {_to_option_name(option): option for option in options}
+ options = _build_option_map(options)
if value is not None and not isinstance(value, str):
value = _to_option_name(value)
@@ -1165,7 +1188,7 @@ class multiselect(UIElement[list[str], list[object]]):
)
if not isinstance(options, dict):
- options = {_to_option_name(option): option for option in options}
+ options = _build_option_map(options)
if value is not None and not isinstance(value, str):
value = [_to_option_name(v) for v in value]
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 56d47e5e6a..833157f984 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -496,6 +496,35 @@ def test_dropdown_invalid_value() -> None:
assert "['1', '2', '3']" in str(e.value)
+def test_dropdown_duplicate_option_names_raise() -> None:
+ # Repeated names collapse silently without this guard.
+ with pytest.raises(ValueError, match="Duplicate option name"):
+ ui.dropdown(options=["a", "a", "b"])
+
+ # Distinct values that stringify to the same name (int 1 vs str "1").
+ with pytest.raises(ValueError, match="Duplicate option name"):
+ ui.dropdown(options=[1, "1"])
+
+ # Non-colliding options are unaffected; all are kept.
+ dd = ui.dropdown(options=[1, "2", 3])
+ assert len(dd.options) == 3
+
+ # Explicit dict input is untouched (keys are already unique).
+ dd = ui.dropdown(options={"a": 1, "b": 2})
+ assert len(dd.options) == 2
+
+
+def test_multiselect_duplicate_option_names_raise() -> None:
+ with pytest.raises(ValueError, match="Duplicate option name"):
+ ui.multiselect(options=["a", "a", "b"])
+
+ with pytest.raises(ValueError, match="Duplicate option name"):
+ ui.multiselect(options=[1, "1"])
+
+ ms = ui.multiselect(options=[1, "2", 3])
+ assert len(ms.options) == 3
+
+
def test_dropdown_disabled() -> None:
dd = ui.dropdown(options=["1", "2", "3"])
assert dd._component_args["disabled"] is False