Skip to content

Commit 3526a53

Browse files
committed
fix: Python 3.14 get_event_loop_policy() deprecated warn
1 parent 89c79f1 commit 3526a53

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ generally can't be patched.
4545
## Comparison with `nest_asyncio`
4646
`nest-asyncio2` is a fork of the unmaintained [`nest_asyncio`](https://github.com/erdewit/nest_asyncio), with the following changes:
4747
- Python 3.12 `loop_factory` parameter support
48-
- Python 3.14 support (`asyncio.current_task()` and others are broken in `nest_asyncio`)
48+
- Python 3.14 support
49+
- Fix broken `asyncio.current_task()` and others
50+
- Fix `DeprecationWarning: 'asyncio.get_event_loop_policy' is deprecated and slated for removal in Python 3.16`
4951

5052
All interfaces are kept as they are. To migrate, you just need to change the package and module name to `nest_asyncio2`.

nest_asyncio2.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _get_event_loop(stacklevel=3):
8383
if sys.version_info < (3, 7, 0):
8484
asyncio.tasks._current_tasks = asyncio.tasks.Task._current_tasks
8585
asyncio.all_tasks = asyncio.tasks.Task.all_tasks
86-
if sys.version_info >= (3, 9, 0):
86+
# The same as asyncio.get_event_loop() on at least Python 3.14
87+
if sys.version_info >= (3, 9, 0) and sys.version_info < (3, 14, 0):
8788
events._get_event_loop = events.get_event_loop = \
8889
asyncio.get_event_loop = _get_event_loop
8990
asyncio.run = run
@@ -93,14 +94,22 @@ def _get_event_loop(stacklevel=3):
9394
def _patch_policy():
9495
"""Patch the policy to always return a patched loop."""
9596

97+
# Removed in Python 3.16
98+
# https://github.com/python/cpython/issues/127949
99+
if sys.version_info >= (3, 16, 0):
100+
return
101+
96102
def get_event_loop(self):
97103
if self._local._loop is None:
98104
loop = self.new_event_loop()
99105
_patch_loop(loop)
100106
self.set_event_loop(loop)
101107
return self._local._loop
102108

103-
policy = events.get_event_loop_policy()
109+
if sys.version_info < (3, 14, 0):
110+
policy = events.get_event_loop_policy()
111+
else:
112+
policy = events._get_event_loop_policy()
104113
policy.__class__.get_event_loop = get_event_loop
105114

106115

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = nest-asyncio2
3-
version = 1.6.1
3+
version = 1.6.2
44
author = Ewald R. de Wit
55
author_email = [email protected]
66
license = BSD

tests/314_task.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
# [tool.uv.sources]
88
# nest-asyncio2 = { path = "../", editable = true }
99
# ///
10+
import warnings
11+
warnings.filterwarnings("default")
12+
1013
import asyncio
1114
import nest_asyncio2
1215

13-
nest_asyncio2.apply()
16+
with warnings.catch_warnings(record=True) as w:
17+
nest_asyncio2.apply()
18+
assert len(w) == 0, w
1419

1520
async def f():
1621
print(asyncio.get_running_loop())
@@ -28,4 +33,6 @@ async def f():
2833
print(asyncio.tasks._current_tasks)
2934
# assert asyncio.tasks._current_tasks == {}
3035

31-
asyncio.run(f())
36+
with warnings.catch_warnings(record=True) as w:
37+
asyncio.run(f())
38+
assert len(w) == 0, w

tests/314_task_mix.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# [tool.uv.sources]
88
# nest-asyncio2 = { path = "../", editable = true }
99
# ///
10+
import warnings
11+
warnings.filterwarnings("default")
12+
1013
import asyncio
1114
import nest_asyncio2
1215

0 commit comments

Comments
 (0)