Skip to content

Commit 6bb0678

Browse files
committed
Update array_api extra and test suite to reflect new behaviour
* Remove resolved NaN comment in `xps.arrays()` doc * `unique` -> `unique_values()` * Remove `count_unique()` helper * Use `astype()` before `sum()`
1 parent 9fdc154 commit 6bb0678

2 files changed

Lines changed: 28 additions & 51 deletions

File tree

‎hypothesis-python/src/hypothesis/extra/array_api.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ def _arrays(
406406
disabled entirely and every element will be generated independently.
407407
* ``unique`` specifies if the elements of the array should all be distinct
408408
from one another; if fill is also set, the only valid values for fill to
409-
return are NaN values. Note that Hypothesis always allows multiple NaN
410-
values, even though `xp.unique() might only return a single NaN.
411-
<https://data-apis.org/array-api/latest/API_specification/set_functions.html#objects-in-api>`__
409+
return are NaN values.
412410
413411
Arrays of specified ``dtype`` and ``shape`` are generated for example
414412
like this:
@@ -875,6 +873,7 @@ def floating_dtypes(
875873
# Constants
876874
nan=np.nan,
877875
# Data type functions
876+
astype=lambda x, d: x.astype(d),
878877
iinfo=np.iinfo,
879878
finfo=np.finfo,
880879
broadcast_arrays=np.broadcast_arrays,
@@ -899,7 +898,7 @@ def floating_dtypes(
899898
# Sorting functions
900899
sort=np.sort,
901900
# Set functions
902-
unique=np.unique,
901+
unique_values=np.unique,
903902
# Utility functions
904903
any=np.any,
905904
all=np.all,

‎hypothesis-python/tests/array_api/test_arrays.py‎

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
from tests.common.debug import find_any, minimal
2424
from tests.common.utils import fails_with, flaky
2525

26-
needs_xp_unique = pytest.mark.skipif(not hasattr(xp, "unique"), reason="optional API")
26+
27+
needs_xp_unique_values = pytest.mark.skipif(
28+
not hasattr(xp, "unique_values"), reason="optional API"
29+
)
2730

2831

2932
def assert_array_namespace(x):
@@ -196,33 +199,7 @@ def test_minimizes_to_fill():
196199
assert xp.all(smallest == 3.0)
197200

198201

199-
def count_unique(x):
200-
"""Returns the number of unique elements.
201-
NaN values are treated as unique to each other.
202-
203-
The Array API doesn't specify how ``unique()`` should behave for Nan values,
204-
so this method provides consistent behaviour.
205-
"""
206-
n_unique = 0
207-
208-
nan_index = xp.isnan(x)
209-
for isnan, count in zip(*xp.unique(nan_index, return_counts=True)):
210-
if isnan:
211-
n_unique += count
212-
break
213-
214-
# TODO: The Array API makes boolean indexing optional, so in the future this
215-
# will need to be reworked if we want to test libraries other than NumPy.
216-
# If not possible, errors should be caught and the test skipped.
217-
# See https://github.com/data-apis/array-api/issues/249
218-
filtered_x = x[~nan_index]
219-
unique_x = xp.unique(filtered_x)
220-
n_unique += unique_x.size
221-
222-
return n_unique
223-
224-
225-
@needs_xp_unique
202+
@needs_xp_unique_values
226203
@given(
227204
xps.arrays(
228205
dtype=xp.float32,
@@ -232,14 +209,14 @@ def count_unique(x):
232209
)
233210
def test_count_unique(x):
234211
"""Utility counts unique elements of arrays generated by unique strategy."""
235-
assert count_unique(x) == x.size
212+
assert xp.unique_values(x).size == x.size
236213

237214

238-
@needs_xp_unique
215+
@needs_xp_unique_values
239216
@given(xps.arrays(xp.int8, st.integers(0, 20), unique=True))
240217
def test_generate_unique_arrays(x):
241218
"""Generates unique arrays."""
242-
assert count_unique(x) == x.size
219+
assert xp.unique_values(x).size == x.size
243220

244221

245222
@fails_with(InvalidArgument)
@@ -266,15 +243,16 @@ def test_cannot_fill_arrays_with_non_castable_value():
266243
)
267244
def test_generate_unique_arrays_with_high_collision_elements(x):
268245
"""Generates unique arrays with just elements of 0.0 and NaN fill."""
269-
assert xp.sum(x == 0.0) <= 1
246+
zero_mask = x == 0.0
247+
assert xp.sum(xp.astype(zero_mask, xp.uint8)) <= 1
270248

271249

272-
@needs_xp_unique
250+
@needs_xp_unique_values
273251
@given(xps.arrays(xp.int8, (4,), elements=st.integers(0, 3), unique=True))
274252
def test_generate_unique_arrays_using_all_elements(x):
275253
"""Unique strategy with elements strategy range equal to its size will only
276254
generate arrays with one of each possible element."""
277-
assert count_unique(x) == x.size
255+
assert xp.unique_values(x).size == x.size
278256

279257

280258
def test_may_fill_unique_arrays_with_nan():
@@ -370,7 +348,7 @@ def test_floats_can_be_constrained_excluding_endpoints(x):
370348
assert xp.all(x < 1)
371349

372350

373-
@needs_xp_unique
351+
@needs_xp_unique_values
374352
@given(
375353
xps.arrays(
376354
dtype=xp.float32,
@@ -382,10 +360,10 @@ def test_floats_can_be_constrained_excluding_endpoints(x):
382360
)
383361
def test_is_still_unique_with_nan_fill(x):
384362
"""Unique strategy with NaN fill generates unique arrays."""
385-
assert count_unique(x) == x.size
363+
assert xp.unique_values(x).size == x.size
386364

387365

388-
@needs_xp_unique
366+
@needs_xp_unique_values
389367
@given(
390368
xps.arrays(
391369
dtype=xp.float32,
@@ -398,10 +376,10 @@ def test_is_still_unique_with_nan_fill(x):
398376
def test_unique_array_with_fill_can_use_all_elements(x):
399377
"""Unique strategy with elements range equivalent to its size and NaN fill
400378
can generate arrays with all possible values."""
401-
assume(count_unique(x) == x.size)
379+
assume(xp.unique_values(x).size == x.size)
402380

403381

404-
@needs_xp_unique
382+
@needs_xp_unique_values
405383
@given(xps.arrays(dtype=xp.uint8, shape=25, unique=True, fill=st.nothing()))
406384
def test_generate_unique_arrays_without_fill(x):
407385
"""Generate arrays from unique strategy with no fill.
@@ -411,21 +389,21 @@ def test_generate_unique_arrays_without_fill(x):
411389
colisions thanks to the birthday paradox, but finding unique values should
412390
still be easy.
413391
"""
414-
assume(count_unique(x) == x.size)
392+
assume(xp.unique_values(x).size == x.size)
415393

416394

417-
@needs_xp_unique
395+
@needs_xp_unique_values
418396
@given(xps.arrays(dtype=xp.int8, shape=255, unique=True))
419397
def test_efficiently_generate_unique_arrays_using_all_elements(x):
420398
"""Unique strategy with elements strategy range equivalent to its size
421399
generates arrays with all possible values. Generation is not too slow.
422400
423401
Avoids the birthday paradox with UniqueSampledListStrategy.
424402
"""
425-
assert count_unique(x) == x.size
403+
assert xp.unique_values(x).size == x.size
426404

427405

428-
@needs_xp_unique
406+
@needs_xp_unique_values
429407
@given(st.data(), st.integers(-100, 100), st.integers(1, 100))
430408
def test_array_element_rewriting(data, start, size):
431409
"""Unique strategy generates arrays with expected elements."""
@@ -476,21 +454,21 @@ def distinct_integers(draw):
476454
return i
477455

478456

479-
@needs_xp_unique
457+
@needs_xp_unique_values
480458
@given(xps.arrays(xp.uint64, 10, elements=distinct_integers()))
481459
def test_does_not_reuse_distinct_integers(x):
482460
"""Strategy with distinct integer elements strategy generates arrays with
483461
distinct values."""
484-
assert count_unique(x) == x.size
462+
assert xp.unique_values(x).size == x.size
485463

486464

487-
@needs_xp_unique
465+
@needs_xp_unique_values
488466
def test_may_reuse_distinct_integers_if_asked():
489467
"""Strategy with shared elements and fill strategies of distinct integers
490468
may generate arrays with non-distinct values."""
491469
find_any(
492470
xps.arrays(
493471
xp.uint64, 10, elements=distinct_integers(), fill=distinct_integers()
494472
),
495-
lambda x: count_unique(x) < x.size,
473+
lambda x: xp.unique_values(x).size < x.size,
496474
)

0 commit comments

Comments
 (0)