Skip to content

Commit 1f86be4

Browse files
committed
better post_test_case_hook error message
1 parent 39a8f72 commit 1f86be4

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from hypothesis import HealthCheck, Phase, Verbosity, settings as Settings
3939
from hypothesis._settings import local_settings
4040
from hypothesis.database import ExampleDatabase
41-
from hypothesis.errors import Flaky, InvalidArgument, StopTest
41+
from hypothesis.errors import Flaky, HypothesisException, InvalidArgument, StopTest
4242
from hypothesis.internal.cache import LRUReusedCache
4343
from hypothesis.internal.compat import (
4444
NotRequired,
@@ -456,9 +456,19 @@ def test_function(self, data: ConjectureData) -> None:
456456
if self.settings.backend != "hypothesis":
457457
for node in data.examples.ir_tree_nodes:
458458
value = data.provider.post_test_case_hook(node.value)
459-
assert (
460-
value is not None
461-
), "providers must return a non-null value from post_test_case_hook"
459+
expected_type = {
460+
"string": str,
461+
"float": float,
462+
"integer": int,
463+
"boolean": bool,
464+
"bytes": bytes,
465+
}[node.ir_type]
466+
if type(value) is not expected_type:
467+
raise HypothesisException(
468+
f"expected {expected_type} from "
469+
f"{data.provider.post_test_case_hook.__qualname__}, "
470+
f"got {type(value)} ({value})"
471+
)
462472
node.value = value
463473

464474
self._cache(data)

hypothesis-python/tests/conjecture/test_alt_backend.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from hypothesis import given, settings, strategies as st
2020
from hypothesis.database import InMemoryExampleDatabase
21-
from hypothesis.errors import Flaky, InvalidArgument
21+
from hypothesis.errors import Flaky, HypothesisException, InvalidArgument
2222
from hypothesis.internal.compat import int_to_bytes
2323
from hypothesis.internal.conjecture.data import (
2424
AVAILABLE_PROVIDERS,
@@ -370,3 +370,23 @@ def test_function(n):
370370

371371
with pytest.raises(Flaky):
372372
test_function()
373+
374+
375+
class BadPostTestCaseHookProvider(TrivialProvider):
376+
def post_test_case_hook(self, value):
377+
return None
378+
379+
380+
def test_bad_post_test_case_hook():
381+
with temp_register_backend("bad_hook", BadPostTestCaseHookProvider):
382+
383+
@given(st.integers())
384+
@settings(backend="bad_hook")
385+
def test_function(n):
386+
pass
387+
388+
with pytest.raises(
389+
HypothesisException,
390+
match="expected .* from BadPostTestCaseHookProvider.post_test_case_hook",
391+
):
392+
test_function()

0 commit comments

Comments
 (0)