Skip to content

Commit ae1a2d0

Browse files
committed
Clean up minimal() helper
1 parent e8cea04 commit ae1a2d0

File tree

7 files changed

+13
-42
lines changed

7 files changed

+13
-42
lines changed

hypothesis-python/tests/array_api/test_arrays.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ def test_minimize_large_uint_arrays(xp, xps):
187187
example."""
188188
if not hasattr(xp, "nonzero"):
189189
pytest.skip("optional API")
190-
smallest = minimal(
191-
xps.arrays(xp.uint8, 100),
192-
lambda x: xp.any(x) and not xp.all(x),
193-
timeout_after=60,
194-
)
190+
smallest = minimal(xps.arrays(xp.uint8, 100), lambda x: xp.any(x) and not xp.all(x))
195191
assert xp.all(xp.logical_or(smallest == 0, smallest == 1))
196192
idx = xp.nonzero(smallest)[0]
197193
assert idx.size in (1, smallest.size - 1)

hypothesis-python/tests/common/debug.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,21 @@ class Timeout(BaseException):
2525
pass
2626

2727

28-
def minimal(definition, condition=lambda x: True, settings=None, timeout_after=10):
28+
def minimal(definition, condition=lambda x: True, settings=None):
2929
definition.validate()
30-
runtime = None
3130
result = None
3231

32+
def wrapped_condition(x):
33+
# This sure seems pointless, but `test_sum_of_pair` fails otherwise...
34+
return condition(x)
35+
3336
if (
3437
context := _current_build_context.value
3538
) and context.data.provider.avoid_realization:
3639
raise SkipTest("`minimal()` helper not supported under symbolic execution")
3740

38-
def wrapped_condition(x):
39-
nonlocal runtime
40-
if timeout_after is not None:
41-
if runtime:
42-
runtime += TIME_INCREMENT
43-
if runtime >= timeout_after:
44-
raise Timeout
45-
result = condition(x)
46-
if result and not runtime:
47-
runtime = 0.0
48-
return result
49-
5041
if settings is None:
51-
settings = Settings(max_examples=50000, phases=(Phase.generate, Phase.shrink))
42+
settings = Settings(max_examples=500, phases=(Phase.generate, Phase.shrink))
5243

5344
verbosity = settings.verbosity
5445
if verbosity == Verbosity.normal:

hypothesis-python/tests/cover/test_datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_bordering_on_a_leap_year():
7373
dt.datetime.min.replace(year=2003), dt.datetime.max.replace(year=2005)
7474
),
7575
lambda x: x.month == 2 and x.day == 29,
76-
timeout_after=60,
76+
settings=settings(max_examples=1200),
7777
)
7878
assert x.year == 2004
7979

hypothesis-python/tests/nocover/test_recursive.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def flatten(x):
3434
max_leaves=size * 2,
3535
),
3636
lambda x: isinstance(x, list) and len(flatten(x)) >= size,
37-
timeout_after=None,
3837
)
3938
assert flatten(xs) == [0] * size
4039

@@ -46,11 +45,7 @@ def depth(x):
4645
else:
4746
return 1
4847

49-
xs = minimal(
50-
st.recursive(st.integers(), st.lists),
51-
lambda x: depth(x) > 1,
52-
timeout_after=None,
53-
)
48+
xs = minimal(st.recursive(st.integers(), st.lists), lambda x: depth(x) > 1)
5449
assert xs in ([0], [[]])
5550

5651

@@ -67,7 +62,6 @@ def breadth(x):
6762
st.recursive(st.booleans(), lambda x: st.lists(x, max_size=target // 2)),
6863
lambda x: breadth(x) >= target,
6964
settings=settings(max_examples=10000),
70-
timeout_after=None,
7165
)
7266
assert breadth(broad) == target
7367

@@ -79,7 +73,7 @@ def test_drawing_many_near_boundary():
7973
lambda x: st.lists(x, min_size=2 * (size - 1), max_size=2 * size).map(tuple),
8074
max_leaves=2 * size - 1,
8175
)
82-
ls = minimal(st.lists(elems), lambda x: len(set(x)) >= size, timeout_after=None)
76+
ls = minimal(st.lists(elems), lambda x: len(set(x)) >= size)
8377
assert len(ls) == size
8478

8579

@@ -117,7 +111,7 @@ def test_can_form_sets_of_recursive_data():
117111
max_leaves=20,
118112
)
119113
)
120-
xs = minimal(trees, lambda x: len(x) >= size, timeout_after=None)
114+
xs = minimal(trees, lambda x: len(x) >= size)
121115
assert len(xs) == size
122116

123117

hypothesis-python/tests/nocover/test_simple_numbers.py

-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def test_list_of_fractional_float():
145145
minimal(
146146
lists(floats(), min_size=5),
147147
lambda x: len([t for t in x if t >= 1.5]) >= 5,
148-
timeout_after=60,
149148
)
150149
) == {2}
151150

@@ -158,7 +157,6 @@ def test_minimizes_lists_of_negative_ints_up_to_boundary():
158157
result = minimal(
159158
lists(integers(), min_size=10),
160159
lambda x: len([t for t in x if t <= -1]) >= 10,
161-
timeout_after=60,
162160
)
163161
assert result == [-1] * 10
164162

hypothesis-python/tests/numpy/test_gen_data.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ def test_generates_and_minimizes():
6262

6363

6464
def test_can_minimize_large_arrays():
65-
x = minimal(
66-
nps.arrays("uint32", 100),
67-
lambda x: np.any(x) and not np.all(x),
68-
timeout_after=60,
69-
)
65+
x = minimal(nps.arrays("uint32", 100), lambda x: np.any(x) and not np.all(x))
7066
assert np.logical_or(x == 0, x == 1).all()
7167
assert np.count_nonzero(x) in (1, len(x) - 1)
7268

hypothesis-python/tests/quality/test_shrink_quality.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_minimize_multiple_elements_in_silly_large_int_range():
185185
desired_result = [0] * 20
186186

187187
ir = integers(-(2**256), 2**256)
188-
x = minimal(lists(ir), lambda x: len(x) >= 20, timeout_after=20)
188+
x = minimal(lists(ir), lambda x: len(x) >= 20, settings(max_examples=10_000))
189189
assert x == desired_result
190190

191191

@@ -196,7 +196,6 @@ def test_minimize_multiple_elements_in_silly_large_int_range_min_is_not_dupe():
196196
x = minimal(
197197
lists(ir),
198198
lambda x: (assume(len(x) >= 20) and all(x[i] >= target[i] for i in target)),
199-
timeout_after=60,
200199
)
201200
assert x == target
202201

@@ -211,7 +210,6 @@ def large_mostly_non_overlapping(xs):
211210
result = minimal(
212211
lists(sets(integers(), min_size=1), min_size=1),
213212
large_mostly_non_overlapping,
214-
timeout_after=120,
215213
)
216214
assert len(result) == 1
217215
union = reduce(set.union, result)
@@ -227,7 +225,6 @@ def test_containment(n, seed):
227225
iv = minimal(
228226
tuples(lists(integers()), integers()),
229227
lambda x: x[1] in x[0] and x[1] >= n,
230-
timeout_after=60,
231228
)
232229
assert iv == ([n], n)
233230

@@ -236,7 +233,6 @@ def test_duplicate_containment():
236233
ls, i = minimal(
237234
tuples(lists(integers()), integers()),
238235
lambda s: s[0].count(s[1]) > 1,
239-
timeout_after=100,
240236
)
241237
assert ls == [0, 0]
242238
assert i == 0

0 commit comments

Comments
 (0)