From 9a8422c3752ddc3c059904160867399bacdcd8cb Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sat, 23 Mar 2024 21:37:58 -0700 Subject: [PATCH] Observe backend in how_generated --- hypothesis-python/RELEASE.rst | 4 ++++ hypothesis-python/src/hypothesis/core.py | 19 +++++++++++-------- .../src/hypothesis/internal/observability.py | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 hypothesis-python/RELEASE.rst diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..9cb6875cbe --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,4 @@ +RELEASE_TYPE: patch + +This patch includes the :obj:`~hypothesis.settings.backend` setting in the +``how_generated`` field of our :doc:`observability output `. diff --git a/hypothesis-python/src/hypothesis/core.py b/hypothesis-python/src/hypothesis/core.py index 402382c6aa..ccd5c43b6e 100644 --- a/hypothesis-python/src/hypothesis/core.py +++ b/hypothesis-python/src/hypothesis/core.py @@ -786,7 +786,6 @@ def __init__(self, stuff, test, settings, random, wrapped_test): self.explain_traces = defaultdict(set) self._start_timestamp = time.time() self._string_repr = "" - self._jsonable_arguments = {} self._timing_features = {} @property @@ -913,7 +912,7 @@ def run(data): ), ) self._string_repr = printer.getvalue() - self._jsonable_arguments = { + data._observability_arguments = { **dict(enumerate(map(to_jsonable, args))), **{k: to_jsonable(v) for k, v in kwargs.items()}, } @@ -1085,19 +1084,23 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None: # Conditional here so we can save some time constructing the payload; in # other cases (without coverage) it's cheap enough to do that regardless. if TESTCASE_CALLBACKS: - if self.failed_normally or self.failed_due_to_deadline: - phase = "shrink" - elif runner := getattr(self, "_runner", None): + if runner := getattr(self, "_runner", None): phase = runner._current_phase + elif self.failed_normally or self.failed_due_to_deadline: + phase = "shrink" else: # pragma: no cover # in case of messing with internals phase = "unknown" + backend_desc = f", using backend={self.settings.backend!r}" * ( + self.settings.backend != "hypothesis" + and not getattr(runner, "_switch_to_hypothesis_provider", False) + ) tc = make_testcase( start_timestamp=self._start_timestamp, test_name_or_nodeid=self.test_identifier, data=data, - how_generated=f"generated during {phase} phase", + how_generated=f"during {phase} phase{backend_desc}", string_repr=self._string_repr, - arguments={**self._jsonable_arguments, **data._observability_args}, + arguments=data._observability_args, timing=self._timing_features, coverage=tractable_coverage_report(trace) or None, phase=phase, @@ -1217,7 +1220,7 @@ def run_engine(self): "status": "passed" if sys.exc_info()[0] else "failed", "status_reason": str(origin or "unexpected/flaky pass"), "representation": self._string_repr, - "arguments": self._jsonable_arguments, + "arguments": ran_example._observability_args, "how_generated": "minimal failing example", "features": { **{ diff --git a/hypothesis-python/src/hypothesis/internal/observability.py b/hypothesis-python/src/hypothesis/internal/observability.py index aff19d805c..a532d054cd 100644 --- a/hypothesis-python/src/hypothesis/internal/observability.py +++ b/hypothesis-python/src/hypothesis/internal/observability.py @@ -36,7 +36,7 @@ def make_testcase( start_timestamp: float, test_name_or_nodeid: str, data: ConjectureData, - how_generated: str = "unknown", + how_generated: str, string_repr: str = "", arguments: Optional[dict] = None, timing: Dict[str, float],