Skip to content

Commit b68073a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent edfbfef commit b68073a

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

pytest_asyncio/plugin.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
PytestPluginManager,
5050
)
5151

52-
from typing import Callable
5352
if sys.version_info >= (3, 10):
5453
from typing import ParamSpec
5554
else:
@@ -164,7 +163,12 @@ def fixture(
164163

165164
@functools.wraps(fixture)
166165
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+
)
168172

169173
return inner
170174

@@ -174,7 +178,9 @@ def _is_asyncio_fixture_function(obj: Any) -> bool:
174178
return getattr(obj, "_force_asyncio_fixture", False)
175179

176180

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:
178184
if hasattr(obj, "__func__"):
179185
# instance method, check the function object
180186
obj = obj.__func__
@@ -239,7 +245,10 @@ def pytest_report_header(config: Config) -> list[str]:
239245

240246

241247
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],
243252
) -> Callable:
244253
"""Returns a synchronous function evaluating the specified fixture."""
245254
fixture_function = resolve_fixture_function(fixturedef, request)
@@ -261,7 +270,7 @@ def _wrap_asyncgen_fixture(
261270
],
262271
runner: Runner,
263272
request: FixtureRequest,
264-
loop_factory:Callable[[], AbstractEventLoop]
273+
loop_factory: Callable[[], AbstractEventLoop],
265274
) -> Callable[AsyncGenFixtureParams, AsyncGenFixtureYieldType]:
266275
@functools.wraps(fixture_function)
267276
def _asyncgen_fixture_wrapper(
@@ -291,6 +300,7 @@ async def async_finalizer() -> None:
291300
msg = "Async generator fixture didn't stop."
292301
msg += "Yield only once."
293302
raise ValueError(msg)
303+
294304
if loop_factory:
295305
_loop = loop_factory()
296306
asyncio.set_event_loop(_loop)
@@ -315,7 +325,7 @@ def _wrap_async_fixture(
315325
],
316326
runner: Runner,
317327
request: FixtureRequest,
318-
loop_factory: Callable[[], AbstractEventLoop] | None = None
328+
loop_factory: Callable[[], AbstractEventLoop] | None = None,
319329
) -> Callable[AsyncFixtureParams, AsyncFixtureReturnType]:
320330

321331
@functools.wraps(fixture_function) # type: ignore[arg-type]
@@ -435,7 +445,9 @@ def _can_substitute(item: Function) -> bool:
435445

436446
def runtest(self) -> None:
437447
# 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+
)
439451
with MonkeyPatch.context() as c:
440452
c.setattr(self, "obj", synchronized_obj)
441453
super().runtest()
@@ -642,12 +654,13 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
642654

643655
def wrap_in_sync(
644656
func: Callable[..., Awaitable[Any]],
645-
loop_factory:Callable[[], AbstractEventLoop] | None = None
657+
loop_factory: Callable[[], AbstractEventLoop] | None = None,
646658
):
647659
"""
648660
Return a sync wrapper around an async function executing it in the
649661
current event loop.
650662
"""
663+
651664
@functools.wraps(func)
652665
def inner(*args, **kwargs):
653666
_last_loop = asyncio.get_event_loop()
@@ -660,7 +673,7 @@ def inner(*args, **kwargs):
660673
try:
661674
_loop.run_until_complete(task)
662675
except BaseException:
663-
676+
664677
# run_until_complete doesn't get the result from exceptions
665678
# that are not subclasses of `Exception`. Consume all
666679
# exceptions to prevent asyncio's warning from logging.
@@ -669,6 +682,7 @@ def inner(*args, **kwargs):
669682
raise
670683

671684
asyncio.set_event_loop(_last_loop)
685+
672686
return inner
673687

674688

@@ -736,9 +750,12 @@ def _get_marked_loop_scope(
736750
) -> _ScopeName:
737751
assert asyncio_marker.name == "asyncio"
738752
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"}
740755
):
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+
)
742759
if "scope" in asyncio_marker.kwargs:
743760
if "loop_scope" in asyncio_marker.kwargs:
744761
raise pytest.UsageError(_DUPLICATE_LOOP_SCOPE_DEFINITION_ERROR)

0 commit comments

Comments
 (0)