28
28
Literal ,
29
29
TypeVar ,
30
30
Union ,
31
- cast ,
32
31
overload ,
33
32
)
34
33
@@ -231,12 +230,14 @@ def pytest_report_header(config: Config) -> list[str]:
231
230
]
232
231
233
232
234
- def _fixture_synchronizer (fixturedef : FixtureDef ) -> Callable :
233
+ def _fixture_synchronizer (
234
+ fixturedef : FixtureDef , event_loop : AbstractEventLoop
235
+ ) -> Callable :
235
236
"""Returns a synchronous function evaluating the specified fixture."""
236
237
if inspect .isasyncgenfunction (fixturedef .func ):
237
- return _wrap_asyncgen_fixture (fixturedef .func )
238
+ return _wrap_asyncgen_fixture (fixturedef .func , event_loop )
238
239
elif inspect .iscoroutinefunction (fixturedef .func ):
239
- return _wrap_async_fixture (fixturedef .func )
240
+ return _wrap_async_fixture (fixturedef .func , event_loop )
240
241
else :
241
242
return fixturedef .func
242
243
@@ -277,6 +278,7 @@ def _wrap_asyncgen_fixture(
277
278
fixture_function : Callable [
278
279
AsyncGenFixtureParams , AsyncGeneratorType [AsyncGenFixtureYieldType , Any ]
279
280
],
281
+ event_loop : AbstractEventLoop ,
280
282
) -> Callable [
281
283
Concatenate [FixtureRequest , AsyncGenFixtureParams ], AsyncGenFixtureYieldType
282
284
]:
@@ -287,10 +289,6 @@ def _asyncgen_fixture_wrapper(
287
289
** kwargs : AsyncGenFixtureParams .kwargs ,
288
290
):
289
291
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 )
294
292
gen_obj = func (* args , ** _add_kwargs (func , kwargs , request ))
295
293
296
294
async def setup ():
@@ -335,6 +333,7 @@ def _wrap_async_fixture(
335
333
fixture_function : Callable [
336
334
AsyncFixtureParams , CoroutineType [Any , Any , AsyncFixtureReturnType ]
337
335
],
336
+ event_loop : AbstractEventLoop ,
338
337
) -> Callable [Concatenate [FixtureRequest , AsyncFixtureParams ], AsyncFixtureReturnType ]:
339
338
340
339
@functools .wraps (fixture_function ) # type: ignore[arg-type]
@@ -344,10 +343,6 @@ def _async_fixture_wrapper(
344
343
** kwargs : AsyncFixtureParams .kwargs ,
345
344
):
346
345
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 )
351
346
352
347
async def setup ():
353
348
res = await func (* args , ** _add_kwargs (func , kwargs , request ))
@@ -374,18 +369,6 @@ async def setup():
374
369
return _async_fixture_wrapper
375
370
376
371
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
-
389
372
def _create_task_in_context (
390
373
loop : asyncio .AbstractEventLoop ,
391
374
coro : AbstractCoroutine [Any , Any , _T ],
@@ -794,7 +777,9 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
794
777
or default_loop_scope
795
778
or fixturedef .scope
796
779
)
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 )
798
783
_make_asyncio_fixture_function (synchronizer , loop_scope )
799
784
with MonkeyPatch .context () as c :
800
785
if "request" not in fixturedef .argnames :
0 commit comments