Skip to content

Commit 4f46426

Browse files
committed
Restore implicit switch to asyncio_mode = auto if legacy mode is detected
1 parent 95d8d45 commit 4f46426

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

CHANGES.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGES
22
=======
33

4+
1.0.2 (2022-01-20)
5+
------------------
6+
7+
- Restore implicit switch to ``asyncio_mode = auto`` if *legacy* mode is detected.
8+
49
1.0.1 (2022-01-20)
510
------------------
611

pytest_aiohttp/plugin.py

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
AiohttpClient = Callable[[Union[Application, BaseTestServer]], Awaitable[TestClient]]
1111

1212

13+
LEGACY_MODE = DeprecationWarning(
14+
"The 'asyncio_mode' is 'legacy', switching to 'auto' for the sake of "
15+
"pytest-aiohttp backward compatibility. "
16+
"Please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' "
17+
"in pytest configuration file."
18+
)
19+
20+
21+
@pytest.mark.tryfirst
22+
def pytest_configure(config) -> None:
23+
val = config.getoption("asyncio_mode")
24+
if val is None:
25+
val = config.getini("asyncio_mode")
26+
if val == "legacy":
27+
config.option.asyncio_mode = "auto"
28+
config.issue_config_time_warning(LEGACY_MODE, stacklevel=2)
29+
30+
1331
@pytest.fixture
1432
def loop(event_loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop:
1533
warnings.warn(

tests/test_switch_mode.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import Any
2+
3+
from pytest_aiohttp.plugin import LEGACY_MODE
4+
5+
pytest_plugins: str = "pytester"
6+
7+
8+
def test_warning_for_legacy_mode(testdir: Any) -> None:
9+
testdir.makepyfile(
10+
"""\
11+
async def test_a():
12+
pass
13+
14+
"""
15+
)
16+
result = testdir.runpytest_subprocess("--asyncio-mode=legacy")
17+
result.assert_outcomes(passed=1)
18+
result.stdout.fnmatch_lines(["*" + str(LEGACY_MODE) + "*"])
19+
20+
21+
def test_auto_mode(testdir: Any) -> None:
22+
testdir.makepyfile(
23+
"""\
24+
async def test_a():
25+
pass
26+
27+
"""
28+
)
29+
result = testdir.runpytest_subprocess("--asyncio-mode=auto")
30+
result.assert_outcomes(passed=1)
31+
result.stdout.no_fnmatch_line("*" + str(LEGACY_MODE) + "*")
32+
33+
34+
def test_strict_mode(testdir: Any) -> None:
35+
testdir.makepyfile(
36+
"""\
37+
import pytest
38+
39+
40+
@pytest.mark.asyncio
41+
async def test_a():
42+
pass
43+
44+
"""
45+
)
46+
result = testdir.runpytest_subprocess("--asyncio-mode=strict")
47+
result.assert_outcomes(passed=1)
48+
result.stdout.no_fnmatch_line("*" + str(LEGACY_MODE) + "*")

0 commit comments

Comments
 (0)