Comments (13)
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.
Hi! I encountered same issue
Versions:
- python 3.12
- 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.
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.
@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 in b22d84e
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:
pytest-asyncio/pytest_asyncio/plugin.py
Lines 669 to 674 in 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.
from pytest-asyncio.
Which version of pytest-asyncio are you using?
Can you provide a small example that reproduces the issue?
from pytest-asyncio.
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.
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.
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.
@AivanF could this be an interaction with anyio (do you have @pytest.mark.anyio ?) or any calls to asyncio.run
?
from pytest-asyncio.
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.
@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.
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)
- Support for Pytest 8.0 HOT 14
- pytest-asyncio makes pytest collect modules not matching `python_files`, causing `ModuleNotFoundError` HOT 3
- Startup failure on Windows with Version 0.23.3 HOT 1
- 0.23.3 breaking ImportError exception HOT 1
- documentation on how to force all tests in one session to use the same event loop appears to not force fixtures into the same event loop HOT 4
- TypeError: __call__() got an unexpected keyword argument 'specname' in version 0.23 pytest-asyncio HOT 3
- Hypothesis integration raises internal error when collecting `RuleBasedStateMachine` HOT 2
- AsyncGenerator early exit doesn't raise CancelledError and doesn't run `finally` branch HOT 4
- RuntimeError: "Timeout context manager should be used inside a task" in aiohttp with version 0.23 pytest-asyncio HOT 1
- pytest 8.0.0 AttributeError: 'Package' object has no attribute 'obj' HOT 2
- AttributeError: '_UnixSelectorEventLoop' object has no attribute '_compute_internal_coro' HOT 1
- Drop support for pytest 7
- Factory fixtures are causing `'Coroutine' object is not callable` warning in UI, but tests still run HOT 3
- Configuration option for default loop scope
- Can't async mock a method in a context manager HOT 2
- Parametrizing `event_loop_policy` parametrizes all tests
- Not compatible with --doctest-modules --doctest-ignore-import-errors HOT 1
- How to use event_loop_policy without triggering warnings? HOT 1
- "The test […] is not an async function" when `asyncio` marker is added via `pytest_collection_modifyitems()` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-asyncio.