49
49
PytestPluginManager ,
50
50
)
51
51
52
- from typing import Callable
53
52
if sys .version_info >= (3 , 10 ):
54
53
from typing import ParamSpec
55
54
else :
@@ -164,7 +163,12 @@ def fixture(
164
163
165
164
@functools .wraps (fixture )
166
165
def inner (fixture_function : FixtureFunction [_P , _R ]) -> FixtureFunction [_P , _R ]:
167
- return fixture (fixture_function , loop_factory = loop_factory , loop_scope = loop_scope , ** kwargs )
166
+ return fixture (
167
+ fixture_function ,
168
+ loop_factory = loop_factory ,
169
+ loop_scope = loop_scope ,
170
+ ** kwargs ,
171
+ )
168
172
169
173
return inner
170
174
@@ -174,7 +178,9 @@ def _is_asyncio_fixture_function(obj: Any) -> bool:
174
178
return getattr (obj , "_force_asyncio_fixture" , False )
175
179
176
180
177
- def _make_asyncio_fixture_function (obj : Any , loop_scope : _ScopeName | None , loop_factory : _ScopeName | None ) -> None :
181
+ def _make_asyncio_fixture_function (
182
+ obj : Any , loop_scope : _ScopeName | None , loop_factory : _ScopeName | None
183
+ ) -> None :
178
184
if hasattr (obj , "__func__" ):
179
185
# instance method, check the function object
180
186
obj = obj .__func__
@@ -239,7 +245,10 @@ def pytest_report_header(config: Config) -> list[str]:
239
245
240
246
241
247
def _fixture_synchronizer (
242
- fixturedef : FixtureDef , runner : Runner , request : FixtureRequest , loop_factory : Callable [[], AbstractEventLoop ]
248
+ fixturedef : FixtureDef ,
249
+ runner : Runner ,
250
+ request : FixtureRequest ,
251
+ loop_factory : Callable [[], AbstractEventLoop ],
243
252
) -> Callable :
244
253
"""Returns a synchronous function evaluating the specified fixture."""
245
254
fixture_function = resolve_fixture_function (fixturedef , request )
@@ -261,7 +270,7 @@ def _wrap_asyncgen_fixture(
261
270
],
262
271
runner : Runner ,
263
272
request : FixtureRequest ,
264
- loop_factory :Callable [[], AbstractEventLoop ]
273
+ loop_factory : Callable [[], AbstractEventLoop ],
265
274
) -> Callable [AsyncGenFixtureParams , AsyncGenFixtureYieldType ]:
266
275
@functools .wraps (fixture_function )
267
276
def _asyncgen_fixture_wrapper (
@@ -291,6 +300,7 @@ async def async_finalizer() -> None:
291
300
msg = "Async generator fixture didn't stop."
292
301
msg += "Yield only once."
293
302
raise ValueError (msg )
303
+
294
304
if loop_factory :
295
305
_loop = loop_factory ()
296
306
asyncio .set_event_loop (_loop )
@@ -315,7 +325,7 @@ def _wrap_async_fixture(
315
325
],
316
326
runner : Runner ,
317
327
request : FixtureRequest ,
318
- loop_factory : Callable [[], AbstractEventLoop ] | None = None
328
+ loop_factory : Callable [[], AbstractEventLoop ] | None = None ,
319
329
) -> Callable [AsyncFixtureParams , AsyncFixtureReturnType ]:
320
330
321
331
@functools .wraps (fixture_function ) # type: ignore[arg-type]
@@ -435,7 +445,9 @@ def _can_substitute(item: Function) -> bool:
435
445
436
446
def runtest (self ) -> None :
437
447
# print(self.obj.pytestmark[0].__dict__)
438
- synchronized_obj = wrap_in_sync (self .obj , self .obj .pytestmark [0 ].kwargs .get ('loop_factory' , None ))
448
+ synchronized_obj = wrap_in_sync (
449
+ self .obj , self .obj .pytestmark [0 ].kwargs .get ("loop_factory" , None )
450
+ )
439
451
with MonkeyPatch .context () as c :
440
452
c .setattr (self , "obj" , synchronized_obj )
441
453
super ().runtest ()
@@ -642,12 +654,13 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
642
654
643
655
def wrap_in_sync (
644
656
func : Callable [..., Awaitable [Any ]],
645
- loop_factory :Callable [[], AbstractEventLoop ] | None = None
657
+ loop_factory : Callable [[], AbstractEventLoop ] | None = None ,
646
658
):
647
659
"""
648
660
Return a sync wrapper around an async function executing it in the
649
661
current event loop.
650
662
"""
663
+
651
664
@functools .wraps (func )
652
665
def inner (* args , ** kwargs ):
653
666
_last_loop = asyncio .get_event_loop ()
@@ -660,7 +673,7 @@ def inner(*args, **kwargs):
660
673
try :
661
674
_loop .run_until_complete (task )
662
675
except BaseException :
663
-
676
+
664
677
# run_until_complete doesn't get the result from exceptions
665
678
# that are not subclasses of `Exception`. Consume all
666
679
# exceptions to prevent asyncio's warning from logging.
@@ -669,6 +682,7 @@ def inner(*args, **kwargs):
669
682
raise
670
683
671
684
asyncio .set_event_loop (_last_loop )
685
+
672
686
return inner
673
687
674
688
@@ -736,9 +750,12 @@ def _get_marked_loop_scope(
736
750
) -> _ScopeName :
737
751
assert asyncio_marker .name == "asyncio"
738
752
if asyncio_marker .args or (
739
- asyncio_marker .kwargs and set (asyncio_marker .kwargs ) - {"loop_scope" , "scope" , "loop_factory" }
753
+ asyncio_marker .kwargs
754
+ and set (asyncio_marker .kwargs ) - {"loop_scope" , "scope" , "loop_factory" }
740
755
):
741
- raise ValueError ("mark.asyncio accepts only a keyword arguments 'loop_scope' or 'loop_factory'" )
756
+ raise ValueError (
757
+ "mark.asyncio accepts only a keyword arguments 'loop_scope' or 'loop_factory'"
758
+ )
742
759
if "scope" in asyncio_marker .kwargs :
743
760
if "loop_scope" in asyncio_marker .kwargs :
744
761
raise pytest .UsageError (_DUPLICATE_LOOP_SCOPE_DEFINITION_ERROR )
0 commit comments