Skip to content

fix: handle empty px.histogram() by skipping None label in hover template#5535

Merged
emilykl merged 1 commit intoplotly:mainfrom
tysoncung:fix-empty-histogram-crash
Mar 10, 2026
Merged

fix: handle empty px.histogram() by skipping None label in hover template#5535
emilykl merged 1 commit intoplotly:mainfrom
tysoncung:fix-empty-histogram-crash

Conversation

@tysoncung
Copy link
Contributor

Summary

px.histogram() crashes with TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' when called with no data arguments, while other chart types like px.scatter(), px.bar(), and px.pie() handle empty invocations correctly.

Root Cause

The histogram-specific hover label path in _core.py unconditionally adds an entry to mapping_labels even when no column is specified. When no data is provided, attr_label is None (from get_decorated_label(args, None, 'x')), resulting in a {None: '%{x}'} entry. The hover template list comprehension then fails:

hover_lines = [k + "=" + v for k, v in mapping_labels_copy.items()]
#              None + "=" + ...  → TypeError

Fix

Skip adding to mapping_labels when attr_label is None, consistent with how other chart types handle empty invocations (they never reach the mapping_labels code path since their attrs check attr_value is not None first).

Test

Added test_empty_histogram to verify px.histogram() returns a valid figure without raising.

Fixes #5534

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 plotly#5534
@emilykl emilykl self-requested a review March 10, 2026 17:53
Copy link
Contributor

@emilykl emilykl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix @tysoncung !

That whole massive if statement inside make_trace_kwargs() is a bit messy. This is a perfectly reasonable fix and addresses the issue.

@emilykl emilykl merged commit 6f4493c into plotly:main Mar 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: creating empty px.histogram() results in an error

2 participants