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): validate that checkbox value is a bool (#10601)

Abdul Rehmanac259d3bbcb7
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index ca75c81961..bb54b6e20d 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -610,6 +610,10 @@ class checkbox(UIElement[bool, bool]):
disabled: bool = False,
on_change: Callable[[bool], None] | None = None,
) -> None:
+ if not isinstance(value, bool):
+ raise ValueError(
+ f"Invalid type: `value` must be a bool, but got {type(value)}"
+ )
super().__init__(
component_name=checkbox._name,
initial_value=value,
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 833157f984..396a25a728 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -365,6 +365,19 @@ def test_checkbox_init() -> None:
assert ui.checkbox(value=True).value
+def test_checkbox_rejects_non_bool_value() -> None:
+ # `checkbox` is typed `UIElement[bool, bool]`; a non-bool value must raise
+ # instead of being silently accepted.
+ for bad in ("yes", 1, 0, [], None):
+ with pytest.raises(ValueError, match="must be a bool"):
+ ui.checkbox(value=bad)
+
+ # bools still work
+ assert ui.checkbox(value=True).value is True
+ assert ui.checkbox(value=False).value is False
+ assert ui.checkbox().value is False
+
+
def test_radio() -> None:
radio = ui.radio(options=["1", "2", "3"], value="1")
assert radio.value == "1"