Skip to content

Commit e1305ad

Browse files
authored
Merge pull request #3189 from Zac-HD/representative-examples
Representative examples
2 parents 6173b2a + 1eeaac6 commit e1305ad

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch makes the ``.example()`` method more representative of
4+
test-time data generation, albeit often at a substantial cost to
5+
readability (:issue:`3182`).

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
import warnings
1818
from collections import defaultdict
19-
from random import choice as random_choice
19+
from random import shuffle
2020
from typing import (
2121
Any,
2222
Callable,
@@ -315,25 +315,30 @@ def example(self) -> Ex:
315315
"#drawing-interactively-in-tests for more details."
316316
)
317317

318+
try:
319+
return self.__examples.pop()
320+
except (AttributeError, IndexError):
321+
self.__examples: List[Ex] = []
322+
318323
from hypothesis.core import given
319324

320325
# Note: this function has a weird name because it might appear in
321326
# tracebacks, and we want users to know that they can ignore it.
322327
@given(self)
323328
@settings(
324329
database=None,
325-
max_examples=10,
330+
max_examples=100,
326331
deadline=None,
327332
verbosity=Verbosity.quiet,
328333
phases=(Phase.generate,),
329334
suppress_health_check=HealthCheck.all(),
330335
)
331336
def example_generating_inner_function(ex):
332-
examples.append(ex)
337+
self.__examples.append(ex)
333338

334-
examples: List[Ex] = []
335339
example_generating_inner_function()
336-
return random_choice(examples)
340+
shuffle(self.__examples)
341+
return self.__examples.pop()
337342

338343
def map(self, pack: Callable[[Ex], T]) -> "SearchStrategy[T]":
339344
"""Returns a new strategy that generates values by generating a value

0 commit comments

Comments
 (0)