Skip to content

Commit

Permalink
Fix undefined slice error
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Sep 9, 2021
1 parent 246caae commit 702af09
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
RELEASE_TYPE: minor

This release allows :func:`~hypothesis.strategies.slices` to generate ``step=None``,
and fixes an off-by-one error where the ``start`` index could be equal to ``size``.
This works fine for all Python sequences and Numpy arrays, but is undefined behaviour
in the `Array API standard <https://data-apis.org/>`__ (see :pull:`3065`).
7 changes: 2 additions & 5 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,12 +1901,9 @@ def slices(draw: Any, size: int) -> slice:
if size == 0:
step = draw(none() | integers().filter(bool))
return slice(None, None, step)

min_start = min_stop = 0
max_start = max_stop = size
# For slices start is inclusive and stop is exclusive
start = draw(integers(min_start, max_start) | none())
stop = draw(integers(min_stop, max_stop) | none())
start = draw(integers(0, size - 1) | none())
stop = draw(integers(0, size) | none())

# Limit step size to be reasonable
if start is None and stop is None:
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/tests/cover/test_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_stop_stays_within_bounds(size):
@use_several_sizes
def test_start_stay_within_bounds(size):
assert_all_examples(
st.slices(size),
lambda x: x.start is None or (x.start >= -size and x.start <= size),
st.slices(size).filter(lambda x: x.start is not None),
lambda x: range(size)[x.start] or True, # no IndexError raised
)


Expand Down

0 comments on commit 702af09

Please sign in to comment.