Skip to content

Commit

Permalink
Merge pull request #3539 from Zac-HD/pprint-cleanup
Browse files Browse the repository at this point in the history
Refactor pretty-printer to unlock future upgrades
  • Loading branch information
Zac-HD authored Jan 7, 2023
2 parents 7af0701 + b4bbe46 commit cb084fe
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 365 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch improves our pretty-printing of failing examples, including
some refactoring to prepare for exciting future features.
4 changes: 2 additions & 2 deletions hypothesis-python/src/hypothesis/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import math
from typing import NoReturn, Union
from typing import NoReturn, Optional, Union

from hypothesis import Verbosity, settings
from hypothesis.errors import InvalidArgument, UnsatisfiedAssumption
Expand Down Expand Up @@ -51,7 +51,7 @@ def currently_in_test_context() -> bool:
return _current_build_context.value is not None


def current_build_context():
def current_build_context() -> "Optional[BuildContext]":
context = _current_build_context.value
if context is None:
raise InvalidArgument("No build context registered")
Expand Down
33 changes: 9 additions & 24 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def is_invalid_test(test, original_sig, given_arguments, given_kwargs):

if ... in given_arguments:
return invalid(
"... was passed as a positional argument to @given, but may only be "
"... was passed as a positional argument to @given, but may only be "
"passed as a keyword argument or as the sole argument of @given"
)

Expand Down Expand Up @@ -717,28 +717,13 @@ def run(data):

if self.print_given_args:
printer.text(" ")
printer.text(test.__name__)
with printer.group(indent=4, open="(", close=""):
printer.break_()
for v in args:
printer.pretty(v)
# We add a comma unconditionally because
# generated arguments will always be kwargs,
# so there will always be more to come.
printer.text(",")
printer.breakable()

for i, (k, v) in enumerate(kwargs.items()):
printer.text(k)
printer.text("=")
printer.pretty(v)
printer.text(",")
if i + 1 < len(kwargs):
printer.breakable()
printer.break_()
printer.text(")")
printer.flush()
report(output.getvalue())
printer.repr_call(
test.__name__,
args,
kwargs,
force_split=True,
)
report(printer.getvalue())
return test(*args, **kwargs)

# Run the test function once, via the executor hook.
Expand Down Expand Up @@ -1119,7 +1104,7 @@ def run_test_as_given(test):
for p in original_sig.parameters.values()
if p.kind is p.POSITIONAL_OR_KEYWORD
]
given_kwargs = dict(zip(posargs[::-1], given_arguments[::-1]))
given_kwargs = dict(list(zip(posargs[::-1], given_arguments[::-1]))[::-1])
# These have been converted, so delete them to prevent accidental use.
del given_arguments

Expand Down
1 change: 1 addition & 0 deletions hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ def nicerepr(v):


def repr_call(f, args, kwargs, reorder=True):
# Note: for multi-line pretty-printing, see RepresentationPrinter.repr_call()
if reorder:
args, kwargs = convert_positional_arguments(f, args, kwargs)

Expand Down
10 changes: 6 additions & 4 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
)
from hypothesis.strategies._internal.utils import cacheable, defines_strategy
from hypothesis.utils.conventions import not_set
from hypothesis.vendor.pretty import RepresentationPrinter

if sys.version_info >= (3, 10):
from types import EllipsisType as EllipsisType
Expand Down Expand Up @@ -1834,10 +1835,11 @@ def draw(self, strategy: SearchStrategy[Ex], label: Any = None) -> Ex:
check_strategy(strategy, "strategy")
result = self.conjecture_data.draw(strategy)
self.count += 1
if label is not None:
note(f"Draw {self.count} ({label}): {result!r}")
else:
note(f"Draw {self.count}: {result!r}")
printer = RepresentationPrinter()
printer.text(f"Draw {self.count}")
printer.text(": " if label is None else f" ({label}): ")
printer.pretty(result)
note(printer.getvalue())
return result


Expand Down
Loading

0 comments on commit cb084fe

Please sign in to comment.