Skip to content

fix: suppress Python 3.14 deprecation warnings for asyncio policy API (#3129) - #3146

Open
r266-tech wants to merge 2 commits into
sanic-org:mainfrom
r266-tech:fix/py314-deprecation-warnings
Open

fix: suppress Python 3.14 deprecation warnings for asyncio policy API (#3129)#3146
r266-tech wants to merge 2 commits into
sanic-org:mainfrom
r266-tech:fix/py314-deprecation-warnings

Conversation

@r266-tech

@r266-tech r266-tech commented Mar 22, 2026

Copy link
Copy Markdown

Description

Fixes #3129

Python 3.14 deprecates asyncio.get_event_loop_policy() and asyncio.set_event_loop_policy() with DeprecationWarning. These APIs are still the recommended way to configure uvloop and Windows event loop policies, so the fix wraps calls in warnings.catch_warnings() on Python >= 3.14 to suppress warnings originating from the asyncio module.

Changes

  • sanic/server/loop.py: Wrapped event loop policy checks in warnings.catch_warnings() blocks for both try_use_uvloop() and try_windows_loop() functions

Testing

On Python 3.14, running Sanic with uvloop should no longer emit:

DeprecationWarning: 'asyncio.get_event_loop_policy' is deprecated and slated for removal in Python 3.16
DeprecationWarning: 'asyncio.set_event_loop_policy' is deprecated and slated for removal in Python 3.16

The pattern is intentionally kept simple — once Python 3.16 provides a loop_factory API, the warnings.catch_warnings wrapper can be replaced with direct set_event_loop_factory() calls.

@r266-tech
r266-tech requested a review from a team as a code owner March 22, 2026 18:48
@r266-tech
r266-tech force-pushed the fix/py314-deprecation-warnings branch from e56022e to 759a319 Compare March 22, 2026 19:18
@codecov

codecov Bot commented Mar 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 46.15385% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.722%. Comparing base (5ffc7b3) to head (4cdeaaf).

Files with missing lines Patch % Lines
sanic/server/loop.py 46.153% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              main     #3146       +/-   ##
=============================================
- Coverage   87.769%   87.722%   -0.048%     
=============================================
  Files          105       105               
  Lines         8152      8161        +9     
  Branches      1291      1293        +2     
=============================================
+ Hits          7155      7159        +4     
- Misses         692       696        +4     
- Partials       305       306        +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module=asyncio in warnings.filterwarnings hides every DeprecationWarning from asyncio inside try_use_uvloop and try_windows_loop, not only the policy call. nit: narrow with message= if tests still pass.

openclaw-bot added 2 commits June 10, 2026 16:08
… policy

Python 3.14 deprecates asyncio.get_event_loop_policy() and
asyncio.set_event_loop_policy() with DeprecationWarning. These are
still the recommended way to configure uvloop and Windows event loop
policies, so we suppress the warnings on Python >= 3.14 where they
originate from the asyncio module.

Fixes sanic-org#3129
Per review feedback, match only the event loop policy deprecation
messages instead of suppressing every DeprecationWarning raised from
the asyncio module.
@r266-tech
r266-tech force-pushed the fix/py314-deprecation-warnings branch from 759a319 to 4cdeaaf Compare June 10, 2026 08:12
@r266-tech

Copy link
Copy Markdown
Author

Good catch, thanks. The filter is now narrowed to message= so it only matches the policy-API deprecation messages (get/set_event_loop_policy and the *EventLoopPolicy class aliases) instead of every DeprecationWarning raised from asyncio.

Verified on CPython 3.14.4: the policy warnings are suppressed — including asyncio.AbstractEventLoopPolicy, which uvloop's policy instantiation touches — while unrelated asyncio deprecations (e.g. asyncio.iscoroutinefunction) still surface. tests/test_server_loop.py passes.

Also rebased onto current main: the earlier red CI came from a bandit-baseline mismatch in the security env (pre-existing B105 findings unrelated to this diff), which is resolved on main now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python 3.14 deprecation warnings for asyncio.get_event_loop_policy still present in 25.12.0

2 participants