Skip to content

Commit

Permalink
Slices permit step=None
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Sep 9, 2021
1 parent 17ad666 commit 246caae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: minor

This release allows :func:`~hypothesis.strategies.slices` to generate ``step=None``,
7 changes: 4 additions & 3 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,6 @@ def slices(draw: Any, size: int) -> slice:

min_start = min_stop = 0
max_start = max_stop = size
min_step = 1
# 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())
Expand All @@ -1919,14 +1918,16 @@ def slices(draw: Any, size: int) -> slice:
else:
max_step = abs(start - stop)

step = draw(integers(min_step, max_step or 1))
step = draw(integers(1, max_step or 1))

if (stop or 0) < (start or 0):
if (draw(booleans()) and start == stop) or (stop or 0) < (start or 0):
step *= -1

if draw(booleans()) and start is not None:
start -= size
if draw(booleans()) and stop is not None:
stop -= size
if (not draw(booleans())) and step == 1:
step = None

return slice(start, stop, step)
8 changes: 5 additions & 3 deletions hypothesis-python/tests/cover/test_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ def test_slices_will_shrink(size):
sliced = minimal(st.slices(size))
assert sliced.start == 0 or sliced.start is None
assert sliced.stop == 0 or sliced.stop is None
assert sliced.step == 1
assert sliced.step is None


@given(st.integers(1, 1000))
@settings(deadline=None)
def test_step_will_be_negative(size):
find_any(st.slices(size), lambda x: x.step < 0, settings(max_examples=10 ** 6))
find_any(
st.slices(size), lambda x: (x.step or 1) < 0, settings(max_examples=10 ** 6)
)


@given(st.integers(1, 1000))
@settings(deadline=None)
def test_step_will_be_positive(size):
find_any(st.slices(size), lambda x: x.step > 0)
find_any(st.slices(size), lambda x: (x.step or 1) > 0)


@pytest.mark.parametrize("size", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Expand Down

0 comments on commit 246caae

Please sign in to comment.