Skip to content

Commit 0a4e40d

Browse files
committed
Re-introduce __array__ with a warning
1 parent 4e5ff09 commit 0a4e40d

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

array_api_strict/_array_object.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import operator
1919
from enum import IntEnum
20+
import warnings
2021

2122
from ._creation_functions import asarray
2223
from ._dtypes import (
@@ -159,23 +160,28 @@ def __repr__(self: Array, /) -> str:
159160
def __array__(self, dtype: None | np.dtype[Any] = None, copy: None | bool = None) -> npt.NDArray[Any]:
160161
# We have to allow this to be internally enabled as there's no other
161162
# easy way to parse a list of Array objects in asarray().
162-
if _allow_array:
163-
if self._device != CPU_DEVICE:
164-
raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.")
165-
# copy keyword is new in 2.0.0; for older versions don't use it
166-
# retry without that keyword.
167-
if np.__version__[0] < '2':
168-
return np.asarray(self._array, dtype=dtype)
169-
elif np.__version__.startswith('2.0.0-dev0'):
170-
# Handle dev version for which we can't know based on version
171-
# number whether or not the copy keyword is supported.
172-
try:
173-
return np.asarray(self._array, dtype=dtype, copy=copy)
174-
except TypeError:
175-
return np.asarray(self._array, dtype=dtype)
176-
else:
163+
if not _allow_array:
164+
warnings.warn("The array_api_strict __array__ method was used. "
165+
"This method is not part of the array API standard and will be "
166+
"removed in a future version of array-api-strict. "
167+
"See https://github.com/data-apis/array-api-strict/issues/67 "
168+
"for more info.", FutureWarning, stacklevel=2)
169+
170+
if self._device != CPU_DEVICE:
171+
raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.")
172+
# copy keyword is new in 2.0.0; for older versions don't use it
173+
# retry without that keyword.
174+
if np.__version__[0] < '2':
175+
return np.asarray(self._array, dtype=dtype)
176+
elif np.__version__.startswith('2.0.0-dev0'):
177+
# Handle dev version for which we can't know based on version
178+
# number whether or not the copy keyword is supported.
179+
try:
177180
return np.asarray(self._array, dtype=dtype, copy=copy)
178-
raise ValueError("Conversion from an array_api_strict array to a NumPy ndarray is not supported")
181+
except TypeError:
182+
return np.asarray(self._array, dtype=dtype)
183+
else:
184+
return np.asarray(self._array, dtype=dtype, copy=copy)
179185

180186
# These are various helper functions to make the array behavior match the
181187
# spec in places where it either deviates from or is more strict than

array_api_strict/tests/test_array_object.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ def test_array_conversion():
364364
with pytest.raises(RuntimeError, match="Can not convert array"):
365365
asarray([a])
366366

367+
def test__array__warning():
368+
a = ones((2, 3))
369+
with pytest.warns(FutureWarning):
370+
np.array(a)
371+
367372
def test_allow_newaxis():
368373
a = ones(5)
369374
indexed_a = a[None, :]

0 commit comments

Comments
 (0)