@@ -136,6 +136,10 @@ def test_recarray(simple_dtype, packed_dtype):
136
136
assert_equal (arr , elements , simple_dtype )
137
137
assert_equal (arr , elements , packed_dtype )
138
138
139
+ # Show what recarray's look like in NumPy.
140
+ assert type (arr [0 ]) == np .void
141
+ assert type (arr [0 ].item ()) == tuple
142
+
139
143
if dtype == simple_dtype :
140
144
assert m .print_rec_simple (arr ) == [
141
145
"s:0,0,0,-0" ,
@@ -296,6 +300,47 @@ def test_vectorize():
296
300
np .testing .assert_array_equal (array , array_2 )
297
301
298
302
303
+ def test_cls_and_dtype_conversion (simple_dtype ):
304
+ s = m .SimpleStruct ()
305
+ assert s .astuple () == (False , 0 , 0. , 0. )
306
+ assert m .SimpleStruct .fromtuple (s .astuple ()).astuple () == s .astuple ()
307
+
308
+ s .uint_ = 2
309
+ assert m .f_simple (s ) == 20
310
+
311
+ # Try as recarray of shape==(1,).
312
+ s_recarray = np .array ([(False , 2 , 0. , 0. )], dtype = simple_dtype )
313
+ # Show that this will work for vectorized case.
314
+ np .testing .assert_array_equal (m .f_simple_vectorized (s_recarray ), [20 ])
315
+
316
+ # Show as a scalar that inherits from np.generic.
317
+ s_scalar = s_recarray [0 ]
318
+ assert isinstance (s_scalar , np .void )
319
+ assert m .f_simple (s_scalar ) == 20
320
+
321
+ # Show that an *array* scalar (np.ndarray.shape == ()) does not convert.
322
+ # More specifically, conversion to SimpleStruct is not implicit.
323
+ s_recarray_scalar = s_recarray .reshape (())
324
+ assert isinstance (s_recarray_scalar , np .ndarray )
325
+ assert s_recarray_scalar .dtype == simple_dtype
326
+ with pytest .raises (TypeError ) as excinfo :
327
+ m .f_simple (s_recarray_scalar )
328
+ assert 'incompatible function arguments' in str (excinfo .value )
329
+ # Explicitly convert to m.SimpleStruct.
330
+ assert m .f_simple (
331
+ m .SimpleStruct .fromtuple (s_recarray_scalar .item ())) == 20
332
+
333
+ # Show that an array of dtype=object does *not* convert.
334
+ s_array_object = np .array ([s ])
335
+ assert s_array_object .dtype == object
336
+ with pytest .raises (TypeError ) as excinfo :
337
+ m .f_simple_vectorized (s_array_object )
338
+ assert 'incompatible function arguments' in str (excinfo .value )
339
+ # Explicitly convert to `np.array(..., dtype=simple_dtype)`
340
+ s_array = np .array ([s .astuple ()], dtype = simple_dtype )
341
+ np .testing .assert_array_equal (m .f_simple_vectorized (s_array ), [20 ])
342
+
343
+
299
344
def test_register_dtype ():
300
345
with pytest .raises (RuntimeError ) as excinfo :
301
346
m .register_dtype ()
0 commit comments