Open jgarvin opened 5 months ago
Some further evidence this may be a pytest-asyncio issue is using this decorator instead of pytest.mark.asyncio
works with no warnings/errors:
import inspect
import asyncio
from typing import Callable, Awaitable
def async_test(f: Callable[[], None] | Callable[[], Awaitable[None]]) -> Callable[[], None]:
def new_function() -> None:
nonlocal f
if inspect.iscoroutinefunction(f):
with asyncio.Runner(loop_factory=CustomEventLoop) as runner:
runner.run(f())
else:
f()
return new_function
Your approach seems generally correct. Some points that come to mind:
The latter point could lead to pytest-asyncio creating a fresh event loop which ends up being discarded without closing it. This is just a wild guess, though.
Can you provide a small runnable example that reproduces the error you're seeing?
I was using a
event_loop
fixture to use a custom event loop class, and noticed the warning from pytest to not do that and use an event loop policy instead. However, no matter how I define my policy fixture pytest warns that I didn't close the event loop. The policy usually isn't responsible for closing the event loop though, so I assume this is a pytest bug? Any tips on what I should be doing appreciated.