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

Remove __array__ #69

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 10 additions & 21 deletions array_api_strict/_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,17 @@ def __repr__(self: Array, /) -> str:
mid = np.array2string(self._array, separator=', ', prefix=prefix, suffix=suffix)
return prefix + mid + suffix

# This function is not required by the spec, but we implement it here for
# convenience so that np.asarray(array_api_strict.Array) will work.
# Disallow __array__, meaning calling `np.func()` on an array_api_strict
# array will give an error. If we don't explicitly disallow it, NumPy
# defaults to creating an object dtype array, which would lead to
# confusing error messages at best and surprising bugs at worst.
#
# The alternative of course is to just support __array__, which is what we
# used to do. But this isn't actually supported by the standard, so it can
# lead to code assuming np.asarray(other_array) would always work in the
# standard.
def __array__(self, dtype: None | np.dtype[Any] = None, copy: None | bool = None) -> npt.NDArray[Any]:
"""
Warning: this method is NOT part of the array API spec. Implementers
of other libraries need not include it, and users should not assume it
will be present in other implementations.

"""
# 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:
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")

# 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
7 changes: 0 additions & 7 deletions array_api_strict/tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,6 @@ def test_array_properties():
assert isinstance(b.mT, Array)
assert b.mT.shape == (3, 2)

def test___array__():
a = ones((2, 3), dtype=int16)
assert np.asarray(a) is a._array
b = np.asarray(a, dtype=np.float64)
assert np.all(np.equal(b, np.ones((2, 3), dtype=np.float64)))
assert b.dtype == np.float64

def test_allow_newaxis():
a = ones(5)
indexed_a = a[None, :]
Expand Down
Loading