Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 50 additions & 51 deletions src/array_api_compat/numpy/_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from numpy import (
complex64,
complex128,
dtype,
float32,
float64,
int8,
Expand Down Expand Up @@ -186,10 +185,10 @@ def default_dtypes(
f' {device}'
)
return {
"real floating": dtype(float64),
"complex floating": dtype(complex128),
"integral": dtype(intp),
"indexing": dtype(intp),
"real floating": float64,
"complex floating": complex128,
"integral": intp,
"indexing": intp,
}

def dtypes(
Expand Down Expand Up @@ -259,71 +258,71 @@ def dtypes(
)
if kind is None:
return {
"bool": dtype(bool),
"int8": dtype(int8),
"int16": dtype(int16),
"int32": dtype(int32),
"int64": dtype(int64),
"uint8": dtype(uint8),
"uint16": dtype(uint16),
"uint32": dtype(uint32),
"uint64": dtype(uint64),
"float32": dtype(float32),
"float64": dtype(float64),
"complex64": dtype(complex64),
"complex128": dtype(complex128),
"bool": bool,
"int8": int8,
"int16": int16,
"int32": int32,
"int64": int64,
"uint8": uint8,
"uint16": uint16,
"uint32": uint32,
"uint64": uint64,
"float32": float32,
"float64": float64,
"complex64": complex64,
"complex128": complex128,
}
if kind == "bool":
return {"bool": dtype(bool)}
return {"bool": bool}
if kind == "signed integer":
return {
"int8": dtype(int8),
"int16": dtype(int16),
"int32": dtype(int32),
"int64": dtype(int64),
"int8": int8,
"int16": int16,
"int32": int32,
"int64": int64,
}
if kind == "unsigned integer":
return {
"uint8": dtype(uint8),
"uint16": dtype(uint16),
"uint32": dtype(uint32),
"uint64": dtype(uint64),
"uint8": uint8,
"uint16": uint16,
"uint32": uint32,
"uint64": uint64,
}
if kind == "integral":
return {
"int8": dtype(int8),
"int16": dtype(int16),
"int32": dtype(int32),
"int64": dtype(int64),
"uint8": dtype(uint8),
"uint16": dtype(uint16),
"uint32": dtype(uint32),
"uint64": dtype(uint64),
"int8": int8,
"int16": int16,
"int32": int32,
"int64": int64,
"uint8": uint8,
"uint16": uint16,
"uint32": uint32,
"uint64": uint64,
}
if kind == "real floating":
return {
"float32": dtype(float32),
"float64": dtype(float64),
"float32": float32,
"float64": float64,
}
if kind == "complex floating":
return {
"complex64": dtype(complex64),
"complex128": dtype(complex128),
"complex64": complex64,
"complex128": complex128,
}
if kind == "numeric":
return {
"int8": dtype(int8),
"int16": dtype(int16),
"int32": dtype(int32),
"int64": dtype(int64),
"uint8": dtype(uint8),
"uint16": dtype(uint16),
"uint32": dtype(uint32),
"uint64": dtype(uint64),
"float32": dtype(float32),
"float64": dtype(float64),
"complex64": dtype(complex64),
"complex128": dtype(complex128),
"int8": int8,
"int16": int16,
"int32": int32,
"int64": int64,
"uint8": uint8,
"uint16": uint16,
"uint32": uint32,
"uint64": uint64,
"float32": float32,
"float64": float64,
"complex64": complex64,
"complex128": complex128,
}
if isinstance(kind, tuple):
res: dict[str, DType] = {}
Expand Down
23 changes: 23 additions & 0 deletions tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,26 @@ def test_reshape_copy_false_returns_a_view_without_deprecation():
def test_reshape_copy_false_rejects_an_input_that_needs_a_copy():
with pytest.raises((ValueError, AttributeError)):
xp.reshape(np.arange(12).reshape(3, 4).T, (2, 6), copy=False)


def test_inspection_dtypes_do_not_compare_equal_to_none():
"""`dtypes()`/`default_dtypes()` must return the scalar types, not
`np.dtype` objects. gh-359.

`np.dtype(None)` is `float64` in NumPy, so `dtypes()["float64"] == None`
is True and a caller cannot tell "unsupported" from "supported". And
`np.dtype` objects have no `__name__`, so they are unusable for the
ordinary introspection callers do on the returned values.
"""
info = xp.__array_namespace_info__()

assert info.dtypes()["float64"] is np.float64
assert info.default_dtypes()["real floating"] is np.float64

for dt in info.dtypes().values():
assert not (dt == None) # noqa: E711
assert isinstance(dt.__name__, str)

for dt in info.default_dtypes().values():
assert not (dt == None) # noqa: E711
assert isinstance(dt.__name__, str)
Loading