From 007e8ec12662ffd896c6151239dc7ed1402dc710 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Thu, 8 Dec 2022 13:08:38 +0100 Subject: [PATCH] [fix] Prevent DeprecationWarning about existing event loops to bubble up into user code. (#461) Pytest-asyncio fixture setup currently uses `get_event_loop` to clean up loops that don't correspond to the loop returned by the `event_loop` fixture. Starting with CPython 3.10.9 and 3.11.1 the call to get_event_loop emits a DeprecationWarning when function is called, but no event loop exists. This warning bubbles up and shows in test runs of library users. If the users have enabled `-W error` their tests will fail due to a warning in pytest-asyncio. This patch ignores the DeprecationWarning in the fixture setup. This is a temporary solution to restore compatibility with the respective CPython patch releases. Addresses #460 Signed-off-by: Michael Seifert Signed-off-by: Michael Seifert --- CHANGELOG.rst | 5 +++++ pytest_asyncio/plugin.py | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index aa48ad69..e6f80383 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +0.20.3 (22-12-08) +================= +- Prevent DeprecationWarning to bubble up on CPython 3.10.9 and 3.11.1. + `#460 `_ + 0.20.2 (22-11-11) ================= - Fixes an issue with async fixtures that are defined as methods on a test class not being rebound to the actual test instance. `#197 `_ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 18d03673..403d814f 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -399,7 +399,9 @@ def pytest_fixture_setup( loop = outcome.get_result() policy = asyncio.get_event_loop_policy() try: - old_loop = policy.get_event_loop() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + old_loop = policy.get_event_loop() if old_loop is not loop: old_loop.close() except RuntimeError: