Skip to content

Commit 2cd541e

Browse files
committed
Revert "[feat!] Async fixtures can no longer request the *event_loop* fixture."
This reverts commit 201f6ec.
1 parent 201f6ec commit 2cd541e

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

docs/source/reference/changelog.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
Changelog
33
=========
44

5-
0.23.0 (UNRELEASED)
6-
===================
7-
- BREAKING: Async fixtures can no longer request the *event_loop* fixture. Use *asyncio.get_running_loop()* instead.
8-
95
0.22.0 (2023-10-31)
106
===================
117
- Class-scoped and module-scoped event loops can be requested

pytest_asyncio/plugin.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,16 @@ def _synchronize_async_fixture(
269269
def _add_kwargs(
270270
func: Callable[..., Any],
271271
kwargs: Dict[str, Any],
272+
event_loop_fixture_id: str,
273+
event_loop: asyncio.AbstractEventLoop,
272274
request: SubRequest,
273275
) -> Dict[str, Any]:
274276
sig = inspect.signature(func)
275277
ret = kwargs.copy()
276278
if "request" in sig.parameters:
277279
ret["request"] = request
280+
if event_loop_fixture_id in sig.parameters:
281+
ret[event_loop_fixture_id] = event_loop
278282
return ret
279283

280284

@@ -305,9 +309,10 @@ def _asyncgen_fixture_wrapper(request: SubRequest, **kwargs: Any):
305309
func = _perhaps_rebind_fixture_func(
306310
fixture, request.instance, fixturedef.unittest
307311
)
308-
kwargs.pop(event_loop_fixture_id)
309-
event_loop = asyncio.get_event_loop()
310-
gen_obj = func(**_add_kwargs(func, kwargs, request))
312+
event_loop = kwargs.pop(event_loop_fixture_id)
313+
gen_obj = func(
314+
**_add_kwargs(func, kwargs, event_loop_fixture_id, event_loop, request)
315+
)
311316

312317
async def setup():
313318
res = await gen_obj.__anext__()
@@ -343,11 +348,12 @@ def _async_fixture_wrapper(request: SubRequest, **kwargs: Any):
343348
func = _perhaps_rebind_fixture_func(
344349
fixture, request.instance, fixturedef.unittest
345350
)
346-
kwargs.pop(event_loop_fixture_id)
347-
event_loop = asyncio.get_event_loop()
351+
event_loop = kwargs.pop(event_loop_fixture_id)
348352

349353
async def setup():
350-
res = await func(**_add_kwargs(func, kwargs, request))
354+
res = await func(
355+
**_add_kwargs(func, kwargs, event_loop_fixture_id, event_loop, request)
356+
)
351357
return res
352358

353359
return event_loop.run_until_complete(setup())

tests/test_explicit_event_loop_fixture_request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def test_uses_fixture(emits_warning):
8989
)
9090
)
9191
result = pytester.runpytest("--asyncio-mode=strict")
92-
result.assert_outcomes(errors=1, warnings=1)
92+
result.assert_outcomes(passed=1, warnings=1)
9393
result.stdout.fnmatch_lines(
9494
['*is asynchronous and explicitly requests the "event_loop" fixture*']
9595
)
@@ -115,7 +115,7 @@ async def test_uses_fixture(emits_warning):
115115
)
116116
)
117117
result = pytester.runpytest("--asyncio-mode=strict")
118-
result.assert_outcomes(errors=1, warnings=1)
118+
result.assert_outcomes(passed=1, warnings=1)
119119
result.stdout.fnmatch_lines(
120120
['*is asynchronous and explicitly requests the "event_loop" fixture*']
121121
)

0 commit comments

Comments
 (0)