Skip to content

Commit a322908

Browse files
authored
Merge pull request #4056 from Zac-HD/misc-cleanup-again
Various small cleanups to reporting, test settings, and a few flakes
2 parents d76d74f + b8a1e1f commit a322908

15 files changed

Lines changed: 50 additions & 58 deletions

‎hypothesis-python/RELEASE.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
Minor internal-only cleanups to some error-handling and reporting code.

‎hypothesis-python/setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def local_file(name):
6060
"pytest": ["pytest>=4.6"],
6161
"dpcontracts": ["dpcontracts>=0.4"],
6262
"redis": ["redis>=3.0.0"],
63-
"crosshair": ["hypothesis-crosshair>=0.0.8", "crosshair-tool>=0.0.62"],
63+
"crosshair": ["hypothesis-crosshair>=0.0.9", "crosshair-tool>=0.0.63"],
6464
# zoneinfo is an odd one: every dependency is conditional, because they're
6565
# only necessary on old versions of Python or Windows systems or emscripten.
6666
"zoneinfo": [

‎hypothesis-python/src/_hypothesis_pytestplugin.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def note_statistics(stats):
309309
with current_pytest_item.with_value(item):
310310
yield
311311
if store.results:
312-
item.hypothesis_report_information = list(store.results)
312+
item.hypothesis_report_information = "\n".join(store.results)
313313

314314
def _stash_get(config, key, default):
315315
if hasattr(config, "stash"):
@@ -325,9 +325,7 @@ def _stash_get(config, key, default):
325325
def pytest_runtest_makereport(item, call):
326326
report = (yield).get_result()
327327
if hasattr(item, "hypothesis_report_information"):
328-
report.sections.append(
329-
("Hypothesis", "\n".join(item.hypothesis_report_information))
330-
)
328+
report.sections.append(("Hypothesis", item.hypothesis_report_information))
331329
if report.when != "teardown":
332330
return
333331

‎hypothesis-python/src/hypothesis/reporting.py‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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-
import inspect
12-
1311
from hypothesis._settings import Verbosity, settings
1412
from hypothesis.internal.compat import escape_unicode_characters
1513
from hypothesis.utils.dynamicvariables import DynamicVariable
@@ -37,14 +35,6 @@ def current_verbosity():
3735
return settings.default.verbosity
3836

3937

40-
def to_text(textish):
41-
if inspect.isfunction(textish):
42-
textish = textish()
43-
if isinstance(textish, bytes):
44-
textish = textish.decode()
45-
return textish
46-
47-
4838
def verbose_report(text):
4939
if current_verbosity() >= Verbosity.verbose:
5040
base_report(text)
@@ -61,4 +51,5 @@ def report(text):
6151

6252

6353
def base_report(text):
64-
current_reporter()(to_text(text))
54+
assert isinstance(text, str), f"unexpected non-str {text=}"
55+
current_reporter()(text)

‎hypothesis-python/tests/common/debug.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from unittest import SkipTest
1212

1313
from hypothesis import HealthCheck, Phase, Verbosity, given, settings as Settings
14+
from hypothesis._settings import local_settings
1415
from hypothesis.control import _current_build_context
1516
from hypothesis.errors import Found, NoSuchExample, Unsatisfiable
1617
from hypothesis.internal.reflection import get_pretty_function_description
@@ -110,10 +111,11 @@ def assert_all_examples(strategy, predicate, settings=None):
110111
:param predicate: (callable) Predicate that takes example and returns bool
111112
"""
112113
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
114+
with local_settings(Settings(parent=settings)):
115+
for _ in range(20):
116+
s = context.data.draw(strategy)
117+
msg = f"Found {s!r} using strategy {strategy} which does not match"
118+
assert predicate(s), msg
117119

118120
else:
119121

‎hypothesis-python/tests/common/setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run():
6767
backend="crosshair",
6868
max_examples=20,
6969
deadline=None,
70-
suppress_health_check=list(HealthCheck),
70+
suppress_health_check=(HealthCheck.too_slow, HealthCheck.filter_too_much),
7171
report_multiple_bugs=False,
7272
)
7373

‎hypothesis-python/tests/cover/test_randoms.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def test(rnd):
294294

295295

296296
def test_triangular_modes():
297+
@settings(report_multiple_bugs=True)
297298
@given(st.randoms(use_true_random=False))
298299
def test(rnd):
299300
x = rnd.triangular(0.0, 1.0, mode=0.5)

‎hypothesis-python/tests/cover/test_regex.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import pytest
1616

1717
from hypothesis import HealthCheck, assume, given, settings, strategies as st
18-
from hypothesis._settings import local_settings
1918
from hypothesis.errors import InvalidArgument
2019
from hypothesis.internal.compat import PYPY
2120
from hypothesis.strategies._internal.regex import (
@@ -144,11 +143,11 @@ def test_can_generate(pattern, encode):
144143
alphabet = st.characters(max_codepoint=1000) if encode is None else None
145144
if encode:
146145
pattern = pattern.encode("ascii")
147-
with local_settings(settings(suppress_health_check=[HealthCheck.data_too_large])):
148-
assert_all_examples(
149-
st.from_regex(pattern, alphabet=alphabet),
150-
re.compile(pattern).search,
151-
)
146+
assert_all_examples(
147+
st.from_regex(pattern, alphabet=alphabet),
148+
re.compile(pattern).search,
149+
settings=settings(suppress_health_check=[HealthCheck.data_too_large]),
150+
)
152151

153152

154153
@pytest.mark.parametrize(

‎hypothesis-python/tests/cover/test_reporting.py‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515

1616
from hypothesis import given, reporting
1717
from hypothesis._settings import Verbosity, settings
18-
from hypothesis.reporting import debug_report, report, verbose_report
18+
from hypothesis.reporting import debug_report, verbose_report
1919
from hypothesis.strategies import integers
2020

2121
from tests.common.utils import capture_out
2222

2323

24-
def test_can_print_bytes():
25-
with capture_out() as o:
26-
with reporting.with_reporter(reporting.default):
27-
report(b"hi")
28-
assert o.getvalue() == "hi\n"
29-
30-
3124
def test_prints_output_by_default():
3225
@given(integers())
3326
def test_int(x):
@@ -77,10 +70,3 @@ def test_can_report_when_system_locale_is_ascii(monkeypatch):
7770
with open(write, "w", encoding="ascii") as write:
7871
monkeypatch.setattr(sys, "stdout", write)
7972
reporting.default("☃")
80-
81-
82-
def test_can_report_functions():
83-
with capture_out() as out:
84-
report(lambda: "foo")
85-
86-
assert out.getvalue().strip() == "foo"

‎hypothesis-python/tests/cover/test_sampled_from.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pytest
1616

17-
from hypothesis import given, strategies as st
17+
from hypothesis import given, settings, strategies as st
1818
from hypothesis.errors import (
1919
FailedHealthCheck,
2020
InvalidArgument,
@@ -63,12 +63,14 @@ def test_can_sample_enums(enum_class):
6363

6464

6565
@fails_with(FailedHealthCheck)
66+
@settings(suppress_health_check=[])
6667
@given(sampled_from(range(10)).filter(lambda x: x < 0))
6768
def test_unsat_filtered_sampling(x):
6869
raise AssertionError
6970

7071

7172
@fails_with(Unsatisfiable)
73+
@settings(suppress_health_check=[])
7274
@given(sampled_from(range(2)).filter(lambda x: x < 0))
7375
def test_unsat_filtered_sampling_in_rejection_stage(x):
7476
# Rejecting all possible indices before we calculate the allowed indices

0 commit comments

Comments
 (0)