Skip to content

Commit 6c9914b

Browse files
committed
refactor: Fixture synchronizers rely on the caller to pass the correct event loop instance.
1 parent 25d42a8 commit 6c9914b

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

pytest_asyncio/plugin.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
Literal,
2929
TypeVar,
3030
Union,
31-
cast,
3231
overload,
3332
)
3433

@@ -231,12 +230,14 @@ def pytest_report_header(config: Config) -> list[str]:
231230
]
232231

233232

234-
def _fixture_synchronizer(fixturedef: FixtureDef) -> Callable:
233+
def _fixture_synchronizer(
234+
fixturedef: FixtureDef, event_loop: AbstractEventLoop
235+
) -> Callable:
235236
"""Returns a synchronous function evaluating the specified fixture."""
236237
if inspect.isasyncgenfunction(fixturedef.func):
237-
return _wrap_asyncgen_fixture(fixturedef.func)
238+
return _wrap_asyncgen_fixture(fixturedef.func, event_loop)
238239
elif inspect.iscoroutinefunction(fixturedef.func):
239-
return _wrap_async_fixture(fixturedef.func)
240+
return _wrap_async_fixture(fixturedef.func, event_loop)
240241
else:
241242
return fixturedef.func
242243

@@ -277,6 +278,7 @@ def _wrap_asyncgen_fixture(
277278
fixture_function: Callable[
278279
AsyncGenFixtureParams, AsyncGeneratorType[AsyncGenFixtureYieldType, Any]
279280
],
281+
event_loop: AbstractEventLoop,
280282
) -> Callable[
281283
Concatenate[FixtureRequest, AsyncGenFixtureParams], AsyncGenFixtureYieldType
282284
]:
@@ -287,10 +289,6 @@ def _asyncgen_fixture_wrapper(
287289
**kwargs: AsyncGenFixtureParams.kwargs,
288290
):
289291
func = _perhaps_rebind_fixture_func(fixture_function, request.instance)
290-
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
291-
request, func
292-
)
293-
event_loop = request.getfixturevalue(event_loop_fixture_id)
294292
gen_obj = func(*args, **_add_kwargs(func, kwargs, request))
295293

296294
async def setup():
@@ -335,6 +333,7 @@ def _wrap_async_fixture(
335333
fixture_function: Callable[
336334
AsyncFixtureParams, CoroutineType[Any, Any, AsyncFixtureReturnType]
337335
],
336+
event_loop: AbstractEventLoop,
338337
) -> Callable[Concatenate[FixtureRequest, AsyncFixtureParams], AsyncFixtureReturnType]:
339338

340339
@functools.wraps(fixture_function) # type: ignore[arg-type]
@@ -344,10 +343,6 @@ def _async_fixture_wrapper(
344343
**kwargs: AsyncFixtureParams.kwargs,
345344
):
346345
func = _perhaps_rebind_fixture_func(fixture_function, request.instance)
347-
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
348-
request, func
349-
)
350-
event_loop = request.getfixturevalue(event_loop_fixture_id)
351346

352347
async def setup():
353348
res = await func(*args, **_add_kwargs(func, kwargs, request))
@@ -374,18 +369,6 @@ async def setup():
374369
return _async_fixture_wrapper
375370

376371

377-
def _get_event_loop_fixture_id_for_async_fixture(
378-
request: FixtureRequest, func: Any
379-
) -> str:
380-
default_loop_scope = cast(
381-
_ScopeName, request.config.getini("asyncio_default_fixture_loop_scope")
382-
)
383-
loop_scope = (
384-
getattr(func, "_loop_scope", None) or default_loop_scope or request.scope
385-
)
386-
return f"_{loop_scope}_event_loop"
387-
388-
389372
def _create_task_in_context(
390373
loop: asyncio.AbstractEventLoop,
391374
coro: AbstractCoroutine[Any, Any, _T],
@@ -794,7 +777,9 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
794777
or default_loop_scope
795778
or fixturedef.scope
796779
)
797-
synchronizer = _fixture_synchronizer(fixturedef)
780+
event_loop_fixture_id = f"_{loop_scope}_event_loop"
781+
event_loop = request.getfixturevalue(event_loop_fixture_id)
782+
synchronizer = _fixture_synchronizer(fixturedef, event_loop)
798783
_make_asyncio_fixture_function(synchronizer, loop_scope)
799784
with MonkeyPatch.context() as c:
800785
if "request" not in fixturedef.argnames:

0 commit comments

Comments
 (0)