summaryrefslogtreecommitdiff
path: root/numpy/array_api/tests/test_array_object.py
diff options
context:
space:
mode:
authorAaron Meurer <asmeurer@gmail.com>2021-12-06 13:59:08 -0700
committerAaron Meurer <asmeurer@gmail.com>2021-12-06 13:59:08 -0700
commit74a3ee7a8b75bf6dc271c9a1a4b55d2ad9758420 (patch)
tree1b6117982b6255f076d95dac7095e2faaf7f63d9 /numpy/array_api/tests/test_array_object.py
parent7ca1d1ad0dc86f0d20414c946eb2b6e8dc19c367 (diff)
downloadnumpy-74a3ee7a8b75bf6dc271c9a1a4b55d2ad9758420.tar.gz
ENH: Add __array__ to the array_api Array object
This is *NOT* part of the array API spec (so it should not be relied on for portable code). However, without this, np.asarray(np.array_api.Array) produces an object array instead of doing the conversion to a NumPy array as expected. This would work once np.asarray() implements dlpack support, but until then, it seems reasonable to make the conversion work. Note that the reverse, calling np.array_api.asarray(np.array), already works because np.array_api.asarray() is just a wrapper for np.asarray().
Diffstat (limited to 'numpy/array_api/tests/test_array_object.py')
-rw-r--r--numpy/array_api/tests/test_array_object.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/numpy/array_api/tests/test_array_object.py b/numpy/array_api/tests/test_array_object.py
index deab50693..b980bacca 100644
--- a/numpy/array_api/tests/test_array_object.py
+++ b/numpy/array_api/tests/test_array_object.py
@@ -315,3 +315,10 @@ def test_array_properties():
assert a.mT.shape == (1, 3, 2)
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