Skip to content

Commit 0b4dc63

Browse files
authored
Merge pull request #3109 from honno/clean-arrays
Clean up `extra.array_api.ArrayStrategy`
2 parents f5ec0a8 + 86f6799 commit 0b4dc63

3 files changed

Lines changed: 63 additions & 45 deletions

File tree

‎hypothesis-python/RELEASE.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch cleans up internal logic for :func:`xps.arrays()`. There is no
4+
user-visible change.

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

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def find_castable_builtin_for_dtype(
133133
# None equals NumPy's xp.float64 object, so we specifically skip it here to
134134
# ensure that InvalidArgument is still raised. xp.float64 is in fact an
135135
# alias of np.dtype('float64'), and its equality with None is meant to be
136-
# deprecated at some point - see https://github.com/numpy/numpy/issues/18434.
136+
# deprecated at some point. See https://github.com/numpy/numpy/issues/18434
137137
if dtype is not None and dtype in float_dtypes:
138138
return float
139139

@@ -266,28 +266,14 @@ def __init__(self, xp, elements_strategy, dtype, shape, fill, unique):
266266
self.array_size = math.prod(shape)
267267
self.builtin = find_castable_builtin_for_dtype(xp, dtype)
268268

269-
def set_value(self, result, i, val, strategy=None):
270-
strategy = strategy or self.elements_strategy
271-
try:
272-
result[i] = val
273-
except TypeError as e:
274-
raise InvalidArgument(
275-
f"Could not add generated array element {val!r} "
276-
f"of dtype {type(val)} to array of dtype {result.dtype}."
277-
) from e
278-
self.check_set_value(val, result[i], strategy)
279-
280269
def check_set_value(self, val, val_0d, strategy):
281-
if self.builtin is bool:
282-
finite = True
283-
else:
284-
finite = self.xp.isfinite(val_0d)
270+
finite = self.builtin is bool or self.xp.isfinite(val_0d)
285271
if finite and self.builtin(val_0d) != val:
286272
raise InvalidArgument(
287273
f"Generated array element {val!r} from strategy {strategy} "
288-
f"cannot be represented as dtype {self.dtype}. "
274+
f"cannot be represented with dtype {self.dtype}. "
289275
f"Array module {self.xp.__name__} instead "
290-
f"represents the element as {val_0d!r}. "
276+
f"represents the element as {val_0d}. "
291277
"Consider using a more precise elements strategy, "
292278
"for example passing the width argument to floats()."
293279
)
@@ -302,26 +288,34 @@ def do_draw(self, data):
302288
# elements strategy does not produce reusable values), so we must
303289
# generate a fully dense array with a freshly drawn value for each
304290
# entry.
305-
306-
# This could legitimately be a xp.empty, but the performance gains
307-
# for that are likely marginal, so there's really not much point
308-
# risking undefined behaviour shenanigans.
309-
result = self.xp.zeros(self.array_size, dtype=self.dtype)
310-
311-
if self.unique:
312-
seen = set()
313-
elems = st.lists(
291+
elems = data.draw(
292+
st.lists(
314293
self.elements_strategy,
315294
min_size=self.array_size,
316295
max_size=self.array_size,
317-
unique=True,
296+
unique=self.unique,
318297
)
319-
for i, v in enumerate(data.draw(elems)):
320-
self.set_value(result, i, v)
321-
else:
322-
for i in range(self.array_size):
323-
val = data.draw(self.elements_strategy)
324-
self.set_value(result, i, val)
298+
)
299+
try:
300+
result = self.xp.asarray(elems, dtype=self.dtype)
301+
except Exception as e:
302+
if len(elems) <= 6:
303+
f_elems = str(elems)
304+
else:
305+
f_elems = f"[{elems[0]}, {elems[1]}, ..., {elems[-2]}, {elems[-1]}]"
306+
types = tuple(
307+
sorted({type(e) for e in elems}, key=lambda t: t.__name__)
308+
)
309+
f_types = f"type {types[0]}" if len(types) == 1 else f"types {types}"
310+
raise InvalidArgument(
311+
f"Generated elements {f_elems} from strategy "
312+
f"{self.elements_strategy} could not be converted "
313+
f"to array of dtype {self.dtype}. "
314+
f"Consider if elements of {f_types} "
315+
f"are compatible with {self.dtype}."
316+
) from e
317+
for i in range(self.array_size):
318+
self.check_set_value(elems[i], result[i], self.elements_strategy)
325319
else:
326320
# We draw arrays as "sparse with an offset". We assume not every
327321
# element will be assigned and so first draw a single value from our
@@ -338,7 +332,7 @@ def do_draw(self, data):
338332
f"with fill value {fill_val!r}"
339333
) from e
340334
sample = result[0]
341-
self.check_set_value(fill_val, sample, strategy=self.fill)
335+
self.check_set_value(fill_val, sample, self.fill)
342336
if self.unique and not self.xp.all(self.xp.isnan(result)):
343337
raise InvalidArgument(
344338
f"Array module {self.xp.__name__} did not recognise fill "
@@ -371,7 +365,14 @@ def do_draw(self, data):
371365
continue
372366
else:
373367
seen.add(val)
374-
self.set_value(result, i, val)
368+
try:
369+
result[i] = val
370+
except Exception as e:
371+
raise InvalidArgument(
372+
f"Could not add generated array element {val!r} "
373+
f"of type {type(val)} to array of dtype {result.dtype}."
374+
) from e
375+
self.check_set_value(val, result[i], self.elements_strategy)
375376
assigned.add(i)
376377

377378
result = self.xp.reshape(result, self.shape)
@@ -459,8 +460,9 @@ def _arrays(
459460
hundreds or more elements, having a fill value is essential if you want
460461
your tests to run in reasonable time.
461462
"""
462-
463-
check_xp_attributes(xp, ["zeros", "full", "all", "isnan", "isfinite", "reshape"])
463+
check_xp_attributes(
464+
xp, ["asarray", "zeros", "full", "all", "isnan", "isfinite", "reshape"]
465+
)
464466

465467
if isinstance(dtype, st.SearchStrategy):
466468
return dtype.flatmap(

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ def test_generate_arrays_from_unsigned_ints(x):
126126
assert_array_namespace(x)
127127

128128

129+
@given(
130+
xps.arrays(
131+
dtype=xp.uint8,
132+
shape=(5, 5),
133+
elements=xps.from_dtype(xp.uint8).map(lambda e: xp.asarray(e, dtype=xp.uint8)),
134+
)
135+
)
136+
def test_generate_arrays_from_0d_arrays(x):
137+
"""Generate arrays from 0d array elements."""
138+
assert x.shape == (5, 5)
139+
assert_array_namespace(x)
140+
141+
129142
def test_minimize_arrays_with_default_dtype_shape_strategies():
130143
"""Strategy with default scalar_dtypes and array_shapes strategies minimize
131144
to a boolean 1-dimensional array of size 1."""
@@ -201,6 +214,7 @@ def count_unique(x):
201214
# TODO: The Array API makes boolean indexing optional, so in the future this
202215
# will need to be reworked if we want to test libraries other than NumPy.
203216
# If not possible, errors should be caught and the test skipped.
217+
# See https://github.com/data-apis/array-api/issues/249
204218
filtered_x = x[~nan_index]
205219
unique_x = xp.unique(filtered_x)
206220
n_unique += unique_x.size
@@ -228,19 +242,17 @@ def test_generate_unique_arrays(x):
228242
assert count_unique(x) == x.size
229243

230244

231-
def test_cannot_draw_unique_arrays_with_too_small_elements():
245+
@fails_with(InvalidArgument)
246+
@given(xps.arrays(xp.int8, 10, elements=st.integers(0, 5), unique=True))
247+
def test_cannot_draw_unique_arrays_with_too_small_elements(_):
232248
"""Unique strategy with elements strategy range smaller than its size raises
233249
helpful error."""
234-
strat = xps.arrays(xp.int8, 10, elements=st.integers(0, 5), unique=True)
235-
with pytest.raises(InvalidArgument):
236-
strat.example()
237250

238251

252+
@fails_with(InvalidArgument)
253+
@given(xps.arrays(xp.int8, 10, fill=st.just("not a castable value")))
239254
def test_cannot_fill_arrays_with_non_castable_value():
240255
"""Strategy with fill not castable to dtype raises helpful error."""
241-
strat = xps.arrays(xp.int8, 10, fill=st.just("not a castable value"))
242-
with pytest.raises(InvalidArgument):
243-
strat.example()
244256

245257

246258
@given(

0 commit comments

Comments
 (0)