From d153f72e1838aa1629d8a6c5bc1abde5f27b4a1a Mon Sep 17 00:00:00 2001 From: Abdeldjalil Hezouat Date: Fri, 24 Jan 2025 22:44:00 +0100 Subject: [PATCH 1/3] fix: replace deprecated asyncio.get_event_loop() with modern approach Replace deprecated get_event_loop() with get_running_loop() and fallback to new_event_loop() to fix the "no current event loop" warning in Python 3.10+ --- CHANGELOG.rst | 6 ++++++ tortoise/contrib/test/__init__.py | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 46146ca43..2aaf42925 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,12 @@ Changelog 0.24 ==== +0.24.1 +------ +Fixed +^^^^^ +- Fixed asyncio "no current event loop" deprecation warning by replacing `asyncio.get_event_loop()` with modern event loop handling using `get_running_loop()` with fallback to `new_event_loop()` (#1865) + 0.24.0 ------ Fixed diff --git a/tortoise/contrib/test/__init__.py b/tortoise/contrib/test/__init__.py index 9613006b6..07c74a061 100644 --- a/tortoise/contrib/test/__init__.py +++ b/tortoise/contrib/test/__init__.py @@ -126,7 +126,11 @@ def initializer( if db_url is not None: # pragma: nobranch _TORTOISE_TEST_DB = db_url _CONFIG = getDBConfig(app_label=app_label, modules=_MODULES) - loop = loop or asyncio.get_event_loop() + if not loop: + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() _LOOP = loop loop.run_until_complete(_init_db(_CONFIG)) _CONNECTIONS = connections._copy_storage() From 8252e468b0f0ccbca93a4b9c339344f01a545a18 Mon Sep 17 00:00:00 2001 From: Abdeldjalil Hezouat Date: Fri, 24 Jan 2025 22:59:47 +0100 Subject: [PATCH 2/3] add missing line --- tortoise/contrib/test/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tortoise/contrib/test/__init__.py b/tortoise/contrib/test/__init__.py index 07c74a061..774293f81 100644 --- a/tortoise/contrib/test/__init__.py +++ b/tortoise/contrib/test/__init__.py @@ -131,6 +131,7 @@ def initializer( loop = asyncio.get_running_loop() except RuntimeError: loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) _LOOP = loop loop.run_until_complete(_init_db(_CONFIG)) _CONNECTIONS = connections._copy_storage() From 0a7fffdc476f2b95238137e04d4ef99e3e246242 Mon Sep 17 00:00:00 2001 From: Abdeldjalil Hezouat Date: Wed, 29 Jan 2025 08:38:08 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 938a55a40..893ebeda8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ Changelog ------ Fixed ^^^^^ +- Fixed asyncio "no current event loop" deprecation warning by replacing `asyncio.get_event_loop()` with modern event loop handling using `get_running_loop()` with fallback to `new_event_loop()` (#1865) Changed ^^^^^^^ @@ -21,12 +22,6 @@ Changed 0.24 ==== -0.24.1 ------- -Fixed -^^^^^ -- Fixed asyncio "no current event loop" deprecation warning by replacing `asyncio.get_event_loop()` with modern event loop handling using `get_running_loop()` with fallback to `new_event_loop()` (#1865) - 0.24.0 ------ Fixed