Skip to content

Accept NumPy scalars in integer and string properties - #5828

Open
RaphaelFakhri wants to merge 1 commit into
plotly:mainfrom
RaphaelFakhri:fix-numpy-scalar-validators
Open

RaphaelFakhri wants to merge 1 commit into
plotly:mainfrom
RaphaelFakhri:fix-numpy-scalar-validators

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Link to issue

Closes #5827

Description of change

Integer properties now accept NumPy integer scalars, and non-strict string properties now accept any number, including NumPy scalars. IntegerValidator checks numbers.Integral instead of int, and StringValidator checks numbers.Number instead of (int, float), which matches how NumberValidator already validates numbers.

Demo

import numpy as np
import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({"year": [2020, 2021], "v": [1, 2]})
fig = go.Figure([go.Scatter(y=[1], name=y) for y in df.year.unique()])
fig.update_layout(xaxis_nticks=np.int64(5))
print([t.name for t in fig.data], fig.layout.xaxis.nticks)

Before: ValueError: Invalid value of type 'numpy.int64' received for the 'name' property of scatter

After: ['2020', '2021'] 5. The figure serializes the same with the json and orjson engines ("name":"2020", "nticks":5).

Testing strategy

The existing integer and string validator tests now include NumPy scalars: scalar acceptance, acceptance within min/max bounds, rejection outside min/max bounds, a list of NumPy integers for array_ok properties, and NumPy integer and float32 conversion to strings. Out-of-range NumPy integers are still rejected.

Additional information (optional)

Strings, booleans, NaN and infinite values are validated as before: numbers.Integral excludes floats, and the string check still runs before the number check.

Guidelines

@RaphaelFakhri
RaphaelFakhri force-pushed the fix-numpy-scalar-validators branch from 1a6e654 to ee5cb34 Compare October 19, 2026 07:46
@camdecoster camdecoster self-assigned this Oct 19, 2026
@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for the update. I'll review and follow up.

This branch has not been deployed

No deployments
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]: Integer and string properties reject NumPy scalars

2 participants