添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When running the test suite on Python 3.11, tests with a @pytest.mark.asyncio decorator, trigger a DeprecationWarning:

.../python3.11/site-packages/pytest_asyncio/plugin.py:884: DeprecationWarning: There is no current event loop
   _loop = asyncio.get_event_loop()

Here's traceback

  /python3.12/site-packages/pytest_asyncio/plugin.py:656: DeprecationWarning: There is no current event loop
    old_loop = asyncio.get_event_loop()

I think this only happens on the older version of python?

After upgrading from python 3.11.0rc1 to 3.11.7, the warning no longer shows up.
It also doesn't show up on 3.12.1 for me.

platform win32 -- Python 3.11.7, pytest-7.4.4, pluggy-1.4.0
platform win32 -- Python 3.12.1, pytest-7.4.4, pluggy-1.4.0
plugins: asyncio-0.23.4
asyncio: mode=Mode.STRICT

The warning did shows up on older version of python before I updated it.

Which version of pytest-asyncio are you using?

platform win32 -- Python 3.11.0rc1, pytest-7.4.4, pluggy-1.4.0
plugins: asyncio-0.23.4
asyncio: mode=Mode.STRICT

Can you provide a small example that reproduces the issue?

import pytest
@pytest.mark.asyncio
async def test():

The docs has a footnote on the warning, so it would probably only show up on 3.10.0–3.10.8 and 3.11.0:
https://docs.python.org/3.11/library/asyncio-eventloop.html#asyncio.get_event_loop

Note In Python versions 3.10.0–3.10.8 and 3.11.0 this function (and other functions which use it implicitly) emitted a DeprecationWarning if there was no running event loop, even if the current loop was set on the policy. In Python versions 3.10.9, 3.11.1 and 3.12 they emit a DeprecationWarning if there is no running event loop and no current loop is set. In some future Python release this will become an error.

When using asyncio_mode = auto I still see DeprecationWarning on Python 3.12.1, regardless whether the marker decorator is present or not. Switching to strict does resolve it, but that is not something I'd like to do, since none of our tests or fixtures are decorated. Is there something I'm missing that needs to be done when using auto mode to avoid this? Seeing that Python documentation states that in future versions this warning might become an error that is not something I'd like to ignore.

I used the same test that @asl97 provided above.

platform linux -- Python 3.12.1, pytest-7.4.4, pluggy-1.4.0
plugins: asyncio-0.23.4
asyncio: mode=Mode.AUTO
collected 1 item
tests/test_deprecation_warning.py::test PASSED                                                                                                                                                                                     [100%]
============================================================================================================ warnings summary ============================================================================================================
tests/test_deprecation_warning.py::test
  /opt/app-root/venv/lib/python3.12/site-packages/pytest_asyncio/plugin.py:656: DeprecationWarning: There is no current event loop
    old_loop = asyncio.get_event_loop()
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

pytest==8.0.0
pytest-asyncio==0.23.5a0

the warning no longer shows up when i remove my async fixture however that I use to init my database:

@pytest_asyncio.fixture(scope="session", autouse=True)
async def init_db(patch_get_settings):
    from ops_center.persistence import engine
    async with engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)
    yield

Alternatively the warning also disappears once I no longer specify:

@pytest.fixture(scope="session")
def event_loop_policy():
    return uvloop.EventLoopPolicy()
          

Just tried out the latest, unfortunately, seems like the problem still persists:

===================================================== test session starts ======================================================
platform darwin -- Python 3.10.7, pytest-7.4.3, pluggy-1.0.0
Using --randomly-seed=724078419
rootdir: /Users/aivanf/MyProject
configfile: pyproject.toml
plugins: asyncio-0.23.5, cov-4.1.0, anyio-4.1.0
asyncio: mode=auto
collected 10 items                                                                                                             
tests/test_mycode.py ......                                                                              [ 80%]
======================================================= warnings summary =======================================================
tests/test_mycode.py::test_functions
  /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pytest_asyncio/plugin.py:904: DeprecationWarning: There is no current event loop
    _loop = asyncio.get_event_loop()
          

We have the same problem in our project.

platform linux -- Python 3.10.6, pytest-8.1.0, pluggy-1.4.0
rootdir: /app
configfile: pytest.ini
plugins: asyncio-0.23.5, requests-mock-1.11.0, cov-4.1.0
asyncio: mode=strict
[...]
self = <Coroutine test_async_foo>
    def runtest(self) -> None:
        self.obj = wrap_in_sync(
            # https://github.com/pytest-dev/pytest-asyncio/issues/596
            self.obj,  # type: ignore[has-type]
>       super().runtest()
/usr/local/lib/python3.10/dist-packages/pytest_asyncio/plugin.py:440: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = (), kwargs = {}, coro = <coroutine object test_async_foo at 0x7537939a0c80>
    @functools.wraps(func)
    def inner(*args, **kwargs):
        coro = func(*args, **kwargs)
>       _loop = asyncio.get_event_loop()
E       DeprecationWarning: There is no current event loop
/usr/local/lib/python3.10/dist-packages/pytest_asyncio/plugin.py:904: DeprecationWarning

The test function looks like this (minimal test that doesn't use anything from the rest of the project):

@pytest.mark.asyncio
async def test_async_foo():
    async def foo():
        return 42
    assert await foo() == 42

@asl97 Excellent question

The way the deprecation of asyncio.get_event_loop was handled is unfortunate, because the function is deprecated in some patch releases and not deprecated in others. However, get_event_loop is definitely deprecated in 3.12 and beyond.

The stack trace suggests that the line 904 of the following function triggers the warning:
pytest-asyncio/pytest_asyncio/plugin.py Lines 901 to 914 b22d84e

I'd be grateful to anyone who contributes a patch with an accompanying regression test added to the test suite.

If I recall correctly, there are a number of places where get_event_loop is called with the pattern illustrated in the lattes example. In that case, it would be even better if the patch deduplicated the code by extracting the above snippet to a dedicated function.

FYI: Currently you can suppress this warning with pytest filterwarnings option as a workaround:

# in pyproject.toml, or you can configure it in pytest.ini
[tool.pytest.ini_options]
filterwarnings = [
    # This's triggered by pytest-asyncio,
    # which calls deprecated asyncio.get_event_loop() method
    # ref: https://github.com/pytest-dev/pytest-asyncio/issues/757
    "ignore:There is no current event loop",

This workaround suits me since I'll never manually create an event loop in my program, thus the option will not blindly suppress warnings generated from my code.

@algebraist Can you provide a minimal reproducer, so we can verify if this and your issue have the same cause?

@seifertm I think my issue may be different actually. I trigger the deprecation warning when I have a single line in a python script:

import ib_async

I was running pytest on files that were importing ib_async, and that was likely triggering the deprecation. The ib_async maintainers are aware of my issue. Thanks for responding!