|
17 | 17 |
|
18 | 18 | import operator
|
19 | 19 | from enum import IntEnum
|
| 20 | +import warnings |
20 | 21 |
|
21 | 22 | from ._creation_functions import asarray
|
22 | 23 | from ._dtypes import (
|
@@ -159,23 +160,28 @@ def __repr__(self: Array, /) -> str:
|
159 | 160 | def __array__(self, dtype: None | np.dtype[Any] = None, copy: None | bool = None) -> npt.NDArray[Any]:
|
160 | 161 | # We have to allow this to be internally enabled as there's no other
|
161 | 162 | # 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: |
177 | 180 | 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) |
179 | 185 |
|
180 | 186 | # These are various helper functions to make the array behavior match the
|
181 | 187 | # spec in places where it either deviates from or is more strict than
|
|
0 commit comments