Skip to content

Commit 82408d9

Browse files
committed
tweak formatting + move note
1 parent b61590e commit 82408d9

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

hypothesis-python/RELEASE.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
RELEASE_TYPE: patch
22

3-
This patch causes a [note to be included](https://peps.python.org/pep-0678/) when :func:`~hypothesis.strategies.sampled_from` is given a nonempty collection of all strategy values _and_ the `given`-decorated test fails with a `TypeError` (:issue:`3819``).
4-
This is because such a call is suggestive of intent to instead use :func:`~hypothesis.strategies.one_of`.
3+
If a test uses :func:`~hypothesis.strategies.sampled_from` on a sequence of
4+
strategies, and raises a ``TypeError``, we now :pep:`add a note <678>` asking
5+
whether you meant to use :func:`~hypothesis.strategies.one_of`.
6+
7+
Thanks to Vince Reuter for suggesting and implementing this hint!

hypothesis-python/src/hypothesis/core.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,18 @@ def run(data):
917917
**dict(enumerate(map(to_jsonable, args))),
918918
**{k: to_jsonable(v) for k, v in kwargs.items()},
919919
}
920-
return test(*args, **kwargs)
920+
921+
try:
922+
return test(*args, **kwargs)
923+
except TypeError as e:
924+
# If we sampled from a sequence of strategies, AND failed with a
925+
# TypeError, *AND that exception mentions SearchStrategy*, add a note:
926+
if "SearchStrategy" in str(e):
927+
try:
928+
add_note(e, data._sampled_from_all_strategies_elements_message) # type: ignore
929+
except AttributeError:
930+
pass
931+
raise
921932

922933
# self.test_runner can include the execute_example method, or setup/teardown
923934
# _example, so it's important to get the PRNG and build context in place first.
@@ -1024,13 +1035,6 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
10241035
# The test failed by raising an exception, so we inform the
10251036
# engine that this test run was interesting. This is the normal
10261037
# path for test runs that fail.
1027-
if isinstance(e, TypeError) and "SearchStrategy" in str(e):
1028-
try:
1029-
suggestion = data._sampled_from_all_strategies_elements_message # type: ignore[attr-defined]
1030-
except AttributeError:
1031-
pass
1032-
else:
1033-
add_note(e, suggestion)
10341038
tb = get_trimmed_traceback()
10351039
info = data.extra_information
10361040
info._expected_traceback = format_exception(e, tb) # type: ignore
@@ -1161,12 +1165,6 @@ def run_engine(self):
11611165
err.__cause__ = err.__context__ = e
11621166
errors_to_report.append((fragments, err))
11631167
except BaseException as e:
1164-
try:
1165-
notes = info._expected_exception.__notes__
1166-
except AttributeError:
1167-
pass
1168-
else:
1169-
e.__notes__ = notes
11701168
# If we have anything for explain-mode, this is the time to report.
11711169
fragments.extend(explanations[falsifying_example.interesting_origin])
11721170
errors_to_report.append(
@@ -1240,7 +1238,6 @@ def _raise_to_user(errors_to_report, settings, target_lines, trailer=""):
12401238
if settings.verbosity >= Verbosity.normal:
12411239
for line in target_lines:
12421240
add_note(the_error_hypothesis_found, line)
1243-
12441241
raise the_error_hypothesis_found
12451242

12461243

0 commit comments

Comments
 (0)