Skip to content

Commit

Permalink
Make executors run in buildcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Sep 24, 2023
1 parent 9f3c159 commit bb00b5e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
114 changes: 58 additions & 56 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,63 +780,65 @@ def test(*args, **kwargs):

def run(data):
# Set up dynamic context needed by a single test run.
with local_settings(self.settings):
with deterministic_PRNG():
with BuildContext(data, is_final=is_final) as context:
if self.stuff.selfy is not None:
data.hypothesis_runner = self.stuff.selfy
# Generate all arguments to the test function.
args = self.stuff.args
kwargs = dict(self.stuff.kwargs)
if example_kwargs is None:
a, kw, argslices = context.prep_args_kwargs_from_strategies(
(), self.stuff.given_kwargs
)
assert not a, "strategies all moved to kwargs by now"
else:
kw = example_kwargs
argslices = {}
kwargs.update(kw)
if expected_failure is not None:
nonlocal text_repr
text_repr = repr_call(test, args, kwargs)
if text_repr in self.xfail_example_reprs:
warnings.warn(
f"We generated {text_repr}, which seems identical "
"to one of your `@example(...).xfail()` cases. "
"Revise the strategy to avoid this overlap?",
HypothesisWarning,
# Checked in test_generating_xfailed_examples_warns!
stacklevel=6,
)

if print_example or current_verbosity() >= Verbosity.verbose:
printer = RepresentationPrinter(context=context)
if print_example:
printer.text("Falsifying example:")
else:
printer.text("Trying example:")

if self.print_given_args:
printer.text(" ")
printer.repr_call(
test.__name__,
args,
kwargs,
force_split=True,
arg_slices=argslices,
leading_comment=(
"# " + context.data.slice_comments[(0, 0)]
if (0, 0) in context.data.slice_comments
else None
),
)
report(printer.getvalue())
return test(*args, **kwargs)
if self.stuff.selfy is not None:
data.hypothesis_runner = self.stuff.selfy
# Generate all arguments to the test function.
args = self.stuff.args
kwargs = dict(self.stuff.kwargs)
if example_kwargs is None:
a, kw, argslices = context.prep_args_kwargs_from_strategies(
(), self.stuff.given_kwargs
)
assert not a, "strategies all moved to kwargs by now"
else:
kw = example_kwargs
argslices = {}
kwargs.update(kw)
if expected_failure is not None:
nonlocal text_repr
text_repr = repr_call(test, args, kwargs)
if text_repr in self.xfail_example_reprs:
warnings.warn(
f"We generated {text_repr}, which seems identical "
"to one of your `@example(...).xfail()` cases. "
"Revise the strategy to avoid this overlap?",
HypothesisWarning,
# Checked in test_generating_xfailed_examples_warns!
stacklevel=6,
)

# Run the test function once, via the executor hook.
# In most cases this will delegate straight to `run(data)`.
result = self.test_runner(data, run)
if print_example or current_verbosity() >= Verbosity.verbose:
printer = RepresentationPrinter(context=context)
if print_example:
printer.text("Falsifying example:")
else:
printer.text("Trying example:")

if self.print_given_args:
printer.text(" ")
printer.repr_call(
test.__name__,
args,
kwargs,
force_split=True,
arg_slices=argslices,
leading_comment=(
"# " + context.data.slice_comments[(0, 0)]
if (0, 0) in context.data.slice_comments
else None
),
)
report(printer.getvalue())
return test(*args, **kwargs)

# self.test_runner can include the execute_example method, or setup/teardown
# _example, so it's important to get the PRNG and build context in place first.
with local_settings(self.settings):
with deterministic_PRNG():
with BuildContext(data, is_final=is_final) as context:
# Run the test function once, via the executor hook.
# In most cases this will delegate straight to `run(data)`.
result = self.test_runner(data, run)

# If a failure was expected, it should have been raised already, so
# instead raise an appropriate diagnostic error.
Expand Down
4 changes: 3 additions & 1 deletion hypothesis-python/tests/cover/test_filtered_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import hypothesis.strategies as st
from hypothesis.control import BuildContext
from hypothesis.internal.conjecture.data import ConjectureData
from hypothesis.strategies._internal.strategies import FilteredStrategy

Expand All @@ -19,7 +20,8 @@ def test_filter_iterations_are_marked_as_discarded():

data = ConjectureData.for_buffer([0, 2, 1, 0])

assert data.draw(x) == 0
with BuildContext(data):
assert data.draw(x) == 0

assert data.has_discards

Expand Down

0 comments on commit bb00b5e

Please sign in to comment.