Skip to content

Commit c10fb9c

Browse files
committed
fix: handle empty histogram() by skipping None label in hover template
When px.histogram() is called with no data arguments, the histogram- specific hover label path inserts a None key into mapping_labels (because attr_label is None when no column is specified). The hover template list comprehension then fails with: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' Skip adding to mapping_labels when attr_label is None, consistent with how other chart types handle empty invocations. Fixes #5534
1 parent c7bffb9 commit c10fb9c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

plotly/express/_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
588588
and attr_name == "z"
589589
):
590590
# ensure that stuff like "count" gets into the hoverlabel
591-
mapping_labels[attr_label] = "%%{%s}" % attr_name
591+
if attr_label is not None:
592+
mapping_labels[attr_label] = "%%{%s}" % attr_name
592593
if trace_spec.constructor not in [go.Parcoords, go.Parcats]:
593594
# Modify mapping_labels according to hover_data keys
594595
# if hover_data is a dict

tests/test_optional/test_px/test_px_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,13 @@ def test_timeline_cols_already_temporal(constructor, datetime_columns):
619619
assert len(fig.data) == 3
620620
assert fig.layout.xaxis.type == "date"
621621
assert fig.layout.xaxis.title.text is None
622+
623+
624+
def test_empty_histogram():
625+
"""Empty px.histogram() should not raise, matching scatter/bar/pie behavior.
626+
627+
Regression test for https://github.com/plotly/plotly.py/issues/5534
628+
"""
629+
fig = px.histogram()
630+
assert len(fig.data) == 1
631+
assert fig.data[0].type == "histogram"

0 commit comments

Comments
 (0)