From d5fcc8392212f7e67d7aa43d233f1157823f32ba Mon Sep 17 00:00:00 2001 From: xxxigm Date: Thu, 7 May 2026 20:55:59 +0700 Subject: [PATCH] fix(tests): avoid asyncio DeprecationWarning in event loop fixture on 3.12+ --- tests/conftest.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index f9ad9d9b2b..4fc15fd1e0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -483,15 +483,26 @@ def _ensure_current_event_loop(request): A number of gateway tests still use asyncio.get_event_loop().run_until_complete(...). Ensure they always have a usable loop without interfering with pytest-asyncio's own loop management for @pytest.mark.asyncio tests. + + On Python 3.12+, ``asyncio.get_event_loop_policy().get_event_loop()`` with no + *running* loop emits DeprecationWarning; skip that path and install a fresh + loop via ``new_event_loop()`` instead. """ if request.node.get_closest_marker("asyncio") is not None: yield return + loop = None try: - loop = asyncio.get_event_loop_policy().get_event_loop() + loop = asyncio.get_running_loop() except RuntimeError: - loop = None + pass + + if loop is None and sys.version_info < (3, 12): + try: + loop = asyncio.get_event_loop_policy().get_event_loop() + except RuntimeError: + loop = None created = loop is None or loop.is_closed() if created: