Skip to content

Commit 364b48c

Browse files
committed
BUG: Fix np.array_api.can_cast() by not relying on np.can_cast()
Original NumPy Commit: 8eac9a4bb5b497ca29ebb852f21169ecfd0191e1
1 parent 6fffb46 commit 364b48c

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

array_api_strict/_data_type_functions.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ def can_cast(from_: Union[Dtype, Array], to: Dtype, /) -> bool:
5050
5151
See its docstring for more information.
5252
"""
53-
from ._array_object import Array
54-
5553
if isinstance(from_, Array):
56-
from_ = from_._array
57-
return np.can_cast(from_, to)
54+
from_ = from_.dtype
55+
elif from_ not in _all_dtypes:
56+
raise TypeError(f"{from_=}, but should be an array_api array or dtype")
57+
if to not in _all_dtypes:
58+
raise TypeError(f"{to=}, but should be a dtype")
59+
try:
60+
dtype = _result_type(from_, to)
61+
return to == dtype
62+
except TypeError:
63+
return False
5864

5965

6066
# These are internal objects for the return types of finfo and iinfo, since
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from numpy import array_api as xp
4+
5+
6+
@pytest.mark.parametrize(
7+
"from_, to, expected",
8+
[
9+
(xp.int8, xp.int16, True),
10+
(xp.int16, xp.int8, False),
11+
# np.can_cast has discrepancies with the Array API
12+
# See https://github.com/numpy/numpy/issues/20870
13+
(xp.bool, xp.int8, False),
14+
(xp.asarray(0, dtype=xp.uint8), xp.int8, False),
15+
],
16+
)
17+
def test_can_cast(from_, to, expected):
18+
"""
19+
can_cast() returns correct result
20+
"""
21+
assert xp.can_cast(from_, to) == expected
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Callable
2+
3+
import pytest
4+
5+
from numpy import array_api as xp
6+
7+
8+
def p(func: Callable, *args, **kwargs):
9+
f_sig = ", ".join(
10+
[str(a) for a in args] + [f"{k}={v}" for k, v in kwargs.items()]
11+
)
12+
id_ = f"{func.__name__}({f_sig})"
13+
return pytest.param(func, args, kwargs, id=id_)
14+
15+
16+
@pytest.mark.parametrize(
17+
"func, args, kwargs",
18+
[
19+
p(xp.can_cast, 42, xp.int8),
20+
p(xp.can_cast, xp.int8, 42),
21+
p(xp.result_type, 42),
22+
],
23+
)
24+
def test_raises_on_invalid_types(func, args, kwargs):
25+
"""Function raises TypeError when passed invalidly-typed inputs"""
26+
with pytest.raises(TypeError):
27+
func(*args, **kwargs)

0 commit comments

Comments
 (0)