Skip to content

Commit 4df3785

Browse files
Switch from asyncio.iscoroutinefunction to inspect. (#637)
1 parent fb3125e commit 4df3785

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

async_lru/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import dataclasses
3+
import inspect
34
import sys
4-
from asyncio.coroutines import _is_coroutine # type: ignore[attr-defined]
55
from functools import _CacheInfo, _make_key, partial, partialmethod
66
from typing import (
77
Any,
@@ -27,6 +27,9 @@
2727
else:
2828
from typing_extensions import Self
2929

30+
if sys.version_info < (3, 14):
31+
from asyncio.coroutines import _is_coroutine # type: ignore[attr-defined]
32+
3033

3134
__version__ = "2.0.5"
3235

@@ -95,7 +98,8 @@ def __init__(
9598
pass
9699
# set __wrapped__ last so we don't inadvertently copy it
97100
# from the wrapped function when updating __dict__
98-
self._is_coroutine = _is_coroutine
101+
if sys.version_info < (3, 14):
102+
self._is_coroutine = _is_coroutine
99103
self.__wrapped__ = fn
100104
self.__maxsize = maxsize
101105
self.__typed = typed
@@ -262,7 +266,8 @@ def __init__(
262266
pass
263267
# set __wrapped__ last so we don't inadvertently copy it
264268
# from the wrapped function when updating __dict__
265-
self._is_coroutine = _is_coroutine
269+
if sys.version_info < (3, 14):
270+
self._is_coroutine = _is_coroutine
266271
self.__wrapped__ = wrapper.__wrapped__
267272
self.__instance = instance
268273
self.__wrapper = wrapper
@@ -299,14 +304,17 @@ def wrapper(fn: _CBP[_R]) -> _LRUCacheWrapper[_R]:
299304
while isinstance(origin, (partial, partialmethod)):
300305
origin = origin.func
301306

302-
if not asyncio.iscoroutinefunction(origin):
307+
if not inspect.iscoroutinefunction(origin):
303308
raise RuntimeError(f"Coroutine function is required, got {fn!r}")
304309

305310
# functools.partialmethod support
306311
if hasattr(fn, "_make_unbound_method"):
307312
fn = fn._make_unbound_method()
308313

309-
return _LRUCacheWrapper(cast(_CB[_R], fn), maxsize, typed, ttl)
314+
wrapper = _LRUCacheWrapper(cast(_CB[_R], fn), maxsize, typed, ttl)
315+
if sys.version_info >= (3, 12):
316+
wrapper = inspect.markcoroutinefunction(wrapper)
317+
return wrapper
310318

311319
return wrapper
312320

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ known_first_party=async_lru
7474
addopts= -s --keep-duplicates --cache-clear --verbose --no-cov-on-fail --cov=async_lru --cov=tests/ --cov-report=term --cov-report=html
7575
filterwarnings =
7676
error
77+
ignore:'asyncio.get_event_loop_policy' is deprecated:DeprecationWarning:pytest_asyncio
78+
ignore:'asyncio.set_event_loop_policy' is deprecated:DeprecationWarning:pytest_asyncio
79+
ignore:'asyncio.set_event_loop' is deprecated:DeprecationWarning:pytest_asyncio
7780
testpaths = tests/
7881
junit_family=xunit2
7982
asyncio_mode=auto

tests/test_basic.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import inspect
23
import platform
34
import sys
45
from functools import _CacheInfo, partial
@@ -27,7 +28,10 @@ async def test_alru_cache_deco(check_lru: Callable[..., None]) -> None:
2728
async def coro() -> None:
2829
pass
2930

30-
assert asyncio.iscoroutinefunction(coro)
31+
if sys.version_info >= (3, 12):
32+
assert inspect.iscoroutinefunction(coro)
33+
if sys.version_info < (3, 14):
34+
assert asyncio.iscoroutinefunction(coro)
3135

3236
check_lru(coro, hits=0, misses=0, cache=0, tasks=0)
3337

@@ -41,7 +45,10 @@ async def test_alru_cache_deco_called(check_lru: Callable[..., None]) -> None:
4145
async def coro() -> None:
4246
pass
4347

44-
assert asyncio.iscoroutinefunction(coro)
48+
if sys.version_info >= (3, 12):
49+
assert inspect.iscoroutinefunction(coro)
50+
if sys.version_info < (3, 14):
51+
assert asyncio.iscoroutinefunction(coro)
4552

4653
check_lru(coro, hits=0, misses=0, cache=0, tasks=0)
4754

@@ -56,7 +63,10 @@ async def coro() -> None:
5663

5764
coro_wrapped = alru_cache(coro)
5865

59-
assert asyncio.iscoroutinefunction(coro_wrapped)
66+
if sys.version_info >= (3, 12):
67+
assert inspect.iscoroutinefunction(coro_wrapped)
68+
if sys.version_info < (3, 14):
69+
assert asyncio.iscoroutinefunction(coro)
6070

6171
check_lru(coro_wrapped, hits=0, misses=0, cache=0, tasks=0)
6272

0 commit comments

Comments
 (0)