Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various core and test changes #3835

Merged
merged 21 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch refactors some internals, continuing our work on supporting alternative backends (:issue:`3086`). There is no user-visible change.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Set,
Tuple,
Type,
TypeVar,
Union,
)

Expand Down Expand Up @@ -88,6 +89,8 @@ def wrapper(tp):
]
TargetObservations = Dict[Optional[str], Union[int, float]]

T = TypeVar("T")


class ExtraInformation:
"""A class for holding shared state on a ``ConjectureData`` that should
Expand Down Expand Up @@ -1719,6 +1722,11 @@ def freeze(self) -> None:
self.buffer = bytes(self.buffer)
self.observer.conclude_test(self.status, self.interesting_origin)

def choice(self, values: Sequence[T], *, forced: Optional[T] = None) -> T:
forced_i = None if forced is None else values.index(forced)
i = self.draw_integer(0, len(values) - 1, forced=forced_i)
return values[i]

def draw_bits(self, n: int, *, forced: Optional[int] = None) -> int:
"""Return an ``n``-bit integer from the underlying source of
bytes. If ``forced`` is set to an integer will instead
Expand Down
12 changes: 2 additions & 10 deletions hypothesis-python/src/hypothesis/internal/conjecture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ def check_sample(
return tuple(values)


def choice(
data: "ConjectureData", values: Sequence[T], *, forced: Optional[T] = None
) -> T:
forced_i = None if forced is None else values.index(forced)
i = data.draw_integer(0, len(values) - 1, forced=forced_i)
return values[i]


class Sampler:
"""Sampler based on Vose's algorithm for the alias method. See
http://www.keithschwarz.com/darts-dice-coins/ for a good explanation.
Expand Down Expand Up @@ -182,8 +174,8 @@ def sample(self, data: "ConjectureData", forced: Optional[int] = None) -> int:
if forced is None
else next((b, a, a_c) for (b, a, a_c) in self.table if forced in (b, a))
)
base, alternate, alternate_chance = choice(
data, self.table, forced=forced_choice
base, alternate, alternate_chance = data.choice(
self.table, forced=forced_choice
)
use_alternate = data.draw_boolean(
alternate_chance, forced=None if forced is None else forced == alternate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def do_filtered_draw(self, data):
# The speculative index didn't work out, but at this point we've built
# and can choose from the complete list of allowed indices and elements.
if allowed:
i, element = cu.choice(data, allowed)
i, element = data.choice(allowed)
data.draw_integer(0, len(self.elements) - 1, forced=i)
return element
# If there are no allowed indices, the filter couldn't be satisfied.
Expand Down
19 changes: 11 additions & 8 deletions hypothesis-python/tests/common/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ class Timeout(BaseException):


def minimal(definition, condition=lambda x: True, settings=None, timeout_after=10):
definition.validate()
runtime = None
result = None

def wrapped_condition(x):
nonlocal runtime
if timeout_after is not None:
if runtime:
runtime[0] += TIME_INCREMENT
if runtime[0] >= timeout_after:
runtime += TIME_INCREMENT
if runtime >= timeout_after:
raise Timeout
result = condition(x)
if result and not runtime:
runtime.append(0.0)
runtime = 0.0
return result

if settings is None:
Expand All @@ -51,16 +56,14 @@ def wrapped_condition(x):
)
def inner(x):
if wrapped_condition(x):
result[:] = [x]
nonlocal result
result = x
raise Found

definition.validate()
runtime = []
result = []
try:
inner()
except Found:
return result[0]
return result
raise Unsatisfiable(
"Could not find any examples from %r that satisfied %s"
% (definition, get_pretty_function_description(condition))
Expand Down
Loading
Loading