Skip to content

Commit b1d0ce7

Browse files
committed
[refactor] Moved code that generates a warning when marking a synchronous function with "asyncio" from the synchronization wrapper to the pytest_pyfunc_call hook.
Whether to raise a warning due to unnecessary marker usage should be of no concern to the synchronization wrapper. The change implies that pytest-asyncio now checks that the test function is a coroutine or async generator, instead of checking that the return value of the test function is awaitable. Signed-off-by: Michael Seifert <[email protected]>
1 parent 9643e2f commit b1d0ce7

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

pytest_asyncio/plugin.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,21 @@ def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> Optional[object]:
672672
pyfuncitem,
673673
pyfuncitem.obj.hypothesis.inner_test,
674674
)
675-
else:
675+
elif _is_coroutine_or_asyncgen(pyfuncitem.obj):
676676
pyfuncitem.obj = wrap_in_sync(
677677
pyfuncitem,
678678
pyfuncitem.obj,
679679
)
680+
else:
681+
pyfuncitem.warn(
682+
pytest.PytestWarning(
683+
f"The test {pyfuncitem} is marked with '@pytest.mark.asyncio' "
684+
"but it is not an async function. "
685+
"Please remove asyncio marker. "
686+
"If the test is not marked explicitly, "
687+
"check for global markers applied via 'pytestmark'."
688+
)
689+
)
680690
yield
681691

682692

@@ -701,17 +711,6 @@ def wrap_in_sync(
701711
@functools.wraps(func)
702712
def inner(*args, **kwargs):
703713
coro = func(*args, **kwargs)
704-
if not inspect.isawaitable(coro):
705-
pyfuncitem.warn(
706-
pytest.PytestWarning(
707-
f"The test {pyfuncitem} is marked with '@pytest.mark.asyncio' "
708-
"but it is not an async function. "
709-
"Please remove asyncio marker. "
710-
"If the test is not marked explicitly, "
711-
"check for global markers applied via 'pytestmark'."
712-
)
713-
)
714-
return
715714
_loop = asyncio.get_event_loop()
716715
task = asyncio.ensure_future(coro, loop=_loop)
717716
try:

0 commit comments

Comments
 (0)