Repository navigation
BUG: return scalar types from dtypes() and default_dtypes() - #480
karandhaodiyal28-hash wants to merge 1 commit into
Conversation
__array_namespace_info__().dtypes() and .default_dtypes() returned np.dtype objects instead of the scalar types. That causes two problems: - np.dtype(None) is float64 in NumPy, so dtypes()["float64"] == None is True and a caller cannot use None to mean "this dtype is unsupported". - np.dtype objects have no __name__, so the returned values cannot be introspected at all. This affects every dtype, not just float64. Both methods already document the scalar types in their docstrings, so this makes the code match its own documentation. The cupy and dask backends have the same pattern and already return a bare `bool` for the bool entry, so the scalar type is the intended convention here too. This only changes the numpy backend; cupy and dask are left alone since they cannot be exercised from the numpy-only test environment. Fixes data-apisgh-359
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
FWIW I agree with #359 (comment):
The whole equality comparison to None is explicitly called out in PEP8:
All in all, this patch fixes what is a borderline user error and has hard-to-predict consequences. Therefore I would prefer to not merge it. Thank you for the patch either way @karandhaodiyal28-hash ! |
Fixes gh-359.
__array_namespace_info__().dtypes()and.default_dtypes()returnnp.dtypeobjects instead of the scalar types:np.dtype(None)isfloat64in NumPy, sofloat64is the one dtype that spuriously compares equal toNone— a caller that usesNoneto mean "this dtype is unsupported" gets it backwards for the most important dtype. The missing__name__hits every dtype.Both docstrings already show the scalar types, so this brings the code in line with its own documentation. rgommers also suggested returning
np.float64rather thannp.dtype('float64')in gh-359, and the cupy/dask backends already return a bareboolfor the bool entry, so the scalar type looks like the intended convention here too.Scope note:
cupy/_info.pyanddask/array/_info.pyhave the same pattern, but I've left them untouched — I can only exercise the numpy backend locally.Changes:
src/array_api_compat/numpy/_info.py— drop thedtype(...)wrapper indtypes()anddefault_dtypes()tests/test_numpy.py— regression test (fails before this change, passes after)