Skip to content

ENH: Test ._is_numeric not .char in np.testing.assert_equal#30879

Merged
seberg merged 2 commits intonumpy:mainfrom
hawkinsp:tests
Mar 6, 2026
Merged

ENH: Test ._is_numeric not .char in np.testing.assert_equal#30879
seberg merged 2 commits intonumpy:mainfrom
hawkinsp:tests

Conversation

@hawkinsp
Copy link
Copy Markdown
Contributor

This PR makes assert_equal correctly handle NaN tests for user-defined float types.

This fixes an issue found when attempting to port ml_dtypes to use the NumPy 2 user dtype APIs (jax-ml/ml_dtypes#360).

@seberg

This PR makes assert_equal correctly handle NaN tests for user-defined
float types.
@seberg
Copy link
Copy Markdown
Member

seberg commented Feb 25, 2026

Hmmm, maybe we can use type(dtype)._is_numeric and then set NPY_DT_NUMERIC flag over there that is a bit private, and it would be nice to expose that as something like isinstance(dtype, numpy.dtypes.NumericDType) or so, but that is fine.

That might be a bit more robust (i.e. should work even if you use a random character) -- although, I am not sure if you need something around isnan here too...

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Feb 25, 2026
@ngoldbaum
Copy link
Copy Markdown
Member

Hmmm, maybe we can use type(dtype)._is_numeric and then set NPY_DT_NUMERIC flag over there that is a bit private

@hawkinsp FWIW I added NPY_DT_NUMERIC for uses like this. I also agree with sebastian it would be really nice to have a better story for registering dtypes in an ontology or hierarchy that's centrally managed by NumPy.

This is very load-bearing code in the ecosystem so I'm a little nervous about changing behavior for legacy dtypes. But we can always add a check for an _is_numeric attribute, since I don't think this is performance-sensitive code, at least not at the level of one getattr call.

@seberg
Copy link
Copy Markdown
Member

seberg commented Mar 6, 2026

Thanks, let's try with this, since Nathan also thought it should be a good solution.

@seberg seberg merged commit a6ce1e7 into numpy:main Mar 6, 2026
79 checks passed
@seberg seberg changed the title ENH: Test .kind not .char in np.testing.assert_equal ENH: Test ._is_numeric not .char in np.testing.assert_equal Mar 6, 2026
charris pushed a commit to charris/numpy that referenced this pull request Mar 7, 2026
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 7, 2026
charris pushed a commit to charris/numpy that referenced this pull request Mar 7, 2026
charris added a commit that referenced this pull request Mar 7, 2026
ENH: Test .kind not .char in np.testing.assert_equal (#30879)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants