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