Skip to content

Commit

Permalink
Raise InvalidArgument with bad shape in xps.arrays()
Browse files Browse the repository at this point in the history
  • Loading branch information
honno committed Oct 20, 2021
1 parent 29c3178 commit 9c0a54a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch adds an error for when ``shapes`` in :func:`xps.arrays()` is not
passed as either a valid shape or strategy.
11 changes: 6 additions & 5 deletions hypothesis-python/src/hypothesis/extra/array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,17 @@ def _arrays(
return dtype.flatmap(
lambda d: _arrays(xp, d, shape, elements=elements, fill=fill, unique=unique)
)
elif isinstance(dtype, str):
dtype = dtype_from_name(xp, dtype)

if isinstance(shape, st.SearchStrategy):
return shape.flatmap(
lambda s: _arrays(xp, dtype, s, elements=elements, fill=fill, unique=unique)
)

if isinstance(dtype, str):
dtype = dtype_from_name(xp, dtype)

if isinstance(shape, int):
elif isinstance(shape, int):
shape = (shape,)
elif not isinstance(shape, tuple):
raise InvalidArgument(f"shape={shape} is not a valid shape or strategy")
check_argument(
all(isinstance(x, int) and x >= 0 for x in shape),
f"shape={shape!r}, but all dimensions must be non-negative integers.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def e(a, **kwargs):
e(xps.arrays, dtype=xp.int8, shape=(0.5,)),
e(xps.arrays, dtype=xp.int8, shape=1, fill=3),
e(xps.arrays, dtype=xp.int8, shape=1, elements="not a strategy"),
e(xps.arrays, dtype=xp.int8, shape=lambda: "not a strategy"),
e(xps.array_shapes, min_side=2, max_side=1),
e(xps.array_shapes, min_dims=3, max_dims=2),
e(xps.array_shapes, min_dims=-1),
Expand Down

0 comments on commit 9c0a54a

Please sign in to comment.