Skip to content

Commit 7280c02

Browse files
committed
refactor: Extracted function to determine the event loop fixture used to run an async fixture.
1 parent dfabf66 commit 7280c02

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

pytest_asyncio/plugin.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,9 @@ def _wrap_asyncgen_fixture(fixturedef: FixtureDef) -> None:
322322
def _asyncgen_fixture_wrapper(request: FixtureRequest, **kwargs: Any):
323323
unittest = fixturedef.unittest if hasattr(fixturedef, "unittest") else False
324324
func = _perhaps_rebind_fixture_func(fixture, request.instance, unittest)
325-
default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope")
326-
loop_scope = (
327-
getattr(func, "_loop_scope", None) or default_loop_scope or request.scope
325+
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
326+
request, func
328327
)
329-
if loop_scope == "function":
330-
event_loop_fixture_id = "event_loop"
331-
else:
332-
event_loop_node = _retrieve_scope_root(request._pyfuncitem, loop_scope)
333-
event_loop_fixture_id = event_loop_node.stash.get(
334-
# Type ignored because of non-optimal mypy inference.
335-
_event_loop_fixture_id, # type: ignore[arg-type]
336-
"",
337-
)
338-
assert event_loop_fixture_id
339328
event_loop = request.getfixturevalue(event_loop_fixture_id)
340329
kwargs.pop(event_loop_fixture_id, None)
341330
gen_obj = func(**_add_kwargs(func, kwargs, event_loop, request))
@@ -373,20 +362,9 @@ def _wrap_async_fixture(fixturedef: FixtureDef) -> None:
373362
def _async_fixture_wrapper(request: FixtureRequest, **kwargs: Any):
374363
unittest = False if pytest.version_tuple >= (8, 2) else fixturedef.unittest
375364
func = _perhaps_rebind_fixture_func(fixture, request.instance, unittest)
376-
default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope")
377-
loop_scope = (
378-
getattr(func, "_loop_scope", None) or default_loop_scope or request.scope
365+
event_loop_fixture_id = _get_event_loop_fixture_id_for_async_fixture(
366+
request, func
379367
)
380-
if loop_scope == "function":
381-
event_loop_fixture_id = "event_loop"
382-
else:
383-
event_loop_node = _retrieve_scope_root(request._pyfuncitem, loop_scope)
384-
event_loop_fixture_id = event_loop_node.stash.get(
385-
# Type ignored because of non-optimal mypy inference.
386-
_event_loop_fixture_id, # type: ignore[arg-type]
387-
"",
388-
)
389-
assert event_loop_fixture_id
390368
event_loop = request.getfixturevalue(event_loop_fixture_id)
391369
kwargs.pop(event_loop_fixture_id, None)
392370

@@ -399,6 +377,26 @@ async def setup():
399377
fixturedef.func = _async_fixture_wrapper
400378

401379

380+
def _get_event_loop_fixture_id_for_async_fixture(
381+
request: FixtureRequest, func: Any
382+
) -> str:
383+
default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope")
384+
loop_scope = (
385+
getattr(func, "_loop_scope", None) or default_loop_scope or request.scope
386+
)
387+
if loop_scope == "function":
388+
event_loop_fixture_id = "event_loop"
389+
else:
390+
event_loop_node = _retrieve_scope_root(request._pyfuncitem, loop_scope)
391+
event_loop_fixture_id = event_loop_node.stash.get(
392+
# Type ignored because of non-optimal mypy inference.
393+
_event_loop_fixture_id, # type: ignore[arg-type]
394+
"",
395+
)
396+
assert event_loop_fixture_id
397+
return event_loop_fixture_id
398+
399+
402400
class PytestAsyncioFunction(Function):
403401
"""Base class for all test functions managed by pytest-asyncio."""
404402

0 commit comments

Comments
 (0)