Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cachetools_async/decorators.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from asyncio import Future, Task, get_event_loop, shield
from functools import update_wrapper
from inspect import iscoroutinefunction
from typing import (
Any,
Awaitable,
Callable,
ContextManager,
MutableMapping,
Optional,
Protocol,
TypeVar,
TYPE_CHECKING
)

from cachetools.keys import hashkey, methodkey

if TYPE_CHECKING:

Check failure on line 18 in src/cachetools_async/decorators.py

View workflow job for this annotation

GitHub Actions / check

Ruff (I001)

src/cachetools_async/decorators.py:1:1: I001 Import block is un-sorted or un-formatted
from threading import Condition

_KT = TypeVar("_KT")
_T = TypeVar("_T")

Expand All @@ -39,6 +43,7 @@
cache: Optional[MutableMapping[_KT, Future]],
key: Callable[..., _KT] = hashkey,
lock: Optional[ContextManager[Any]] = None,
cond: Optional[Condition] = None,
info: bool = False,
) -> IdentityFunction:
"""Wrap a function to save results in a cache."""
Expand All @@ -48,6 +53,9 @@
if lock is not None:
raise NotImplementedError("cachetools_async does not support `lock`")

if cond is not None:
raise NotImplementedError("cachetools_async does not support `cond`")

def decorator(fn: Callable[..., Awaitable]):
if not iscoroutinefunction(fn):
raise TypeError("Expected Coroutine function, got {}".format(fn))
Expand Down Expand Up @@ -109,11 +117,15 @@
cache: Callable[[Any], Optional[MutableMapping[_KT, Future]]],
key: Callable[..., _KT] = methodkey,
lock: Optional[Callable[[Any], ContextManager[Any]]] = None,
cond: Optional[Condition] = None
) -> IdentityFunction:
"""Wrap a class or instance method to save results in a cache."""
if lock is not None:
raise NotImplementedError("cachetools_async does not support `lock`")

if cond is not None:
raise NotImplementedError("cachetools_async does not support `cond`")

def decorator(method: Callable[..., Awaitable]):
if not iscoroutinefunction(method):
raise TypeError("Expected Coroutine function, got {}".format(method))
Expand Down
Loading