99# obtain one at https://mozilla.org/MPL/2.0/.
1010
1111import typing
12+ import warnings
1213
1314import numpy as np
1415import pytest
1516
16- from hypothesis import given
17+ from hypothesis import given , strategies as st
18+ from hypothesis .errors import SmallSearchSpaceWarning
1719from hypothesis .extra .numpy import ArrayLike , NDArray , _NestedSequence , _SupportsArray
18- from hypothesis .strategies import builds , from_type
1920
2021from .test_from_dtype import STANDARD_TYPES
2122from tests .common .debug import assert_simple_property , find_any
2627needs_np_private_typing = {"reason" : "numpy._typing is not available" }
2728
2829
29- @given (dtype = from_type (np .dtype ))
30+ @given (dtype = st . from_type (np .dtype ))
3031def test_resolves_dtype_type (dtype ):
3132 assert isinstance (dtype , np .dtype )
3233
3334
34- @pytest .mark .parametrize ("typ" , [np .object_ , np .void ])
35- def test_does_not_resolve_nonscalar_types (typ ):
36- # Comparing the objects directly fails on Windows,
37- # so compare their reprs instead.
38- assert repr (from_type (typ )) == repr (builds (typ ))
35+ def test_does_not_resolve_nonscalar_types ():
36+ # this was previously a parametrized test over np.object_ and np.void which
37+ # used the same repr code path for the test. But then numpy changed their types
38+ # such that we defer evaluation for st.from_type and are no longer identical
39+ # to st.builds, but rather something morally equivalent to it. So we have
40+ # these slightly more complicated checks.
41+ with warnings .catch_warnings ():
42+ warnings .simplefilter ("ignore" , SmallSearchSpaceWarning )
43+ assert_simple_property (st .from_type (np .object_ ), lambda value : value is None )
44+
45+ with pytest .raises (TypeError ):
46+ # np.void() requires an argument, and so throws when instantiated
47+ assert_simple_property (st .from_type (np .void ))
3948
4049
4150@pytest .mark .parametrize ("typ" , STANDARD_TYPES_TYPE )
4251def test_resolves_and_varies_numpy_scalar_type (typ ):
4352 # Check that we find an instance that is not equal to the default
44- x = find_any (from_type (typ ), lambda x : x != type (x )())
53+ x = find_any (st . from_type (typ ), lambda x : x != type (x )())
4554 assert isinstance (x , typ )
4655
4756
4857@pytest .mark .parametrize ("atype" , [np .ndarray , NDArray ])
4958def test_resolves_unspecified_array_type (atype ):
5059 if atype is not None :
51- assert_simple_property (from_type (atype ), lambda v : isinstance (v , np .ndarray ))
60+ assert_simple_property (st . from_type (atype ), lambda v : isinstance (v , np .ndarray ))
5261
5362
5463def workaround (dtype ):
@@ -65,12 +74,12 @@ def workaround(dtype):
6574@pytest .mark .parametrize ("typ" , [workaround (t ) for t in STANDARD_TYPES_TYPE ])
6675def test_resolves_specified_ndarray_type (typ ):
6776 assert_simple_property (
68- from_type (np .ndarray [typ ]),
77+ st . from_type (np .ndarray [typ ]),
6978 lambda arr : isinstance (arr , np .ndarray ) and arr .dtype .type == typ ,
7079 )
7180
7281 assert_simple_property (
73- from_type (np .ndarray [typing .Any , typ ]),
82+ st . from_type (np .ndarray [typing .Any , typ ]),
7483 lambda arr : isinstance (arr , np .ndarray ) and arr .dtype .type == typ ,
7584 )
7685
@@ -79,20 +88,20 @@ def test_resolves_specified_ndarray_type(typ):
7988@pytest .mark .parametrize ("typ" , [workaround (t ) for t in STANDARD_TYPES_TYPE ])
8089def test_resolves_specified_NDArray_type (typ ):
8190 assert_simple_property (
82- from_type (NDArray [typ ]),
91+ st . from_type (NDArray [typ ]),
8392 lambda arr : isinstance (arr , np .ndarray ) and arr .dtype .type == typ ,
8493 )
8594
8695
8796@pytest .mark .skipif (NDArray is None , ** needs_np_typing )
8897def test_resolves_NDArray_with_dtype_union ():
89- strat = from_type (NDArray [np .float64 | np .complex128 ])
98+ strat = st . from_type (NDArray [np .float64 | np .complex128 ])
9099 find_any (strat , lambda arr : arr .dtype == np .dtype ("float64" ))
91100 find_any (strat , lambda arr : arr .dtype == np .dtype ("complex128" ))
92101
93102
94103@pytest .mark .skipif (ArrayLike is None , ** needs_np_typing )
95- @given (arr_like = from_type (ArrayLike ))
104+ @given (arr_like = st . from_type (ArrayLike ))
96105def test_resolves_ArrayLike_type (arr_like ):
97106 arr = np .array (arr_like )
98107 assert isinstance (arr , np .ndarray )
@@ -103,7 +112,7 @@ def test_resolves_ArrayLike_type(arr_like):
103112
104113@pytest .mark .skipif (_NestedSequence is None , ** needs_np_private_typing )
105114def test_resolves_specified_NestedSequence ():
106- @given (seq = from_type (_NestedSequence [int ]))
115+ @given (seq = st . from_type (_NestedSequence [int ]))
107116 def test (seq ):
108117 assert hasattr (seq , "__iter__" )
109118
@@ -120,20 +129,20 @@ def flatten(lst):
120129
121130
122131@pytest .mark .skipif (_NestedSequence is None , ** needs_np_private_typing )
123- @given (seq = from_type (_NestedSequence ))
132+ @given (seq = st . from_type (_NestedSequence ))
124133def test_resolves_unspecified_NestedSequence (seq ):
125134 assert hasattr (seq , "__iter__" )
126135
127136
128137@pytest .mark .skipif (_SupportsArray is None , ** needs_np_private_typing )
129- @given (arr = from_type (_SupportsArray ))
138+ @given (arr = st . from_type (_SupportsArray ))
130139def test_resolves_unspecified_SupportsArray (arr ):
131140 assert hasattr (arr , "__array__" )
132141
133142
134143@pytest .mark .skipif (_SupportsArray is None , ** needs_np_private_typing )
135144def test_resolves_SupportsArray ():
136- @given (arr = from_type (_SupportsArray [int ]))
145+ @given (arr = st . from_type (_SupportsArray [int ]))
137146 def test (arr ):
138147 assert hasattr (arr , "__array__" )
139148 assert np .asarray (arr ).dtype .kind == "i"
@@ -162,7 +171,7 @@ def test_resolve_ArrayLike_equivalent():
162171 ]
163172 )
164173
165- @given (arr_like = from_type (ArrayLike_like ))
174+ @given (arr_like = st . from_type (ArrayLike_like ))
166175 def test (arr_like ):
167176 arr = np .array (arr_like )
168177 assert isinstance (arr , np .ndarray )
0 commit comments