Skip to content

Commit 4543005

Browse files
committed
limit the number of elements shown for API use suggestion; #3819
1 parent aa3c272 commit 4543005

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

hypothesis-python/src/hypothesis/core.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,11 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
10251025
# path for test runs that fail.
10261026
if isinstance(e, TypeError) and "SearchStrategy" in str(e):
10271027
try:
1028-
bad_strat_repr = data._sampled_from_strategy_repr # type: ignore[attr-defined]
1028+
suggestion = data._sampled_from_all_strategies_elements_message
10291029
except AttributeError:
10301030
pass
10311031
else:
1032-
add_note(
1033-
e,
1034-
f"sample_from was given a collection of strategies: {bad_strat_repr}. Was one_of intended?",
1035-
)
1032+
add_note(e, suggestion)
10361033
tb = get_trimmed_traceback()
10371034
info = data.extra_information
10381035
info._expected_traceback = format_exception(e, tb) # type: ignore

hypothesis-python/src/hypothesis/strategies/_internal/strategies.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,16 @@ def do_draw(self, data):
531531
if isinstance(result, SearchStrategy) and all(
532532
isinstance(x, SearchStrategy) for x in self.elements
533533
):
534-
data._sampled_from_strategy_repr = repr(self)
534+
max_num_strats = 3
535+
preamble = (
536+
f"(first {max_num_strats}) "
537+
if len(self.elements) > max_num_strats
538+
else ""
539+
)
540+
strat_reprs = ", ".join(
541+
map(get_pretty_function_description, self.elements[:max_num_strats])
542+
)
543+
data._sampled_from_all_strategies_elements_message = f"sample_from was given a collection of strategies: {preamble}{strat_reprs}. Was one_of intended?"
535544
if result is filter_not_satisfied:
536545
data.mark_invalid(f"Aborted test because unable to satisfy {self!r}")
537546
return result

0 commit comments

Comments
 (0)