Skip to content

Commit 4ba9021

Browse files
authored
Merge pull request HypothesisWorks#2956 from Zac-HD/target-returns
Allow `target()` inline in expressions
2 parents ce77c25 + 2840a3a commit 4ba9021

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

hypothesis-python/RELEASE.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: minor
2+
3+
:func:`hypothesis.target` now returns the ``observation`` value,
4+
allowing it to be conveniently used inline in expressions such as
5+
``assert target(abs(a - b)) < 0.1``.

hypothesis-python/src/hypothesis/control.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def event(value: str) -> None:
135135
context.data.note_event(value)
136136

137137

138-
def target(observation: Union[int, float], *, label: str = "") -> None:
138+
def target(observation: Union[int, float], *, label: str = "") -> Union[int, float]:
139139
"""Calling this function with an ``int`` or ``float`` observation gives it feedback
140140
with which to guide our search for inputs that will cause an error, in
141141
addition to all the usual heuristics. Observations must always be finite.
@@ -165,11 +165,6 @@ def target(observation: Union[int, float], *, label: str = "") -> None:
165165
and immediately obvious by around ten thousand examples
166166
*per label* used by your test.
167167
168-
.. note::
169-
``hypothesis.target`` is considered experimental, and may be radically
170-
changed or even removed in a future version. If you find it useful,
171-
please let us know so we can share and build on that success!
172-
173168
:ref:`statistics` include the best score seen for each label,
174169
which can help avoid `the threshold problem
175170
<https://hypothesis.works/articles/threshold-problem/>`__ when the minimal
@@ -182,7 +177,10 @@ def target(observation: Union[int, float], *, label: str = "") -> None:
182177

183178
context = _current_build_context.value
184179
if context is None:
185-
raise InvalidArgument("Calling target() outside of a test is invalid.")
180+
raise InvalidArgument(
181+
"Calling target() outside of a test is invalid. "
182+
"Consider guarding this call with `if currently_in_test_context(): ...`"
183+
)
186184
verbose_report(f"Saw target(observation={observation!r}, label={label!r})")
187185

188186
if label in context.data.target_observations:
@@ -192,3 +190,5 @@ def target(observation: Union[int, float], *, label: str = "") -> None:
192190
)
193191
else:
194192
context.data.target_observations[label] = observation
193+
194+
return observation

hypothesis-python/tests/nocover/test_targeting.py

+7
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ def test_with_targeting(ls):
5454

5555
with pytest.raises(AssertionError):
5656
test_with_targeting()
57+
58+
59+
@given(st.integers(), st.integers())
60+
def test_target_returns_value(a, b):
61+
difference = target(abs(a - b))
62+
assert difference == abs(a - b)
63+
assert isinstance(difference, int)

0 commit comments

Comments
 (0)