fix: Python 3.14 compatibility for deprecated asyncio APIs#3139
Open
daniel7an wants to merge 1 commit intosanic-org:mainfrom
Open
fix: Python 3.14 compatibility for deprecated asyncio APIs#3139daniel7an wants to merge 1 commit intosanic-org:mainfrom
daniel7an wants to merge 1 commit intosanic-org:mainfrom
Conversation
Fixes sanic-org#3132 - Suppress DeprecationWarning for asyncio.get_event_loop_policy() and asyncio.set_event_loop_policy() which are deprecated in Python 3.14 and slated for removal in 3.16 - Replace asyncio.get_event_loop() with asyncio.get_running_loop() in async contexts (create_server, stop, websocket impl) since get_event_loop() no longer creates an implicit event loop in 3.14 - Suppress uvloop's internal use of deprecated asyncio.iscoroutinefunction in add_signal_handler calls
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3139 +/- ##
=============================================
+ Coverage 87.793% 87.800% +0.007%
=============================================
Files 105 105
Lines 8143 8148 +5
Branches 1290 1290
=============================================
+ Hits 7149 7154 +5
- Misses 687 688 +1
+ Partials 307 306 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
Suppression of deprecation warnings is not the right solution. Python 3.16 comes very soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3132
Python 3.14 deprecated several asyncio APIs that Sanic uses, causing
DeprecationWarnings (which become errors with-W error) and, in some code paths,RuntimeErrors due toasyncio.get_event_loop()no longer creating an implicit event loop.Changes
sanic/server/loop.pyasyncio.get_event_loop_policy()andasyncio.set_event_loop_policy()calls inwarnings.catch_warnings()to suppress the deprecation warnings (deprecated in 3.14, removal in 3.16). This also suppresses the cascadingasyncio.AbstractEventLoopPolicydeprecation from uvloop.sanic/mixins/startup.pyget_event_loop()withget_running_loop()increate_server()(async method) andstop()— both run inside an event loop, soget_running_loop()is correct and doesn't trigger the Python 3.14 deprecation/error.sanic/app.pyloopproperty fallback path that usesget_event_loop_policy().get_event_loop().sanic/server/runners.pyasyncio.iscoroutinefunctioninloop.add_signal_handler()calls.sanic/server/websockets/impl.pyasyncio.get_event_loop()withasyncio.get_running_loop()— this code always runs within an active event loop.Testing
test_signals.py,test_signal_handlers.py)python3 -W error::DeprecationWarning— no sanic-originated deprecation warnings remain.