2323from tests .common .debug import find_any , minimal
2424from tests .common .utils import fails_with , flaky
2525
26-
2726needs_xp_unique_values = pytest .mark .skipif (
2827 not hasattr (xp , "unique_values" ), reason = "optional API"
2928)
3029
3130
31+ # xp.unique_value() should return distinct NaNs - if not, tests that (rightly)
32+ # assume such behaviour will likely fail. This mark namely addresses mocking the
33+ # array module with NumPy 1.21, which treats NaNs as not distinct.
34+ # See https://mail.python.org/pipermail/numpy-discussion/2021-August/081995.html
35+ two_nans = xp .asarray ([float ("nan" ), float ("nan" )])
36+ assumes_distinct_nans = pytest .mark .xfail (
37+ hasattr (xp , "unique_values" ) and xp .unique_values (two_nans ).size != 2 ,
38+ reason = "NaNs not distinct" ,
39+ )
40+
41+
3242def assert_array_namespace (x ):
3343 """Check array has __array_namespace__() and it returns the correct module.
3444
@@ -199,19 +209,6 @@ def test_minimizes_to_fill():
199209 assert xp .all (smallest == 3.0 )
200210
201211
202- @needs_xp_unique_values
203- @given (
204- xps .arrays (
205- dtype = xp .float32 ,
206- elements = st .just (xp .nan ),
207- shape = xps .array_shapes (),
208- )
209- )
210- def test_count_unique (x ):
211- """Utility counts unique elements of arrays generated by unique strategy."""
212- assert xp .unique_values (x ).size == x .size
213-
214-
215212@needs_xp_unique_values
216213@given (xps .arrays (xp .int8 , st .integers (0 , 20 ), unique = True ))
217214def test_generate_unique_arrays (x ):
@@ -349,6 +346,7 @@ def test_floats_can_be_constrained_excluding_endpoints(x):
349346
350347
351348@needs_xp_unique_values
349+ @assumes_distinct_nans
352350@given (
353351 xps .arrays (
354352 dtype = xp .float32 ,
@@ -364,6 +362,7 @@ def test_is_still_unique_with_nan_fill(x):
364362
365363
366364@needs_xp_unique_values
365+ @assumes_distinct_nans
367366@given (
368367 xps .arrays (
369368 dtype = xp .float32 ,
@@ -404,6 +403,7 @@ def test_efficiently_generate_unique_arrays_using_all_elements(x):
404403
405404
406405@needs_xp_unique_values
406+ @assumes_distinct_nans
407407@given (st .data (), st .integers (- 100 , 100 ), st .integers (1 , 100 ))
408408def test_array_element_rewriting (data , start , size ):
409409 """Unique strategy generates arrays with expected elements."""
0 commit comments