Skip to content

Preprocess async fixtures for each event loop #906

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 7 commits into from
Aug 9, 2024
2 changes: 1 addition & 1 deletion dependencies/default/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Always adjust install_requires in setup.cfg and pytest-min-requirements.txt
# when changing runtime dependencies
pytest >= 7.0.0,<9
pytest >= 8.2,<9
4 changes: 2 additions & 2 deletions dependencies/pytest-min/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ iniconfig==2.0.0
mock==5.1.0
nose==1.3.7
packaging==23.2
pluggy==1.3.0
pluggy==1.5.0
py==1.11.0
Pygments==2.16.1
pytest==7.0.0
pytest==8.2.0
requests==2.31.0
sortedcontainers==2.4.0
tomli==2.0.1
Expand Down
2 changes: 1 addition & 1 deletion dependencies/pytest-min/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Always adjust install_requires in setup.cfg and requirements.txt
# when changing minimum version dependencies
pytest[testing] == 7.0.0
pytest[testing] == 8.2.0
2 changes: 2 additions & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Changelog

0.24.0 (UNRELEASED)
===================
- BREAKING: Updated minimum supported pytest version to v8.2.0
- Adds an optional `loop_scope` keyword argument to `pytest.mark.asyncio`. This argument controls which event loop is used to run the marked async test. `#706`_, `#871 <https://github.com/pytest-dev/pytest-asyncio/pull/871>`_
- Deprecates the optional `scope` keyword argument to `pytest.mark.asyncio` for API consistency with ``pytest_asyncio.fixture``. Users are encouraged to use the `loop_scope` keyword argument, which does exactly the same.
- Raises an error when passing `scope` or `loop_scope` as a positional argument to ``@pytest.mark.asyncio``. `#812 <https://github.com/pytest-dev/pytest-asyncio/issues/812>`_
- Fixes a bug that caused module-scoped async fixtures to fail when reused in other modules `#862 <https://github.com/pytest-dev/pytest-asyncio/issues/862>`_ `#668 <https://github.com/pytest-dev/pytest-asyncio/issues/668>`_


0.23.8 (2024-07-17)
Expand Down
129 changes: 56 additions & 73 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
overload,
)

import pluggy
import pytest
from pytest import (
Class,
Collector,
Config,
FixtureDef,
FixtureRequest,
Function,
Item,
Expand Down Expand Up @@ -62,12 +64,6 @@
FixtureFunction = Union[SimpleFixtureFunction, FactoryFixtureFunction]
FixtureFunctionMarker = Callable[[FixtureFunction], FixtureFunction]

# https://github.com/pytest-dev/pytest/commit/fb55615d5e999dd44306596f340036c195428ef1
if pytest.version_tuple < (8, 0):
FixtureDef = Any
else:
from pytest import FixtureDef


class PytestAsyncioError(Exception):
"""Base class for exceptions raised by pytest-asyncio"""
Expand Down Expand Up @@ -251,15 +247,8 @@ def _preprocess_async_fixtures(
or default_loop_scope
or fixturedef.scope
)
if scope == "function":
event_loop_fixture_id: Optional[str] = "event_loop"
else:
event_loop_node = _retrieve_scope_root(collector, scope)
event_loop_fixture_id = event_loop_node.stash.get(
# Type ignored because of non-optimal mypy inference.
_event_loop_fixture_id, # type: ignore[arg-type]
None,
)
if scope == "function" and "event_loop" not in fixturedef.argnames:
fixturedef.argnames += ("event_loop",)
_make_asyncio_fixture_function(func, scope)
function_signature = inspect.signature(func)
if "event_loop" in function_signature.parameters:
Expand All @@ -271,64 +260,39 @@ def _preprocess_async_fixtures(
f"instead."
)
)
assert event_loop_fixture_id
_inject_fixture_argnames(
fixturedef,
event_loop_fixture_id,
)
_synchronize_async_fixture(
fixturedef,
event_loop_fixture_id,
)
if "request" not in fixturedef.argnames:
fixturedef.argnames += ("request",)
_synchronize_async_fixture(fixturedef)
assert _is_asyncio_fixture_function(fixturedef.func)
processed_fixturedefs.add(fixturedef)


def _inject_fixture_argnames(
fixturedef: FixtureDef, event_loop_fixture_id: str
) -> None:
"""
Ensures that `request` and `event_loop` are arguments of the specified fixture.
"""
to_add = []
for name in ("request", event_loop_fixture_id):
if name not in fixturedef.argnames:
to_add.append(name)
if to_add:
fixturedef.argnames += tuple(to_add)


def _synchronize_async_fixture(
fixturedef: FixtureDef, event_loop_fixture_id: str
) -> None:
def _synchronize_async_fixture(fixturedef: FixtureDef) -> None:
"""
Wraps the fixture function of an async fixture in a synchronous function.
"""
if inspect.isasyncgenfunction(fixturedef.func):
_wrap_asyncgen_fixture(fixturedef, event_loop_fixture_id)
_wrap_asyncgen_fixture(fixturedef)
elif inspect.iscoroutinefunction(fixturedef.func):
_wrap_async_fixture(fixturedef, event_loop_fixture_id)
_wrap_async_fixture(fixturedef)


def _add_kwargs(
func: Callable[..., Any],
kwargs: Dict[str, Any],
event_loop_fixture_id: str,
event_loop: asyncio.AbstractEventLoop,
request: FixtureRequest,
) -> Dict[str, Any]:
sig = inspect.signature(func)
ret = kwargs.copy()
if "request" in sig.parameters:
ret["request"] = request
if event_loop_fixture_id in sig.parameters:
ret[event_loop_fixture_id] = event_loop
if "event_loop" in sig.parameters:
ret["event_loop"] = event_loop
return ret


def _perhaps_rebind_fixture_func(
func: _T, instance: Optional[Any], unittest: bool
) -> _T:
def _perhaps_rebind_fixture_func(func: _T, instance: Optional[Any]) -> _T:
if instance is not None:
# The fixture needs to be bound to the actual request.instance
# so it is bound to the same object as the test method.
Expand All @@ -337,36 +301,36 @@ def _perhaps_rebind_fixture_func(
unbound, cls = func.__func__, type(func.__self__) # type: ignore
except AttributeError:
pass
# If unittest is true, the fixture is bound unconditionally.
# otherwise, only if the fixture was bound before to an instance of
# Only if the fixture was bound before to an instance of
# the same type.
if unittest or (cls is not None and isinstance(instance, cls)):
if cls is not None and isinstance(instance, cls):
func = unbound.__get__(instance) # type: ignore
return func


def _wrap_asyncgen_fixture(fixturedef: FixtureDef, event_loop_fixture_id: str) -> None:
def _wrap_asyncgen_fixture(fixturedef: FixtureDef) -> None:
fixture = fixturedef.func

@functools.wraps(fixture)
def _asyncgen_fixture_wrapper(request: FixtureRequest, **kwargs: Any):
unittest = fixturedef.unittest if hasattr(fixturedef, "unittest") else False
func = _perhaps_rebind_fixture_func(fixture, request.instance, unittest)
event_loop = kwargs.pop(event_loop_fixture_id)
gen_obj = func(
**_add_kwargs(func, kwargs, event_loop_fixture_id, event_loop, request)
func = _perhaps_rebind_fixture_func(fixture, request.instance)
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
request, func
)
event_loop = request.getfixturevalue(event_loop_fixture_id)
kwargs.pop(event_loop_fixture_id, None)
gen_obj = func(**_add_kwargs(func, kwargs, event_loop, request))

async def setup():
res = await gen_obj.__anext__()
res = await gen_obj.__anext__() # type: ignore[union-attr]
return res

def finalizer() -> None:
"""Yield again, to finalize."""

async def async_finalizer() -> None:
try:
await gen_obj.__anext__()
await gen_obj.__anext__() # type: ignore[union-attr]
except StopAsyncIteration:
pass
else:
Expand All @@ -380,27 +344,48 @@ async def async_finalizer() -> None:
request.addfinalizer(finalizer)
return result

fixturedef.func = _asyncgen_fixture_wrapper
fixturedef.func = _asyncgen_fixture_wrapper # type: ignore[misc]


def _wrap_async_fixture(fixturedef: FixtureDef, event_loop_fixture_id: str) -> None:
def _wrap_async_fixture(fixturedef: FixtureDef) -> None:
fixture = fixturedef.func

@functools.wraps(fixture)
def _async_fixture_wrapper(request: FixtureRequest, **kwargs: Any):
unittest = False if pytest.version_tuple >= (8, 2) else fixturedef.unittest
func = _perhaps_rebind_fixture_func(fixture, request.instance, unittest)
event_loop = kwargs.pop(event_loop_fixture_id)
func = _perhaps_rebind_fixture_func(fixture, request.instance)
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
request, func
)
event_loop = request.getfixturevalue(event_loop_fixture_id)
kwargs.pop(event_loop_fixture_id, None)

async def setup():
res = await func(
**_add_kwargs(func, kwargs, event_loop_fixture_id, event_loop, request)
)
res = await func(**_add_kwargs(func, kwargs, event_loop, request))
return res

return event_loop.run_until_complete(setup())

fixturedef.func = _async_fixture_wrapper
fixturedef.func = _async_fixture_wrapper # type: ignore[misc]


def _get_event_loop_fixture_id_for_async_fixture(
request: FixtureRequest, func: Any
) -> str:
default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope")
loop_scope = (
getattr(func, "_loop_scope", None) or default_loop_scope or request.scope
)
if loop_scope == "function":
event_loop_fixture_id = "event_loop"
else:
event_loop_node = _retrieve_scope_root(request._pyfuncitem, loop_scope)
event_loop_fixture_id = event_loop_node.stash.get(
# Type ignored because of non-optimal mypy inference.
_event_loop_fixture_id, # type: ignore[arg-type]
"",
)
assert event_loop_fixture_id
return event_loop_fixture_id


class PytestAsyncioFunction(Function):
Expand Down Expand Up @@ -555,13 +540,12 @@ def pytest_pycollect_makeitem_preprocess_async_fixtures(
return None


# TODO: #778 Narrow down return type of function when dropping support for pytest 7
# The function name needs to start with "pytest_"
# see https://github.com/pytest-dev/pytest/issues/11307
@pytest.hookimpl(specname="pytest_pycollect_makeitem", hookwrapper=True)
def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
collector: Union[pytest.Module, pytest.Class], name: str, obj: object
) -> Generator[None, Any, None]:
) -> Generator[None, pluggy.Result, None]:
"""
Converts coroutines and async generators collected as pytest.Functions
to AsyncFunction items.
Expand Down Expand Up @@ -772,11 +756,10 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:
)


# TODO: #778 Narrow down return type of function when dropping support for pytest 7
@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(
fixturedef: FixtureDef,
) -> Generator[None, Any, None]:
) -> Generator[None, pluggy.Result, None]:
"""Adjust the event loop policy when an event loop is produced."""
if fixturedef.argname == "event_loop":
# The use of a fixture finalizer is preferred over the
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ include_package_data = True

# Always adjust requirements.txt and pytest-min-requirements.txt when changing runtime dependencies
install_requires =
pytest >= 7.0.0,<9
pytest >= 8.2,<9

[options.extras_require]
testing =
Expand Down
35 changes: 35 additions & 0 deletions tests/async_fixtures/test_shared_module_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from textwrap import dedent

from pytest import Pytester


def test_asyncio_mark_provides_package_scoped_loop_strict_mode(pytester: Pytester):
pytester.makepyfile(
__init__="",
conftest=dedent(
"""\
import pytest_asyncio
@pytest_asyncio.fixture(scope="module")
async def async_shared_module_fixture():
return True
"""
),
test_module_one=dedent(
"""\
import pytest
@pytest.mark.asyncio
async def test_shared_module_fixture_use_a(async_shared_module_fixture):
assert async_shared_module_fixture is True
"""
),
test_module_two=dedent(
"""\
import pytest
@pytest.mark.asyncio
async def test_shared_module_fixture_use_b(async_shared_module_fixture):
assert async_shared_module_fixture is True
"""
),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
9 changes: 1 addition & 8 deletions tests/test_is_async_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from textwrap import dedent

import pytest
from pytest import Pytester


Expand Down Expand Up @@ -74,13 +73,7 @@ def pytest_collection_modifyitems(items):
)
)
result = pytester.runpytest("--asyncio-mode=strict")
if pytest.version_tuple < (7, 2):
# Probably related to https://github.com/pytest-dev/pytest/pull/10012
result.assert_outcomes(failed=1)
elif pytest.version_tuple < (8,):
result.assert_outcomes(skipped=1)
else:
result.assert_outcomes(failed=1)
result.assert_outcomes(failed=1)


def test_returns_true_for_unmarked_coroutine_item_in_auto_mode(pytester: Pytester):
Expand Down