Coder Social home page Coder Social logo

Comments (13)

AivanF avatar AivanF commented on May 28, 2024 4

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()

from pytest-asyncio.

CherrySuryp avatar CherrySuryp commented on May 28, 2024 3

Hi! I encountered same issue

Versions:

  1. python 3.12
  2. pytest-asyncio 0.23.4

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()

from pytest-asyncio.

asl97 avatar asl97 commented on May 28, 2024 1

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():
    pass

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.

from pytest-asyncio.

seifertm avatar seifertm commented on May 28, 2024 1

@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:

@functools.wraps(func)
def inner(*args, **kwargs):
coro = func(*args, **kwargs)
_loop = asyncio.get_event_loop()
task = asyncio.ensure_future(coro, loop=_loop)
try:
_loop.run_until_complete(task)
except BaseException:
# run_until_complete doesn't get the result from exceptions
# that are not subclasses of `Exception`. Consume all
# exceptions to prevent asyncio's warning from logging.
if task.done() and not task.cancelled():
task.exception()
raise

This is the reason why I reopened the issue.

Until pytest-asyncio moves away from the use of asyncio.get_event_loop(), which requires breaking changes, all such call from pytest-asyncio to get_event_loop should ignore the warning as in the following example:

try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
old_loop = asyncio.get_event_loop()
except RuntimeError:
old_loop = None

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.

from pytest-asyncio.

seifertm avatar seifertm commented on May 28, 2024

Which version of pytest-asyncio are you using?
Can you provide a small example that reproduces the issue?

from pytest-asyncio.

michaljelonek avatar michaljelonek commented on May 28, 2024

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

from pytest-asyncio.

ingwinlu avatar ingwinlu commented on May 28, 2024

I also still see it with 3.12.1 and strict mode:

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()

from pytest-asyncio.

seifertm avatar seifertm commented on May 28, 2024

Pytest-asyncio still uses asyncio.get_event_loop(). The DeprecationWarning could be triggered by the first asyncio test in a test suite. The warning was silenced in pytest-asyncio-0.23.5, so that it doesn't affect user code.

from pytest-asyncio.

graingert avatar graingert commented on May 28, 2024

@AivanF could this be an interaction with anyio (do you have @pytest.mark.anyio ?) or any calls to asyncio.run?

from pytest-asyncio.

binaryDiv avatar binaryDiv commented on May 28, 2024

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

from pytest-asyncio.

asl97 avatar asl97 commented on May 28, 2024

@AivanF @binaryDiv both your versions are older than 3.10.9.

@seifertm should this issue remain open just for issue on older python version which explicitly mentions would give an warning in the docs, and which would probably go away as soon as they update to a newer 3.10?

from pytest-asyncio.

boholder avatar boholder commented on May 28, 2024

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.

from pytest-asyncio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.