Fixes a bug where custom dtypes (floats, ints, complexes) raised TypeError when compared (==, !=) against incompatible types like strings or None.#380
Merged
Conversation
eb16dc5 to
00e1227
Compare
…Error when compared (==, !=) against incompatible types like strings or None. Before this change, comparing a custom dtype to an incompatible type crashed: ```python >>> import ml_dtypes >>> ml_dtypes.bfloat16(1.0) == "param" TypeError: ufunc 'equal' not supported for the input types, and the inputs could not be safely coerced... ``` Now it safely returns False (or True for !=): ```python >>> import ml_dtypes >>> ml_dtypes.bfloat16(1.0) == "param" False ``` The C++ rich comparison now returns Py_NotImplemented early for strings and non-sequence incompatible types. This allows Python's standard identity fallback to work safely instead of crashing, while still allowing NumPy to handle valid sequence comparisons (e.g. lists). This isn't ideal, but I'm not sure it's possible to do better until we migrate to NumPy 2.0 dtypes. I believe this to be the root cause of this JAX CI flake https://github.com/jax-ml/jax/actions/runs/27343723608/job/80786816925 which occurs when a bfloat16 scalar and a string happen to collide in a dict and end up getting compared. PiperOrigin-RevId: 930641853
00e1227 to
dcb22b3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a bug where custom dtypes (floats, ints, complexes) raised TypeError when compared (==, !=) against incompatible types like strings or None.
Before this change, comparing a custom dtype to an incompatible type crashed:
Now it safely returns False (or True for !=):
The C++ rich comparison now returns Py_NotImplemented early for strings and non-sequence incompatible types. This allows Python's standard identity fallback to work safely instead of crashing, while still allowing NumPy to handle valid sequence comparisons (e.g. lists). This isn't ideal, but I'm not sure it's possible to do better until we migrate to NumPy 2.0 dtypes.
I believe this to be the root cause of this JAX CI flake https://github.com/jax-ml/jax/actions/runs/27343723608/job/80786816925 which occurs when a bfloat16 scalar and a string happen to collide in a dict and end up getting compared.