Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dash/development/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/development/test_base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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