From c10fb9c6862ca45bae05c6ba66782ea0d030df99 Mon Sep 17 00:00:00 2001 From: Tyson Cung Date: Tue, 10 Mar 2026 17:20:22 +0000 Subject: [PATCH] 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 --- plotly/express/_core.py | 3 ++- tests/test_optional/test_px/test_px_functions.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plotly/express/_core.py b/plotly/express/_core.py index 9fbf526cdeb..2756346c55e 100644 --- a/plotly/express/_core.py +++ b/plotly/express/_core.py @@ -588,7 +588,8 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): and attr_name == "z" ): # ensure that stuff like "count" gets into the hoverlabel - mapping_labels[attr_label] = "%%{%s}" % attr_name + if attr_label is not None: + mapping_labels[attr_label] = "%%{%s}" % attr_name if trace_spec.constructor not in [go.Parcoords, go.Parcats]: # Modify mapping_labels according to hover_data keys # if hover_data is a dict diff --git a/tests/test_optional/test_px/test_px_functions.py b/tests/test_optional/test_px/test_px_functions.py index 0814898f89d..2822a931fd7 100644 --- a/tests/test_optional/test_px/test_px_functions.py +++ b/tests/test_optional/test_px/test_px_functions.py @@ -619,3 +619,13 @@ def test_timeline_cols_already_temporal(constructor, datetime_columns): assert len(fig.data) == 3 assert fig.layout.xaxis.type == "date" assert fig.layout.xaxis.title.text is None + + +def test_empty_histogram(): + """Empty px.histogram() should not raise, matching scatter/bar/pie behavior. + + Regression test for https://github.com/plotly/plotly.py/issues/5534 + """ + fig = px.histogram() + assert len(fig.data) == 1 + assert fig.data[0].type == "histogram"