Skip to content

Do not try to initialize async fixtures without explicit asyncio mark in strict mode #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

UNRELEASED
=================
- Adds `pytest-trio <https://pypi.org/project/pytest-trio/>`_ to the test dependencies
- Fixes a bug that caused pytest-asyncio to try to set up async pytest_trio fixtures in strict mode. `#298 <https://github.com/pytest-dev/pytest-asyncio/issues/298>`_

0.18.2 (22-03-03)
=================
- Fix asyncio auto mode not marking static methods. `#295 <https://github.com/pytest-dev/pytest-asyncio/issues/295>`_
Expand Down
6 changes: 5 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def _preprocess_async_fixtures(config: Config, holder: Set[FixtureDef]) -> None:
# Nothing to do with a regular fixture function
continue
if not _has_explicit_asyncio_mark(func):
if asyncio_mode == Mode.AUTO:
if asyncio_mode == Mode.STRICT:
# Ignore async fixtures without explicit asyncio mark in strict mode
# This applies to pytest_trio fixtures, for example
continue
elif asyncio_mode == Mode.AUTO:
# Enforce asyncio mode if 'auto'
_set_explicit_asyncio_mark(func)
elif asyncio_mode == Mode.LEGACY:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ testing =
hypothesis >= 5.7.1
flaky >= 3.5.0
mypy == 0.931
pytest-trio >= 0.7.0

[options.entry_points]
pytest11 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import pytest


class CustomSelectorLoopSession(asyncio.SelectorEventLoop):
class CustomSelectorLoop(asyncio.SelectorEventLoop):
"""A subclass with no overrides, just to test for presence."""


loop = CustomSelectorLoopSession()
loop = CustomSelectorLoop()


@pytest.fixture(scope="package")
@pytest.fixture(scope="module")
def event_loop():
"""Create an instance of the default event loop for each test case."""
yield loop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Unit tests for overriding the event loop with a session scoped one."""
"""Unit tests for overriding the event loop with a larger scoped one."""
import asyncio

import pytest
Expand All @@ -8,7 +8,7 @@
async def test_for_custom_loop():
"""This test should be executed using the custom loop."""
await asyncio.sleep(0.01)
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoopSession"
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop"


@pytest.mark.asyncio
Expand Down
23 changes: 13 additions & 10 deletions tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ def event_loop():
loop.close()


@pytest.mark.asyncio(forbid_global_loop=False)
async def test_subprocess(event_loop):
"""Starting a subprocess should be possible."""
proc = await asyncio.subprocess.create_subprocess_exec(
sys.executable, "--version", stdout=asyncio.subprocess.PIPE
)
await proc.communicate()
@pytest.mark.skipif(
sys.version_info < (3, 8),
reason="""
When run with Python 3.7 asyncio.subprocess.create_subprocess_exec seems to be
affected by an issue that prevents correct cleanup. Tests using pytest-trio
will report that signal handling is already performed by another library and
fail. [1] This is possibly a bug in CPython 3.7, so we ignore this test for
that Python version.


@pytest.mark.asyncio(forbid_global_loop=True)
async def test_subprocess_forbid(event_loop):
[1] https://github.com/python-trio/pytest-trio/issues/126
""",
)
@pytest.mark.asyncio
async def test_subprocess(event_loop):
"""Starting a subprocess should be possible."""
proc = await asyncio.subprocess.create_subprocess_exec(
sys.executable, "--version", stdout=asyncio.subprocess.PIPE
Expand Down
25 changes: 25 additions & 0 deletions tests/trio/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from textwrap import dedent


def test_strict_mode_ignores_trio_fixtures(testdir):
testdir.makepyfile(
dedent(
"""\
import pytest
import pytest_asyncio
import pytest_trio
pytest_plugins = ["pytest_asyncio", "pytest_trio"]
@pytest_trio.trio_fixture
async def any_fixture():
return True
@pytest.mark.trio
async def test_anything(any_fixture):
pass
"""
)
)
result = testdir.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)