Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-introduce __array__ with a warning #91

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 22 additions & 16 deletions array_api_strict/_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import operator
from enum import IntEnum
import warnings

from ._creation_functions import asarray
from ._dtypes import (
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions array_api_strict/tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, :]
Expand Down
Loading