Skip to content

BUG: return scalar types from dtypes() and default_dtypes() - #480

Open
karandhaodiyal28-hash wants to merge 1 commit into
data-apis:mainfrom
karandhaodiyal28-hash:fix-inspection-dtypes-return-scalar-types
Open

karandhaodiyal28-hash wants to merge 1 commit into
data-apis:mainfrom
karandhaodiyal28-hash:fix-inspection-dtypes-return-scalar-types

Conversation

@karandhaodiyal28-hash

Copy link
Copy Markdown

Fixes gh-359.

__array_namespace_info__().dtypes() and .default_dtypes() return np.dtype objects instead of the scalar types:

>>> from array_api_compat import numpy as xp
>>> info = xp.__array_namespace_info__()
>>> info.dtypes()["float64"] == None
True
>>> info.dtypes()["float32"].__name__
AttributeError: 'numpy.dtypes.Float32DType' object has no attribute '__name__'

np.dtype(None) is float64 in NumPy, so float64 is the one dtype that spuriously compares equal to None — a caller that uses None to 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.float64 rather than np.dtype('float64') in gh-359, and the cupy/dask backends already return a bare bool for the bool entry, so the scalar type looks like the intended convention here too.

Scope note: cupy/_info.py and dask/array/_info.py have 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 the dtype(...) wrapper in dtypes() and default_dtypes()
  • tests/test_numpy.py — regression test (fails before this change, passes after)

__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
Copilot AI balanced review requested due to automatic review settings October 15, 2026 15:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

numpy.__array_namespace_info__().dtypes()["float64"] == None is True but numpy.float64 == None is False

2 participants