Skip to content

Commit b0aa8e9

Browse files
authored
Merge pull request #3969 from tybug/string-kwargs-filter
Improve `draw_string_kwargs` efficiency
2 parents 0a17774 + c7347e8 commit b0aa8e9

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

hypothesis-python/tests/common/strategies.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ def build_intervals(intervals):
6565
yield batch
6666

6767

68-
def interval_lists(min_codepoint=0, max_codepoint=sys.maxunicode):
68+
def interval_lists(*, min_codepoint=0, max_codepoint=sys.maxunicode, min_size=0):
6969
return (
70-
st.lists(st.integers(min_codepoint, max_codepoint), unique=True)
70+
st.lists(
71+
st.integers(min_codepoint, max_codepoint),
72+
unique=True,
73+
min_size=min_size * 2,
74+
)
7175
.map(sorted)
7276
.map(build_intervals)
7377
)
7478

7579

76-
def intervals(min_codepoint=0, max_codepoint=sys.maxunicode):
77-
return st.builds(IntervalSet, interval_lists(min_codepoint, max_codepoint))
80+
def intervals(*, min_codepoint=0, max_codepoint=sys.maxunicode, min_size=0):
81+
return st.builds(
82+
IntervalSet,
83+
interval_lists(
84+
min_codepoint=min_codepoint, max_codepoint=max_codepoint, min_size=min_size
85+
),
86+
)

hypothesis-python/tests/conjecture/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ def draw_integer_kwargs(
168168

169169
@st.composite
170170
def draw_string_kwargs(draw, *, use_min_size=True, use_max_size=True, use_forced=False):
171-
interval_set = draw(intervals())
172-
# TODO relax this restriction once we handle empty pseudo-choices in the ir
173-
assume(len(interval_set) > 0)
171+
# TODO also sample empty intervals, ie remove this min_size, once we handle empty
172+
# pseudo-choices in the ir
173+
interval_set = draw(intervals(min_size=1))
174174
forced = (
175175
draw(TextStrategy(OneCharStringStrategy(interval_set))) if use_forced else None
176176
)

hypothesis-python/tests/conjecture/test_ir.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pytest
1616

17-
from hypothesis import HealthCheck, assume, example, given, settings, strategies as st
17+
from hypothesis import assume, example, given, strategies as st
1818
from hypothesis.errors import StopTest
1919
from hypothesis.internal.conjecture.data import (
2020
ConjectureData,
@@ -367,12 +367,7 @@ def test_data_with_empty_ir_tree_is_overrun():
367367
assert data.status is Status.OVERRUN
368368

369369

370-
# root cause of too_slow is filtering too much via assume in kwargs strategies.
371-
# exacerbated in this test because we draw kwargs twice.
372-
# TODO revisit and improve the kwargs strategies at some point, once the ir
373-
# is further along we can maybe remove e.g. a string assumption.
374370
@given(st.data())
375-
@settings(suppress_health_check=[HealthCheck.too_slow])
376371
def test_node_with_different_ir_type_is_invalid(data):
377372
node = data.draw(ir_nodes())
378373
(ir_type, kwargs) = data.draw(ir_types_and_kwargs())
@@ -406,7 +401,6 @@ def test_node_with_same_ir_type_but_different_value_is_invalid(data):
406401

407402

408403
@given(st.data())
409-
@settings(suppress_health_check=[HealthCheck.too_slow])
410404
def test_data_with_changed_was_forced(data):
411405
# we had a normal node and then tried to draw a different forced value from it.
412406
# ir tree: v1 [was_forced=False]
@@ -423,7 +417,6 @@ def test_data_with_changed_was_forced(data):
423417

424418

425419
@given(ir_nodes(was_forced=True))
426-
@settings(suppress_health_check=[HealthCheck.too_slow])
427420
def test_data_with_changed_forced_value(node):
428421
# we had a forced node and then tried to draw a different forced value from it.
429422
# ir tree: v1 [was_forced=True]

0 commit comments

Comments
 (0)