From 0d6fdd4a78a17ebb2a734519ac769c0eb4d80d17 Mon Sep 17 00:00:00 2001 From: monsterlag <64719697+monsterlag@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:03:10 -0400 Subject: [PATCH] Make components always truthy Component defined __len__ but not __bool__, so truthiness fell back to the number of children and a component without children (or without a children prop, like DataTable) was falsy. Fixes #2906. --- dash/development/base_component.py | 5 +++++ tests/unit/development/test_base_component.py | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/dash/development/base_component.py b/dash/development/base_component.py index b6798064e1..fb865312cd 100644 --- a/dash/development/base_component.py +++ b/dash/development/base_component.py @@ -433,6 +433,11 @@ def __iter__(self): for t in self._traverse_ids(): yield t.id # type: ignore[reportAttributeAccessIssue] + def __bool__(self): + # Without this, truthiness falls back to __len__, so a component + # with no children (or no children prop, like DataTable) is falsy. + return True + def __len__(self): """Return the number of items in the tree.""" # TODO - Should we return the number of items that have IDs diff --git a/tests/unit/development/test_base_component.py b/tests/unit/development/test_base_component.py index cefde04fde..a7ba6a2720 100644 --- a/tests/unit/development/test_base_component.py +++ b/tests/unit/development/test_base_component.py @@ -6,7 +6,7 @@ from dash import __version__, Dash from dash import html from dash.development.base_component import Component -from dash import dcc, Input, Output +from dash import dcc, dash_table, Input, Output Component._prop_names = ("id", "a", "children", "style") Component._type = "TestComponent" @@ -539,3 +539,11 @@ def update2(v): def test_debc030_invalid_children_args(): with pytest.raises(TypeError): dcc.Input(children="invalid children") + + +def test_debc031_bool_is_always_true(): + assert bool(Component()) + assert bool(Component(children=[])) + assert bool(html.Div()) + assert bool(dash_table.DataTable(data=[{"a": 1}])) + assert (html.Div() or None) is not None