Skip to content

Commit 1f6ae21

Browse files
committed
Disallow .example(), add replacements
1 parent 17e23f2 commit 1f6ae21

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

hypothesis-python/tests/common/debug.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,46 @@ def assert_no_examples(strategy, condition=lambda _: True):
8989
pass
9090

9191

92-
def assert_all_examples(strategy, predicate):
92+
def assert_all_examples(strategy, predicate, settings=None):
9393
"""Asserts that all examples of the given strategy match the predicate.
9494
9595
:param strategy: Hypothesis strategy to check
9696
:param predicate: (callable) Predicate that takes example and returns bool
9797
"""
9898

9999
@given(strategy)
100+
@Settings(parent=settings)
100101
def assert_examples(s):
101102
msg = f"Found {s!r} using strategy {strategy} which does not match"
102103
assert predicate(s), msg
103104

104105
assert_examples()
106+
107+
108+
def assert_simple_property(strategy, predicate, settings=None):
109+
"""Like assert_all_examples, intended as a self-documenting shortcut for simple constant
110+
properties (`is`, `isinstance`, `==`, ...) that can be adequately verified in just a few
111+
examples.
112+
113+
For more thorough checking, use assert_all_examples.
114+
"""
115+
116+
assert_all_examples(strategy, predicate, Settings(parent=settings, max_examples=15))
117+
118+
119+
def check_can_generate_examples(strategy, settings=None):
120+
"""Tries to generate a small number of examples from the strategy, to verify that it can
121+
do so without raising.
122+
123+
Nothing is returned, it only checks that no error is raised.
124+
"""
125+
126+
assert_simple_property(
127+
strategy,
128+
lambda _: True,
129+
settings=Settings(
130+
parent=settings,
131+
phases=(Phase.generate,),
132+
suppress_health_check=list(HealthCheck),
133+
),
134+
)

hypothesis-python/tests/common/setup.py

-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from hypothesis import Phase, Verbosity, settings
1515
from hypothesis._settings import not_set
16-
from hypothesis.errors import NonInteractiveExampleWarning
1716
from hypothesis.internal.coverage import IN_COVERAGE_TESTS
1817

1918

@@ -36,9 +35,6 @@ def run():
3635
category=UserWarning,
3736
)
3837

39-
# User-facing warning which does not apply to our own tests
40-
filterwarnings("ignore", category=NonInteractiveExampleWarning)
41-
4238
# We do a smoke test here before we mess around with settings.
4339
x = settings()
4440

pytest.ini

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ addopts =
1212
xfail_strict = True
1313
filterwarnings =
1414
error
15-
ignore::hypothesis.errors.NonInteractiveExampleWarning
1615
# https://github.com/pandas-dev/pandas/issues/41199
1716
default:Creating a LegacyVersion has been deprecated and will be removed in the next major release:DeprecationWarning
1817
default:distutils Version classes are deprecated\. Use packaging\.version instead:DeprecationWarning

0 commit comments

Comments
 (0)