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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: add __bool__ method to UIElement to handle truth value (#2025)
Myles Scolnickd234d702d200
diff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.pyindex 58ade96f1e..487b27ed63 100644--- a/marimo/_plugins/ui/_core/ui_element.py+++ b/marimo/_plugins/ui/_core/ui_element.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport abcimport base64import copy+import sysimport typesimport uuidimport weakref@@ -512,3 +513,10 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):their own side-effects."""return copy.deepcopy(self)++ def __bool__(self) -> bool:+ sys.stderr.write(+ "The truth value of a UIElement is always True. You "+ "probably want to call `.value` instead."+ )+ return Truediff --git a/tests/_plugins/ui/_core/test_ui_element.py b/tests/_plugins/ui/_core/test_ui_element.pyindex 57e1f206ea..9effeb08fc 100644--- a/tests/_plugins/ui/_core/test_ui_element.py+++ b/tests/_plugins/ui/_core/test_ui_element.py@@ -1,3 +1,6 @@+import io+from contextlib import redirect_stderr+from marimo._plugins.ui._core.ui_element import UIElement@@ -32,3 +35,19 @@ def test_ui_element_clone() -> None:assert element._on_change is not Noneassert clone._on_change is not Noneassert id(element._on_change.__self__) != id(clone._on_change.__self__)+++def test_bool_ui_element() -> None:+ element = Element()+ expected_warning = (+ "The truth value of a UIElement is always True. You "+ "probably want to call `.value` instead."+ )+ with io.StringIO() as buf, redirect_stderr(buf):+ assert bool(element) is True+ assert buf.getvalue() == expected_warning++ with io.StringIO() as buf, redirect_stderr(buf):+ res = not element+ del res+ assert buf.getvalue() == expected_warning