Skip to content

Commit

Permalink
Relax asyncio_mode type definition; it allows to support pytest 6.1+ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jan 16, 2022
1 parent 087e0b6 commit 36f277d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.rst
Expand Up @@ -262,6 +262,7 @@ Changelog
- Fixed a bug that closes the default event loop if the loop doesn't exist `#257 <https://github.com/pytest-dev/pytest-asyncio/issues/257>`_
- Added type annotations. `#198 <https://github.com/pytest-dev/pytest-asyncio/issues/198>`_
- Show asyncio mode in pytest report headers. `#266 <https://github.com/pytest-dev/pytest-asyncio/issues/266>`_
- Relax ``asyncio_mode`` type definition; it allows to support pytest 6.1+. `#262 <https://github.com/pytest-dev/pytest-asyncio/issues/262>`_

0.17.0 (22-01-13)
~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 1 addition & 2 deletions pytest_asyncio/plugin.py
Expand Up @@ -90,7 +90,6 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None
parser.addini(
"asyncio_mode",
help="default value for --asyncio-mode",
type="string",
default="legacy",
)

Expand Down Expand Up @@ -461,7 +460,7 @@ def pytest_runtest_setup(item: pytest.Item) -> None:


@pytest.fixture
def event_loop(request: pytest.FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]:
def event_loop(request: "pytest.FixtureRequest") -> Iterator[asyncio.AbstractEventLoop]:
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -38,7 +38,7 @@ setup_requires =
setuptools_scm >= 6.2

install_requires =
pytest >= 5.4.0
pytest >= 6.1.0
typing-extensions >= 4.0

[options.extras_require]
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Expand Up @@ -2,6 +2,8 @@

import pytest

pytest_plugins = "pytester"


@pytest.fixture
def dependent_fixture(event_loop):
Expand Down
12 changes: 6 additions & 6 deletions tests/hypothesis/test_base.py
Expand Up @@ -43,8 +43,8 @@ async def test_can_use_fixture_provided_event_loop(event_loop, n):
await semaphore.acquire()


def test_async_auto_marked(pytester):
pytester.makepyfile(
def test_async_auto_marked(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -60,13 +60,13 @@ async def test_hypothesis(n: int):
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_sync_not_auto_marked(pytester):
def test_sync_not_auto_marked(testdir):
"""Assert that synchronous Hypothesis functions are not marked with asyncio"""
pytester.makepyfile(
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -84,5 +84,5 @@ def test_hypothesis(request, n: int):
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)
28 changes: 13 additions & 15 deletions tests/modes/test_auto_mode.py
@@ -1,10 +1,8 @@
from textwrap import dedent

pytest_plugins = "pytester"


def test_auto_mode_cmdline(pytester):
pytester.makepyfile(
def test_auto_mode_cmdline(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -17,12 +15,12 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_cfg(pytester):
pytester.makepyfile(
def test_auto_mode_cfg(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -35,13 +33,13 @@ async def test_a():
"""
)
)
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n")
result = pytester.runpytest()
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n")
result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_auto_mode_async_fixture(pytester):
pytester.makepyfile(
def test_auto_mode_async_fixture(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -60,12 +58,12 @@ async def test_a(fixture_a):
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)


def test_auto_mode_method_fixture(pytester):
pytester.makepyfile(
def test_auto_mode_method_fixture(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -87,5 +85,5 @@ async def test_a(self, fixture_a):
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)
29 changes: 13 additions & 16 deletions tests/modes/test_legacy_mode.py
@@ -1,8 +1,5 @@
from textwrap import dedent

pytest_plugins = "pytester"


LEGACY_MODE = (
"The 'asyncio_mode' default value will change to 'strict' in future, "
"please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' "
Expand All @@ -18,8 +15,8 @@
).format(name="*")


def test_warning_for_legacy_mode_cmdline(pytester):
pytester.makepyfile(
def test_warning_for_legacy_mode_cmdline(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -33,13 +30,13 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=legacy")
result = testdir.runpytest("--asyncio-mode=legacy")
assert result.parseoutcomes()["warnings"] == 1
result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"])


def test_warning_for_legacy_mode_cfg(pytester):
pytester.makepyfile(
def test_warning_for_legacy_mode_cfg(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -53,15 +50,15 @@ async def test_a():
"""
)
)
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n")
result = pytester.runpytest()
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n")
result = testdir.runpytest()
assert result.parseoutcomes()["warnings"] == 1
result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"])
result.stdout.no_fnmatch_line("*" + LEGACY_ASYNCIO_FIXTURE + "*")


def test_warning_for_legacy_fixture(pytester):
pytester.makepyfile(
def test_warning_for_legacy_fixture(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -81,13 +78,13 @@ async def test_a(fixture_a):
"""
)
)
result = pytester.runpytest("--asyncio-mode=legacy")
result = testdir.runpytest("--asyncio-mode=legacy")
assert result.parseoutcomes()["warnings"] == 2
result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"])


def test_warning_for_legacy_method_fixture(pytester):
pytester.makepyfile(
def test_warning_for_legacy_method_fixture(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -110,6 +107,6 @@ async def test_a(self, fixture_a):
"""
)
)
result = pytester.runpytest("--asyncio-mode=legacy")
result = testdir.runpytest("--asyncio-mode=legacy")
assert result.parseoutcomes()["warnings"] == 2
result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"])
22 changes: 10 additions & 12 deletions tests/modes/test_strict_mode.py
@@ -1,10 +1,8 @@
from textwrap import dedent

pytest_plugins = "pytester"


def test_strict_mode_cmdline(pytester):
pytester.makepyfile(
def test_strict_mode_cmdline(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -18,12 +16,12 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result = testdir.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)


def test_strict_mode_cfg(pytester):
pytester.makepyfile(
def test_strict_mode_cfg(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -37,13 +35,13 @@ async def test_a():
"""
)
)
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n")
result = pytester.runpytest()
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n")
result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_strict_mode_method_fixture(pytester):
pytester.makepyfile(
def test_strict_mode_method_fixture(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -66,5 +64,5 @@ async def test_a(self, fixture_a):
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result = testdir.runpytest("--asyncio-mode=auto")
result.assert_outcomes(passed=1)
10 changes: 3 additions & 7 deletions tests/test_flaky_integration.py
@@ -1,14 +1,10 @@
"""Tests for the Flaky integration, which retries failed tests.
"""


from textwrap import dedent

pytest_plugins = "pytester"


def test_auto_mode_cmdline(pytester):
pytester.makepyfile(
def test_auto_mode_cmdline(testdir):
testdir.makepyfile(
dedent(
"""\
import asyncio
Expand All @@ -29,7 +25,7 @@ async def test_asyncio_flaky_thing_that_fails_then_succeeds():
)
# runpytest_subprocess() is required to don't pollute the output
# with flaky restart information
result = pytester.runpytest_subprocess()
result = testdir.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines(
[
Expand Down
14 changes: 12 additions & 2 deletions tox.ini
@@ -1,12 +1,22 @@
[tox]
minversion = 3.14.0
envlist = py37, py38, py39, py310, lint, version-info
envlist = py37, py38, py39, py310, lint, version-info, pytest-min
skip_missing_interpreters = true
passenv =
CI

[testenv]
extras = testing
deps =
pytest == 6.2.5 # required for Python 3.10, not bad for others
commands = make test
allowlist_externals =
make

[testenv:pytest-min]
extras = testing
deps =
pytest == 6.1.0
commands = make test
allowlist_externals =
make
Expand Down Expand Up @@ -38,7 +48,7 @@ commands =

[gh-actions]
python =
3.7: py37
3.7: py37, pytest-min
3.8: py38
3.9: py39, lint
3.10: py310
Expand Down

0 comments on commit 36f277d

Please sign in to comment.