Skip to content
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

Fix compatibility with pytest 8.2 FixtureDef.unittest removal #800

Merged
merged 4 commits into from
Mar 19, 2024
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
9 changes: 9 additions & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
Changelog
=========

0.23.6 (2024-03-19)
===================
- Fix compatibility with pytest 8.2 `#800 <https://github.com/pytest-dev/pytest-asyncio/pull/800>`_

Known issues
------------
As of v0.23, pytest-asyncio attaches an asyncio event loop to each item of the test suite (i.e. session, packages, modules, classes, functions) and allows tests to be run in those loops when marked accordingly. Pytest-asyncio currently assumes that async fixture scope is correlated with the new event loop scope. This prevents fixtures from being evaluated independently from the event loop scope and breaks some existing test suites (see `#706`_). For example, a test suite may require all fixtures and tests to run in the same event loop, but have async fixtures that are set up and torn down for each module. If you're affected by this issue, please continue using the v0.21 release, until it is resolved.


0.23.5 (2024-02-09)
===================
- Declare compatibility with pytest 8 `#737 <https://github.com/pytest-dev/pytest-asyncio/issues/737>`_
Expand Down
24 changes: 7 additions & 17 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import functools
import inspect
import socket
import sys
import warnings
from asyncio import AbstractEventLoopPolicy
from textwrap import dedent
Expand Down Expand Up @@ -64,7 +63,6 @@

# https://github.com/pytest-dev/pytest/pull/9510
FixtureDef = Any
SubRequest = Any


class PytestAsyncioError(Exception):
Expand Down Expand Up @@ -282,7 +280,7 @@ def _add_kwargs(
kwargs: Dict[str, Any],
event_loop_fixture_id: str,
event_loop: asyncio.AbstractEventLoop,
request: SubRequest,
request: FixtureRequest,
) -> Dict[str, Any]:
sig = inspect.signature(func)
ret = kwargs.copy()
Expand Down Expand Up @@ -316,10 +314,9 @@ def _wrap_asyncgen_fixture(fixturedef: FixtureDef, event_loop_fixture_id: str) -
fixture = fixturedef.func

@functools.wraps(fixture)
def _asyncgen_fixture_wrapper(request: SubRequest, **kwargs: Any):
func = _perhaps_rebind_fixture_func(
fixture, request.instance, fixturedef.unittest
)
def _asyncgen_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)
gen_obj = func(
**_add_kwargs(func, kwargs, event_loop_fixture_id, event_loop, request)
Expand Down Expand Up @@ -355,10 +352,9 @@ def _wrap_async_fixture(fixturedef: FixtureDef, event_loop_fixture_id: str) -> N
fixture = fixturedef.func

@functools.wraps(fixture)
def _async_fixture_wrapper(request: SubRequest, **kwargs: Any):
func = _perhaps_rebind_fixture_func(
fixture, request.instance, fixturedef.unittest
)
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)

async def setup():
Expand Down Expand Up @@ -657,12 +653,6 @@ def _patched_collect():
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop


def _removesuffix(s: str, suffix: str) -> str:
if sys.version_info < (3, 9):
return s[: -len(suffix)]
return s.removesuffix(suffix)


@contextlib.contextmanager
def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[None]:
old_loop_policy = asyncio.get_event_loop_policy()
Expand Down
Loading