From 442dccef6524b313cc62887e02e964aa285321ee Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 30 Jan 2020 22:55:01 +0100 Subject: [PATCH] python: factor out async_warn --- src/_pytest/python.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index c30dbc477..1b94aaf00 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -146,27 +146,30 @@ def pytest_configure(config): ) -@hookimpl(trylast=True) -def pytest_pyfunc_call(pyfuncitem): - def async_warn(): - msg = "async def functions are not natively supported and have been skipped.\n" - msg += "You need to install a suitable plugin for your async framework, for example:\n" - msg += " - pytest-asyncio\n" - msg += " - pytest-trio\n" - msg += " - pytest-tornasync" - warnings.warn(PytestUnhandledCoroutineWarning(msg.format(pyfuncitem.nodeid))) - skip(msg="async def function and no async plugin installed (see warnings)") +def async_warn(nodeid: str) -> None: + msg = "async def functions are not natively supported and have been skipped.\n" + msg += ( + "You need to install a suitable plugin for your async framework, for example:\n" + ) + msg += " - pytest-asyncio\n" + msg += " - pytest-trio\n" + msg += " - pytest-tornasync" + warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid))) + skip(msg="async def function and no async plugin installed (see warnings)") + +@hookimpl(trylast=True) +def pytest_pyfunc_call(pyfuncitem: "Function"): testfunction = pyfuncitem.obj if iscoroutinefunction(testfunction) or ( sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction) ): - async_warn() + async_warn(pyfuncitem.nodeid) funcargs = pyfuncitem.funcargs testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} result = testfunction(**testargs) if hasattr(result, "__await__") or hasattr(result, "__aiter__"): - async_warn() + async_warn(pyfuncitem.nodeid) return True