Skip to content

Commit

Permalink
Switch from asyncio.iscoroutinefunction to inspect for Py 3.14+.
Browse files Browse the repository at this point in the history
The former causes: DeprecationWarning:
'asyncio.iscoroutinefunction' is deprecated and slated for removal
in Python 3.16; use inspect.iscoroutinefunction() instead.

Fixes: #635
  • Loading branch information
frenzymadness committed Jan 21, 2025
1 parent 1e1ffdc commit c2a83bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion async_lru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
else:
from typing_extensions import Self

if sys.version_info >= (3, 14):
import inspect
iscoroutinefunction = inspect.iscoroutinefunction

Check warning on line 32 in async_lru/__init__.py

View check run for this annotation

Codecov / codecov/patch

async_lru/__init__.py#L31-L32

Added lines #L31 - L32 were not covered by tests
else:
iscoroutinefunction = asyncio.iscoroutinefunction


__version__ = "2.0.4"

Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import asyncio
import inspect
import platform
import sys
from functools import _CacheInfo, partial
from typing import Callable

import pytest

from async_lru import _CacheParameters, alru_cache
from async_lru import _CacheParameters, alru_cache, iscoroutinefunction


def test_alru_cache_not_callable() -> None:
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit c2a83bb

Please sign in to comment.