diff --git a/array_api_strict/_array_object.py b/array_api_strict/_array_object.py index c57d6ed..4b9a5de 100644 --- a/array_api_strict/_array_object.py +++ b/array_api_strict/_array_object.py @@ -17,6 +17,7 @@ import operator from enum import IntEnum +import warnings from ._creation_functions import asarray from ._dtypes import ( @@ -159,23 +160,28 @@ def __repr__(self: Array, /) -> str: def __array__(self, dtype: None | np.dtype[Any] = None, copy: None | bool = None) -> npt.NDArray[Any]: # We have to allow this to be internally enabled as there's no other # easy way to parse a list of Array objects in asarray(). - if _allow_array: - if self._device != CPU_DEVICE: - raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.") - # copy keyword is new in 2.0.0; for older versions don't use it - # retry without that keyword. - if np.__version__[0] < '2': - return np.asarray(self._array, dtype=dtype) - elif np.__version__.startswith('2.0.0-dev0'): - # Handle dev version for which we can't know based on version - # number whether or not the copy keyword is supported. - try: - return np.asarray(self._array, dtype=dtype, copy=copy) - except TypeError: - return np.asarray(self._array, dtype=dtype) - else: + if not _allow_array: + warnings.warn("The array_api_strict __array__ method was used. " + "This method is not part of the array API standard and will be " + "removed in a future version of array-api-strict. " + "See https://github.com/data-apis/array-api-strict/issues/67 " + "for more info.", FutureWarning, stacklevel=2) + + if self._device != CPU_DEVICE: + raise RuntimeError(f"Can not convert array on the '{self._device}' device to a Numpy array.") + # copy keyword is new in 2.0.0; for older versions don't use it + # retry without that keyword. + if np.__version__[0] < '2': + return np.asarray(self._array, dtype=dtype) + elif np.__version__.startswith('2.0.0-dev0'): + # Handle dev version for which we can't know based on version + # number whether or not the copy keyword is supported. + try: return np.asarray(self._array, dtype=dtype, copy=copy) - raise ValueError("Conversion from an array_api_strict array to a NumPy ndarray is not supported") + except TypeError: + return np.asarray(self._array, dtype=dtype) + else: + return np.asarray(self._array, dtype=dtype, copy=copy) # These are various helper functions to make the array behavior match the # spec in places where it either deviates from or is more strict than diff --git a/array_api_strict/tests/test_array_object.py b/array_api_strict/tests/test_array_object.py index 4f843ba..2c979d4 100644 --- a/array_api_strict/tests/test_array_object.py +++ b/array_api_strict/tests/test_array_object.py @@ -364,6 +364,11 @@ def test_array_conversion(): with pytest.raises(RuntimeError, match="Can not convert array"): asarray([a]) +def test__array__warning(): + a = ones((2, 3)) + with pytest.warns(FutureWarning): + np.array(a) + def test_allow_newaxis(): a = ones(5) indexed_a = a[None, :]