Skip to content

Commit 2c14bae

Browse files
authored
Merge pull request #3056 from Zac-HD/coroutine-healthcheck
Improve error message when `@given()` is applied to an `async def` function
2 parents 0dd057f + e3bdb1c commit 2c14bae

4 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎hypothesis-python/RELEASE.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
RELEASE_TYPE: minor
2+
3+
This release emits a more useful error message when :func:`@given() <hypothesis.given>`
4+
is applied to a coroutine function, i.e. one defined using ``async def`` (:issue:`3054`).
5+
6+
This was previously only handled by the generic :obj:`~hypothesis.HealthCheck.return_value`
7+
health check, which doesn't direct you to use either :ref:`a custom executor <custom-function-execution>`
8+
or a library such as :pypi:`pytest-trio` or :pypi:`pytest-asyncio` to handle it for you.

‎hypothesis-python/src/hypothesis/core.py‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
Unsatisfiable,
6161
UnsatisfiedAssumption,
6262
)
63-
from hypothesis.executors import new_style_executor
63+
from hypothesis.executors import default_new_style_executor, new_style_executor
6464
from hypothesis.internal.compat import (
6565
PYPY,
6666
bad_django_TestCase,
@@ -1019,15 +1019,12 @@ def wrapped_test(*arguments, **kwargs):
10191019

10201020
if getattr(test, "is_hypothesis_test", False):
10211021
raise InvalidArgument(
1022-
(
1023-
"You have applied @given to the test %s more than once, which "
1024-
"wraps the test several times and is extremely slow. A "
1025-
"similar effect can be gained by combining the arguments "
1026-
"of the two calls to given. For example, instead of "
1027-
"@given(booleans()) @given(integers()), you could write "
1028-
"@given(booleans(), integers())"
1029-
)
1030-
% (test.__name__,)
1022+
f"You have applied @given to the test {test.__name__} more than "
1023+
"once, which wraps the test several times and is extremely slow. "
1024+
"A similar effect can be gained by combining the arguments "
1025+
"of the two calls to given. For example, instead of "
1026+
"@given(booleans()) @given(integers()), you could write "
1027+
"@given(booleans(), integers())"
10311028
)
10321029

10331030
settings = wrapped_test._hypothesis_internal_use_settings
@@ -1039,12 +1036,28 @@ def wrapped_test(*arguments, **kwargs):
10391036
)
10401037
arguments, kwargs, test_runner, search_strategy = processed_args
10411038

1039+
if (
1040+
inspect.iscoroutinefunction(test)
1041+
and test_runner is default_new_style_executor
1042+
):
1043+
# See https://github.com/HypothesisWorks/hypothesis/issues/3054
1044+
# If our custom executor doesn't handle coroutines, or we return an
1045+
# awaitable from a non-async-def function, we just rely on the
1046+
# return_value health check. This catches most user errors though.
1047+
raise InvalidArgument(
1048+
"Hypothesis doesn't know how to run async test functions like "
1049+
f"{test.__name__}. You'll need to write a custom executor, "
1050+
"or use a library like pytest-asyncio or pytest-trio which can "
1051+
"handle the translation for you.\n See https://hypothesis."
1052+
"readthedocs.io/en/latest/details.html#custom-function-execution"
1053+
)
1054+
10421055
runner = getattr(search_strategy, "runner", None)
10431056
if isinstance(runner, TestCase) and test.__name__ in dir(TestCase):
10441057
msg = (
1045-
"You have applied @given to the method %s, which is "
1058+
f"You have applied @given to the method {test.__name__}, which is "
10461059
"used by the unittest runner but is not itself a test."
1047-
" This is not useful in any way." % test.__name__
1060+
" This is not useful in any way."
10481061
)
10491062
fail_health_check(settings, msg, HealthCheck.not_a_test_method)
10501063
if bad_django_TestCase(runner): # pragma: no cover

‎hypothesis-python/tests/cover/test_given_error_conditions.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,16 @@ def test_given_is_not_a_class_decorator():
7474
class test_given_is_not_a_class_decorator:
7575
def __init__(self, i):
7676
pass
77+
78+
79+
def test_specific_error_for_coroutine_functions():
80+
@settings(database=None)
81+
@given(booleans())
82+
async def foo(x):
83+
pass
84+
85+
with pytest.raises(
86+
InvalidArgument,
87+
match="Hypothesis doesn't know how to run async test functions",
88+
):
89+
foo()

‎tooling/src/hypothesistooling/__main__.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def audit_conjecture_rust():
543543
def tasks():
544544
"""Print a list of all task names supported by the build system."""
545545
for task_name in sorted(TASKS.keys()):
546-
print(task_name)
546+
print(" " + task_name)
547547

548548

549549
if __name__ == "__main__":
@@ -569,6 +569,11 @@ def tasks():
569569
)
570570
sys.exit(1)
571571

572+
if task_to_run not in TASKS:
573+
print(f"\nUnknown task {task_to_run!r}. Available tasks are:")
574+
tasks()
575+
sys.exit(1)
576+
572577
try:
573578
TASKS[task_to_run](*args)
574579
except subprocess.CalledProcessError as e:

0 commit comments

Comments
 (0)