Skip to content

Commit 807c014

Browse files
committed
changed warning raised by async def to error
1 parent 00be7c0 commit 807c014

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

src/_pytest/config/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from typing import final
2-
3-
4-
@final
51
class UsageError(Exception):
62
"""Error in pytest usage or invocation."""
73

src/_pytest/python.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from _pytest.config import Config
5555
from _pytest.config import hookimpl
5656
from _pytest.config.argparsing import Parser
57+
from _pytest.config.exceptions import UsageError
5758
from _pytest.deprecated import check_ispytest
5859
from _pytest.fixtures import FixtureDef
5960
from _pytest.fixtures import FixtureRequest
@@ -77,7 +78,6 @@
7778
from _pytest.stash import StashKey
7879
from _pytest.warning_types import PytestCollectionWarning
7980
from _pytest.warning_types import PytestReturnNotNoneWarning
80-
from _pytest.warning_types import PytestUnhandledCoroutineWarning
8181

8282

8383
if TYPE_CHECKING:
@@ -138,6 +138,16 @@ def pytest_configure(config: Config) -> None:
138138
)
139139

140140

141+
@final
142+
class PytestUnhandledCoroutineError(UsageError):
143+
"""An unraisable exception resulted in an error.
144+
145+
Unraisable exceptions are exceptions raised in :meth:`__del__ <object.__del__>`
146+
implementations and similar situations when the exception cannot be raised
147+
as normal.
148+
"""
149+
150+
141151
def async_warn_and_skip(nodeid: str) -> None:
142152
msg = "async def functions are not natively supported and have been skipped.\n"
143153
msg += (
@@ -148,7 +158,9 @@ def async_warn_and_skip(nodeid: str) -> None:
148158
msg += " - pytest-tornasync\n"
149159
msg += " - pytest-trio\n"
150160
msg += " - pytest-twisted"
151-
warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))
161+
raise PytestUnhandledCoroutineError(
162+
msg.format(nodeid)
163+
) # TODO: This is the warning to look at
152164
skip(reason="async def function and no async plugin installed (see warnings)")
153165

154166

src/_pytest/warning_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def simple(cls, apiname: str) -> "PytestExperimentalApiWarning":
7777

7878

7979
@final
80-
class PytestUnhandledCoroutineWarning(PytestReturnNotNoneWarning):
80+
class PytestUnhandledCoroutineWarning(PytestReturnNotNoneWarning): # TODO: look at this
8181
"""Warning emitted for an unhandled coroutine.
8282
8383
A coroutine was encountered when collecting test functions, but was not

testing/acceptance_test.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ def test_usage_error_code(pytester: Pytester) -> None:
12331233
assert result.ret == ExitCode.USAGE_ERROR
12341234

12351235

1236-
def test_warn_on_async_function(pytester: Pytester) -> None:
1236+
def test_error_on_async_function(pytester: Pytester) -> None: # TODO: Change this
12371237
# In the below we .close() the coroutine only to avoid
12381238
# "RuntimeWarning: coroutine 'test_2' was never awaited"
12391239
# which messes with other tests.
@@ -1249,23 +1249,19 @@ def test_3():
12491249
return coro
12501250
"""
12511251
)
1252-
result = pytester.runpytest("-Wdefault")
1252+
result = pytester.runpytest()
12531253
result.stdout.fnmatch_lines(
12541254
[
1255-
"test_async.py::test_1",
1256-
"test_async.py::test_2",
1257-
"test_async.py::test_3",
1255+
"*test_async.py::test_1*",
1256+
"*test_async.py::test_2*",
1257+
"*test_async.py::test_3*",
12581258
"*async def functions are not natively supported*",
1259-
"*3 skipped, 3 warnings in*",
12601259
]
12611260
)
1262-
# ensure our warning message appears only once
1263-
assert (
1264-
result.stdout.str().count("async def functions are not natively supported") == 1
1265-
)
1261+
result.assert_outcomes(failed=3)
12661262

12671263

1268-
def test_warn_on_async_gen_function(pytester: Pytester) -> None:
1264+
def test_error_on_async_gen_function(pytester: Pytester) -> None: # TODO: Change this
12691265
pytester.makepyfile(
12701266
test_async="""
12711267
async def test_1():
@@ -1276,20 +1272,15 @@ def test_3():
12761272
return test_2()
12771273
"""
12781274
)
1279-
result = pytester.runpytest("-Wdefault")
1275+
result = pytester.runpytest()
12801276
result.stdout.fnmatch_lines(
12811277
[
1282-
"test_async.py::test_1",
1283-
"test_async.py::test_2",
1284-
"test_async.py::test_3",
1278+
"*test_async.py::test_1*",
1279+
"*test_async.py::test_2*",
1280+
"*test_async.py::test_3*",
12851281
"*async def functions are not natively supported*",
1286-
"*3 skipped, 3 warnings in*",
12871282
]
12881283
)
1289-
# ensure our warning message appears only once
1290-
assert (
1291-
result.stdout.str().count("async def functions are not natively supported") == 1
1292-
)
12931284

12941285

12951286
def test_pdb_can_be_rewritten(pytester: Pytester) -> None:

testing/test_unittest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def test_1(self):
13091309
assert tracked == []
13101310

13111311

1312-
def test_async_support(pytester: Pytester) -> None:
1312+
def test_async_support(pytester: Pytester) -> None: # TODO: Change this
13131313
pytest.importorskip("unittest.async_case")
13141314

13151315
pytester.copy_example("unittest/test_unittest_asyncio.py")

0 commit comments

Comments
 (0)