1
+ import itertools
2
+
1
3
import numpy as np
2
4
3
5
@@ -24,22 +26,39 @@ def test_dtype_identity():
24
26
annotated_int_array = np .asarray (int_array , dtype = unequal_type )
25
27
assert annotated_int_array is not int_array
26
28
assert annotated_int_array .base is int_array
27
-
28
- # These ``asarray()`` calls may produce a new view or a copy,
29
- # but never the same object.
30
- long_int_array = np .asarray (int_array , dtype = 'l' )
31
- assert long_int_array is not int_array
32
- assert np .asarray (int_array , dtype = 'q' ) is not int_array
33
- assert np .asarray (long_int_array , dtype = 'q' ) is not long_int_array
34
- assert long_int_array is not np .asarray (int_array , dtype = 'l' )
35
- assert long_int_array .base is np .asarray (int_array , dtype = 'l' ).base
36
-
29
+ # Create an equivalent descriptor with a new and distinct dtype instance.
37
30
equivalent_requirement = np .dtype ('i' , metadata = {'spam' : True })
38
31
annotated_int_array_alt = np .asarray (annotated_int_array ,
39
32
dtype = equivalent_requirement )
40
- # The descriptors are equivalent, but we have created
41
- # distinct dtype instances.
42
33
assert unequal_type == equivalent_requirement
43
34
assert unequal_type is not equivalent_requirement
44
35
assert annotated_int_array_alt is not annotated_int_array
45
36
assert annotated_int_array_alt .dtype is equivalent_requirement
37
+
38
+ # Check the same logic for a pair of C types whose equivalence may vary
39
+ # between computing environments.
40
+ # Find an equivalent pair.
41
+ integer_type_codes = ('i' , 'l' , 'q' )
42
+ integer_dtypes = [np .dtype (code ) for code in integer_type_codes ]
43
+ typeA = None
44
+ typeB = None
45
+ for typeA , typeB in itertools .permutations (integer_dtypes , r = 2 ):
46
+ if typeA == typeB :
47
+ assert typeA is not typeB
48
+ break
49
+ assert isinstance (typeA , np .dtype ) and isinstance (typeB , np .dtype )
50
+
51
+ # These ``asarray()`` calls may produce a new view or a copy,
52
+ # but never the same object.
53
+ long_int_array = np .asarray (int_array , dtype = 'l' )
54
+ long_long_int_array = np .asarray (int_array , dtype = 'q' )
55
+ assert long_int_array is not int_array
56
+ assert long_long_int_array is not int_array
57
+ assert np .asarray (long_int_array , dtype = 'q' ) is not long_int_array
58
+ array_a = np .asarray (int_array , dtype = typeA )
59
+ assert typeA == typeB
60
+ assert typeA is not typeB
61
+ assert array_a .dtype is typeA
62
+ assert array_a is not np .asarray (array_a , dtype = typeB )
63
+ assert np .asarray (array_a , dtype = typeB ).dtype is typeB
64
+ assert array_a is np .asarray (array_a , dtype = typeB ).base
0 commit comments