Skip to content

Commit 0b98d07

Browse files
committed
check PrimitiveProvider.add_observation_callback instead
1 parent a92991f commit 0b98d07

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

hypothesis-python/docs/prolog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
.. |PrimitiveProvider.draw_bytes| replace:: :func:`~hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_bytes`
118118
.. |PrimitiveProvider.on_observation| replace:: :func:`~hypothesis.internal.conjecture.providers.PrimitiveProvider.on_observation`
119119
.. |PrimitiveProvider.per_test_case_context_manager| replace:: :func:`~hypothesis.internal.conjecture.providers.PrimitiveProvider.per_test_case_context_manager`
120+
.. |PrimitiveProvider.add_observation_callback| replace:: :data:`~hypothesis.internal.conjecture.providers.PrimitiveProvider.add_observation_callback`
120121

121122
.. |AVAILABLE_PROVIDERS| replace:: :data:`~hypothesis.internal.conjecture.providers.AVAILABLE_PROVIDERS`
122123
.. |TESTCASE_CALLBACKS| replace:: :data:`~hypothesis.internal.observability.TESTCASE_CALLBACKS`

hypothesis-python/src/hypothesis/internal/conjecture/engine.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,8 @@ def on_observation(observation: Observation) -> None:
848848
# only for lifetime = "test_function" providers (guaranteed
849849
# by this isinstance check)
850850
and isinstance(self.provider, PrimitiveProvider)
851-
# and the provider class overrode the default
852-
# (see https://github.com/python/mypy/issues/14123 for type ignore)
853-
and type(self.provider).on_observation
854-
is not PrimitiveProvider.on_observation
851+
# and the provider opted-in to observations
852+
and self.provider.add_observation_callback
855853
)
856854
else nullcontext()
857855
)

hypothesis-python/src/hypothesis/internal/conjecture/providers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import (
2222
TYPE_CHECKING,
2323
Any,
24+
ClassVar,
2425
Literal,
2526
Optional,
2627
TypedDict,
@@ -356,6 +357,15 @@ class PrimitiveProvider(abc.ABC):
356357
#: Only set this to ``True`` if it is necessary for your backend.
357358
avoid_realization = False
358359

360+
#: If ``True``, |PrimitiveProvider.on_observation| will be added as a
361+
#: callback to |TESTCASE_CALLBACKS|, enabling observability during the lifetime
362+
#: of this provider. If ``False``, |PrimitiveProvider.on_observation| will
363+
#: never be called by Hypothesis.
364+
#:
365+
#: The opt-in behavior of observability is because enabling observability
366+
#: might increase runtime or memory usage.
367+
add_observation_callback: ClassVar[bool] = False
368+
359369
def __init__(self, conjecturedata: Optional["ConjectureData"], /) -> None:
360370
self._cd = conjecturedata
361371

@@ -551,6 +561,14 @@ def on_observation(self, observation: Observation) -> None: # noqa: B027
551561
other callbacks in |TESTCASE_CALLBACKS|. This method is not called with
552562
``observation["type"] in {"info", "alert", "error"}`` observations.
553563
564+
.. important::
565+
566+
For |PrimitiveProvider.on_observation| to be called by Hypothesis,
567+
|PrimitiveProvider.add_observation_callback| must be set to ``True``,
568+
569+
|PrimitiveProvider.on_observation| is explicitly opt-in, as enabling
570+
observability might increase runtime or memory usage.
571+
554572
Calls to this method are guaranteed to alternate with calls to
555573
|PrimitiveProvider.per_test_case_context_manager|. For example:
556574

hypothesis-python/tests/conjecture/test_alt_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ def test_replay_choices():
722722

723723

724724
class ObservationProvider(TrivialProvider):
725+
add_observation_callback = True
726+
725727
def __init__(self, conjecturedata: "ConjectureData", /) -> None:
726728
super().__init__(conjecturedata)
727729
# calls to per_test_case_context_manager and on_observation alternate,

0 commit comments

Comments
 (0)