Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve draw_string_kwargs efficiency #3969

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions hypothesis-python/tests/common/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,22 @@ def build_intervals(intervals):
yield batch


def interval_lists(min_codepoint=0, max_codepoint=sys.maxunicode):
def interval_lists(*, min_codepoint=0, max_codepoint=sys.maxunicode, min_size=0):
return (
st.lists(st.integers(min_codepoint, max_codepoint), unique=True)
st.lists(
st.integers(min_codepoint, max_codepoint),
unique=True,
min_size=min_size * 2,
)
.map(sorted)
.map(build_intervals)
)


def intervals(min_codepoint=0, max_codepoint=sys.maxunicode):
return st.builds(IntervalSet, interval_lists(min_codepoint, max_codepoint))
def intervals(*, min_codepoint=0, max_codepoint=sys.maxunicode, min_size=0):
return st.builds(
IntervalSet,
interval_lists(
min_codepoint=min_codepoint, max_codepoint=max_codepoint, min_size=min_size
),
)
6 changes: 3 additions & 3 deletions hypothesis-python/tests/conjecture/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def draw_integer_kwargs(

@st.composite
def draw_string_kwargs(draw, *, use_min_size=True, use_max_size=True, use_forced=False):
interval_set = draw(intervals())
# TODO relax this restriction once we handle empty pseudo-choices in the ir
assume(len(interval_set) > 0)
# TODO also sample empty intervals, ie remove this min_size, once we handle empty
# pseudo-choices in the ir
interval_set = draw(intervals(min_size=1))
forced = (
draw(TextStrategy(OneCharStringStrategy(interval_set))) if use_forced else None
)
Expand Down
9 changes: 1 addition & 8 deletions hypothesis-python/tests/conjecture/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import pytest

from hypothesis import HealthCheck, assume, example, given, settings, strategies as st
from hypothesis import assume, example, given, strategies as st
from hypothesis.errors import StopTest
from hypothesis.internal.conjecture.data import (
ConjectureData,
Expand Down Expand Up @@ -367,12 +367,7 @@ def test_data_with_empty_ir_tree_is_overrun():
assert data.status is Status.OVERRUN


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


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


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