1
1
from __future__ import annotations
2
2
3
3
import copy
4
+ import warnings
4
5
from collections .abc import Mapping
5
6
from typing import TYPE_CHECKING , Any , Generic , cast , overload
6
7
@@ -66,13 +67,13 @@ def test_namedarray_init() -> None:
66
67
expected = np .array ([1 , 2 ], dtype = dtype )
67
68
actual : NamedArray [Any , np .dtype [np .int8 ]]
68
69
actual = NamedArray (("x" ,), expected )
69
- assert np .array_equal (actual .data , expected )
70
+ assert np .array_equal (np . asarray ( actual .data ) , expected )
70
71
71
72
with pytest .raises (AttributeError ):
72
73
expected2 = [1 , 2 ]
73
74
actual2 : NamedArray [Any , Any ]
74
75
actual2 = NamedArray (("x" ,), expected2 ) # type: ignore[arg-type]
75
- assert np .array_equal (actual2 .data , expected2 )
76
+ assert np .array_equal (np . asarray ( actual2 .data ) , expected2 )
76
77
77
78
78
79
@pytest .mark .parametrize (
@@ -101,7 +102,7 @@ def test_from_array(
101
102
else :
102
103
actual = from_array (dims , data )
103
104
104
- assert np .array_equal (actual .data , expected )
105
+ assert np .array_equal (np . asarray ( actual .data ) , expected )
105
106
106
107
107
108
def test_from_array_with_masked_array () -> None :
@@ -114,7 +115,8 @@ def test_from_array_with_masked_array() -> None:
114
115
def test_from_array_with_0d_object () -> None :
115
116
data = np .empty ((), dtype = object )
116
117
data [()] = (10 , 12 , 12 )
117
- np .array_equal (from_array ((), data ).data , data )
118
+ narr = from_array ((), data )
119
+ np .array_equal (np .asarray (narr .data ), data )
118
120
119
121
120
122
# TODO: Make xr.core.indexing.ExplicitlyIndexed pass as a subclass of_arrayfunction_or_api
@@ -140,7 +142,7 @@ def test_properties() -> None:
140
142
named_array : NamedArray [Any , Any ]
141
143
named_array = NamedArray (["x" , "y" ], data , {"key" : "value" })
142
144
assert named_array .dims == ("x" , "y" )
143
- assert np .array_equal (named_array .data , data )
145
+ assert np .array_equal (np . asarray ( named_array .data ) , data )
144
146
assert named_array .attrs == {"key" : "value" }
145
147
assert named_array .ndim == 2
146
148
assert named_array .sizes == {"x" : 2 , "y" : 5 }
@@ -162,7 +164,7 @@ def test_attrs() -> None:
162
164
def test_data (random_inputs : np .ndarray [Any , Any ]) -> None :
163
165
named_array : NamedArray [Any , Any ]
164
166
named_array = NamedArray (["x" , "y" , "z" ], random_inputs )
165
- assert np .array_equal (named_array .data , random_inputs )
167
+ assert np .array_equal (np . asarray ( named_array .data ) , random_inputs )
166
168
with pytest .raises (ValueError ):
167
169
named_array .data = np .random .random ((3 , 4 )).astype (np .float64 )
168
170
@@ -181,11 +183,11 @@ def test_real_and_imag() -> None:
181
183
named_array = NamedArray (["x" ], arr )
182
184
183
185
actual_real : duckarray [Any , np .dtype [np .float64 ]] = named_array .real .data
184
- assert np .array_equal (actual_real , expected_real )
186
+ assert np .array_equal (np . asarray ( actual_real ) , expected_real )
185
187
assert actual_real .dtype == expected_real .dtype
186
188
187
189
actual_imag : duckarray [Any , np .dtype [np .float64 ]] = named_array .imag .data
188
- assert np .array_equal (actual_imag , expected_imag )
190
+ assert np .array_equal (np . asarray ( actual_imag ) , expected_imag )
189
191
assert actual_imag .dtype == expected_imag .dtype
190
192
191
193
@@ -214,7 +216,7 @@ def test_0d_object() -> None:
214
216
named_array = from_array ([], (10 , 12 , 12 ))
215
217
expected_data = np .empty ((), dtype = object )
216
218
expected_data [()] = (10 , 12 , 12 )
217
- assert np .array_equal (named_array .data , expected_data )
219
+ assert np .array_equal (np . asarray ( named_array .data ) , expected_data )
218
220
219
221
assert named_array .dims == ()
220
222
assert named_array .sizes == {}
@@ -294,6 +296,20 @@ def test_duck_array_typevar(a: duckarray[Any, _DType]) -> duckarray[Any, _DType]
294
296
test_duck_array_typevar (numpy_a )
295
297
test_duck_array_typevar (custom_a )
296
298
299
+ # Test numpy's array api:
300
+ with warnings .catch_warnings ():
301
+ warnings .filterwarnings (
302
+ "ignore" ,
303
+ r"The numpy.array_api submodule is still experimental" ,
304
+ category = UserWarning ,
305
+ )
306
+ import numpy .array_api as nxp
307
+
308
+ # TODO: nxp doesn't use dtype typevars, so can only use Any for the moment:
309
+ arrayapi_a : duckarray [Any , Any ] # duckarray[Any, np.dtype[np.int64]]
310
+ arrayapi_a = nxp .asarray ([2.1 , 4 ], dtype = np .dtype (np .int64 ))
311
+ test_duck_array_typevar (arrayapi_a )
312
+
297
313
298
314
def test_new_namedarray () -> None :
299
315
dtype_float = np .dtype (np .float32 )
0 commit comments