Skip to content

Commit 01f24b7

Browse files
committed
fix(tasks): use inspect.iscoroutinefunction for Python 3.14 compatibility
asyncio.iscoroutinefunction is deprecated in Python 3.14 and emits a DeprecationWarning. inspect.iscoroutinefunction is the recommended replacement and is equivalent on Python 3.12+ (asyncio.coroutine was removed in 3.12). Closes #373 Signed-off-by: SAY-5 <say.apm35@gmail.com>
1 parent e9e7e2c commit 01f24b7

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

fastapi_utils/tasks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import inspect
45
import logging
56
import warnings
67
from functools import wraps
@@ -19,15 +20,15 @@
1920

2021

2122
async def _handle_func(func: NoArgsNoReturnAnyFuncT) -> None:
22-
if asyncio.iscoroutinefunction(func):
23+
if inspect.iscoroutinefunction(func):
2324
await func()
2425
else:
2526
await run_in_threadpool(func)
2627

2728

2829
async def _handle_exc(exc: Exception, on_exception: ExcArgNoReturnAnyFuncT | None) -> None:
2930
if on_exception:
30-
if asyncio.iscoroutinefunction(on_exception):
31+
if inspect.iscoroutinefunction(on_exception):
3132
await on_exception(exc)
3233
else:
3334
await run_in_threadpool(on_exception, exc)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Regression tests for issue #373.
2+
3+
The tasks module must use ``inspect.iscoroutinefunction`` rather than
4+
``asyncio.iscoroutinefunction``, which is deprecated in Python 3.14
5+
(``DeprecationWarning``) and scheduled for removal.
6+
7+
See: https://github.com/fastapiutils/fastapi-utils/issues/373
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import inspect
13+
14+
from fastapi_utils import tasks as tasks_module
15+
16+
17+
def test_tasks_module_uses_inspect_iscoroutinefunction() -> None:
18+
source = inspect.getsource(tasks_module)
19+
assert "asyncio.iscoroutinefunction" not in source
20+
assert "inspect.iscoroutinefunction" in source

0 commit comments

Comments
 (0)