Skip to content

Commit 8214743

Browse files
tjkusonseifertm
authored andcommitted
Reinstate event loop if unset
1 parent e83ddae commit 8214743

File tree

4 files changed

+392
-5
lines changed

4 files changed

+392
-5
lines changed

changelog.d/1172.added.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pyright type checking support in tox configuration to improve type safety and compatibility.

changelog.d/1177.fixed.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``RuntimeError: There is no current event loop in thread 'MainThread'`` when using shared event loops after any test unsets the event loop (such as when using ``asyncio.run`` and ``asyncio.Runner``).

pytest_asyncio/plugin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
_ScopeName = Literal["session", "package", "module", "class", "function"]
6868
_R = TypeVar("_R", bound=Union[Awaitable[Any], AsyncIterator[Any]])
6969
_P = ParamSpec("_P")
70+
T = TypeVar("T")
7071
FixtureFunction = Callable[_P, _R]
72+
CoroutineFunction = Callable[_P, Awaitable[T]]
7173

7274

7375
class PytestAsyncioError(Exception):
@@ -311,7 +313,7 @@ def _asyncgen_fixture_wrapper(
311313
gen_obj = fixture_function(*args, **kwargs)
312314

313315
async def setup():
314-
res = await gen_obj.__anext__() # type: ignore[union-attr]
316+
res = await gen_obj.__anext__()
315317
return res
316318

317319
context = contextvars.copy_context()
@@ -324,7 +326,7 @@ def finalizer() -> None:
324326

325327
async def async_finalizer() -> None:
326328
try:
327-
await gen_obj.__anext__() # type: ignore[union-attr]
329+
await gen_obj.__anext__()
328330
except StopAsyncIteration:
329331
pass
330332
else:
@@ -353,8 +355,7 @@ def _wrap_async_fixture(
353355
runner: Runner,
354356
request: FixtureRequest,
355357
) -> Callable[AsyncFixtureParams, AsyncFixtureReturnType]:
356-
357-
@functools.wraps(fixture_function) # type: ignore[arg-type]
358+
@functools.wraps(fixture_function)
358359
def _async_fixture_wrapper(
359360
*args: AsyncFixtureParams.args,
360361
**kwargs: AsyncFixtureParams.kwargs,
@@ -782,7 +783,7 @@ def _get_marked_loop_scope(
782783
return scope
783784

784785

785-
def _get_default_test_loop_scope(config: Config) -> _ScopeName:
786+
def _get_default_test_loop_scope(config: Config) -> Any:
786787
return config.getini("asyncio_default_test_loop_scope")
787788

788789

0 commit comments

Comments
 (0)