From c2a83bb41ca6ffd964854d0fe9c6014e5356ad2c Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Fri, 17 Jan 2025 14:57:03 +0100 Subject: [PATCH] Switch from asyncio.iscoroutinefunction to inspect for Py 3.14+. The former causes: DeprecationWarning: 'asyncio.iscoroutinefunction' is deprecated and slated for removal in Python 3.16; use inspect.iscoroutinefunction() instead. Fixes: https://github.com/aio-libs/async-lru/issues/635 --- async_lru/__init__.py | 8 +++++++- tests/test_basic.py | 9 +++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/async_lru/__init__.py b/async_lru/__init__.py index 228ebad..9b88faf 100644 --- a/async_lru/__init__.py +++ b/async_lru/__init__.py @@ -27,6 +27,12 @@ else: from typing_extensions import Self +if sys.version_info >= (3, 14): + import inspect + iscoroutinefunction = inspect.iscoroutinefunction +else: + iscoroutinefunction = asyncio.iscoroutinefunction + __version__ = "2.0.4" @@ -299,7 +305,7 @@ def wrapper(fn: _CBP[_R]) -> _LRUCacheWrapper[_R]: while isinstance(origin, (partial, partialmethod)): origin = origin.func - if not asyncio.iscoroutinefunction(origin): + if not iscoroutinefunction(origin): raise RuntimeError(f"Coroutine function is required, got {fn!r}") # functools.partialmethod support diff --git a/tests/test_basic.py b/tests/test_basic.py index acc57a2..3898f49 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,4 +1,5 @@ import asyncio +import inspect import platform import sys from functools import _CacheInfo, partial @@ -6,7 +7,7 @@ import pytest -from async_lru import _CacheParameters, alru_cache +from async_lru import _CacheParameters, alru_cache, iscoroutinefunction def test_alru_cache_not_callable() -> None: @@ -27,7 +28,7 @@ async def test_alru_cache_deco(check_lru: Callable[..., None]) -> None: async def coro() -> None: pass - assert asyncio.iscoroutinefunction(coro) + assert iscoroutinefunction(coro) check_lru(coro, hits=0, misses=0, cache=0, tasks=0) @@ -41,7 +42,7 @@ async def test_alru_cache_deco_called(check_lru: Callable[..., None]) -> None: async def coro() -> None: pass - assert asyncio.iscoroutinefunction(coro) + assert iscoroutinefunction(coro) check_lru(coro, hits=0, misses=0, cache=0, tasks=0) @@ -56,7 +57,7 @@ async def coro() -> None: coro_wrapped = alru_cache(coro) - assert asyncio.iscoroutinefunction(coro_wrapped) + assert iscoroutinefunction(coro_wrapped) check_lru(coro_wrapped, hits=0, misses=0, cache=0, tasks=0)