Skip to content

Commit 3315be6

Browse files
committed
avoid nested-given helpers on crosshair
1 parent da944cc commit 3315be6

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

hypothesis-python/tests/common/debug.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
from unittest import SkipTest
12+
1113
from hypothesis import HealthCheck, Phase, Verbosity, given, settings as Settings
14+
from hypothesis.control import _current_build_context
1215
from hypothesis.errors import Found, NoSuchExample, Unsatisfiable
1316
from hypothesis.internal.reflection import get_pretty_function_description
1417

@@ -26,6 +29,11 @@ def minimal(definition, condition=lambda x: True, settings=None, timeout_after=1
2629
runtime = None
2730
result = None
2831

32+
if (
33+
context := _current_build_context.value
34+
) and context.data.provider.avoid_realization:
35+
raise SkipTest("`minimal()` helper not supported under symbolic execution")
36+
2937
def wrapped_condition(x):
3038
nonlocal runtime
3139
if timeout_after is not None:
@@ -71,6 +79,13 @@ def inner(x):
7179

7280

7381
def find_any(definition, condition=lambda _: True, settings=None):
82+
# If nested within an existing @given
83+
if context := _current_build_context.value:
84+
while True:
85+
if condition(s := context.data.draw(definition)):
86+
return s
87+
88+
# If top-level
7489
settings = settings or Settings.default
7590
return minimal(
7691
definition,
@@ -83,8 +98,7 @@ def find_any(definition, condition=lambda _: True, settings=None):
8398

8499
def assert_no_examples(strategy, condition=lambda _: True):
85100
try:
86-
result = find_any(strategy, condition)
87-
raise AssertionError(f"Expected no results but found {result!r}")
101+
assert_all_examples(strategy, lambda val: not condition(val))
88102
except (Unsatisfiable, NoSuchExample):
89103
pass
90104

@@ -95,14 +109,21 @@ def assert_all_examples(strategy, predicate, settings=None):
95109
:param strategy: Hypothesis strategy to check
96110
:param predicate: (callable) Predicate that takes example and returns bool
97111
"""
112+
if context := _current_build_context.value:
113+
for _ in range(20):
114+
s = context.data.draw(strategy)
115+
msg = f"Found {s!r} using strategy {strategy} which does not match"
116+
assert predicate(s), msg
117+
118+
else:
98119

99-
@given(strategy)
100-
@Settings(parent=settings, database=None)
101-
def assert_examples(s):
102-
msg = f"Found {s!r} using strategy {strategy} which does not match"
103-
assert predicate(s), msg
120+
@given(strategy)
121+
@Settings(parent=settings, database=None)
122+
def assert_examples(s):
123+
msg = f"Found {s!r} using strategy {strategy} which does not match"
124+
assert predicate(s), msg
104125

105-
assert_examples()
126+
assert_examples()
106127

107128

108129
def assert_simple_property(strategy, predicate, settings=None):

hypothesis-python/tests/cover/test_datetimes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import pytest
1414

15-
from hypothesis import given, settings
15+
from hypothesis import HealthCheck, given, settings
1616
from hypothesis.strategies import dates, datetimes, timedeltas, times
1717

1818
from tests.common.debug import assert_simple_property, find_any, minimal
@@ -49,6 +49,7 @@ def test_max_value_is_respected():
4949
assert minimal(timedeltas(max_value=dt.timedelta(days=-10))).days == -10
5050

5151

52+
@settings(suppress_health_check=list(HealthCheck))
5253
@given(timedeltas())
5354
def test_single_timedelta(val):
5455
assert_simple_property(timedeltas(val, val), lambda v: v is val)

hypothesis-python/tests/cover/test_slices.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import pytest
1212

13-
from hypothesis import given, settings, strategies as st
13+
from hypothesis import HealthCheck, given, settings, strategies as st
1414

1515
from tests.common.debug import assert_all_examples, find_any, minimal
1616

@@ -63,9 +63,9 @@ def test_slices_will_shrink(size):
6363

6464

6565
@given(st.integers(1, 1000))
66-
@settings(deadline=None)
66+
@settings(deadline=None, suppress_health_check=list(HealthCheck))
6767
def test_step_will_be_negative(size):
68-
find_any(st.slices(size), lambda x: (x.step or 1) < 0, settings(max_examples=10**6))
68+
find_any(st.slices(size), lambda x: (x.step or 1) < 0)
6969

7070

7171
@given(st.integers(1, 1000))

hypothesis-python/tests/numpy/test_gen_data.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from hypothesis import (
1919
HealthCheck,
20+
Phase,
2021
assume,
2122
given,
2223
note,
@@ -968,7 +969,7 @@ def test_mutually_broadcastable_shapes_can_generate_arbitrary_ndims(
968969
)
969970

970971

971-
@settings(deadline=None)
972+
@settings(deadline=None, suppress_health_check=list(HealthCheck))
972973
@given(
973974
base_shape=nps.array_shapes(min_dims=0, max_dims=3, min_side=0, max_side=2),
974975
max_dims=st.integers(1, 4),
@@ -1102,10 +1103,10 @@ def index_selects_values_in_order(index):
11021103
target(float(np.sum(target_array == selected)), label="elements correct")
11031104
return np.all(target_array == selected)
11041105

1105-
find_any(
1106+
minimal(
11061107
nps.integer_array_indices(shape, result_shape=st.just(target_array.shape)),
11071108
index_selects_values_in_order,
1108-
settings(max_examples=10**6),
1109+
settings(max_examples=10**6, phases=[Phase.generate, Phase.target]),
11091110
)
11101111

11111112

hypothesis-python/tests/numpy/test_gufunc.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import pytest
1313
from pytest import param
1414

15-
from hypothesis import example, given, note, settings, strategies as st
15+
from hypothesis import HealthCheck, example, given, note, settings, strategies as st
1616
from hypothesis.extra import numpy as nps
1717
from hypothesis.extra._array_helpers import _hypothesis_parse_gufunc_signature
1818

@@ -66,7 +66,7 @@ def test_matmul_gufunc_shapes(everything):
6666
assert out.shape == result_shape
6767

6868

69-
@settings(deadline=None, max_examples=10)
69+
@settings(deadline=None, max_examples=10, suppress_health_check=list(HealthCheck))
7070
@pytest.mark.parametrize(
7171
"target_sig",
7272
("(i),(i)->()", "(m,n),(n,p)->(m,p)", "(n),(n,p)->(p)", "(m,n),(n)->(m)"),
@@ -83,7 +83,6 @@ def test_matmul_signature_can_exercise_all_combination_of_optional_dims(
8383
signature="(m?,n),(n,p?)->(m?,p?)", max_dims=0
8484
),
8585
lambda shapes: shapes == target_shapes,
86-
settings(max_examples=10**6),
8786
)
8887

8988

0 commit comments

Comments
 (0)