Skip to content
Merged
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
39 changes: 20 additions & 19 deletions cachebox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing
from copy import copy as _shallow_copy
from copy import deepcopy as _deep_copy
from collections.abc import Callable, Hashable

from ._cachebox import BaseCacheImpl, LRUCache
from ._wrappers import (
Expand All @@ -23,7 +24,7 @@
KT = typing.TypeVar("KT")
VT = typing.TypeVar("VT")
DT = typing.TypeVar("DT")
FT = typing.TypeVar("FT", bound=typing.Callable[..., typing.Any])
FT = typing.TypeVar("FT", bound=Callable[..., typing.Any])


_COPY_TYPES = frozenset((dict, list, set))
Expand Down Expand Up @@ -63,7 +64,7 @@ def postprocess_deepcopy(value: VT) -> VT:
_FAST_TYPES = frozenset((int, str))


def make_key(*args, **kwds) -> typing.Hashable:
def make_key(*args: typing.Any, **kwds: typing.Any) -> Hashable:
"""
Default cache key.

Expand All @@ -83,7 +84,7 @@ def make_key(*args, **kwds) -> typing.Hashable:
return key


def make_hash_key(*args, **kwds) -> int:
def make_hash_key(*args: typing.Any, **kwds: typing.Any) -> int:
"""
Key as the hash of all positional and keyword arguments.

Expand All @@ -98,7 +99,7 @@ def make_hash_key(*args, **kwds) -> int:
return hash(key)


def make_typed_key(*args, **kwds) -> tuple:
def make_typed_key(*args: typing.Any, **kwds: typing.Any) -> tuple[typing.Any, ...]:
"""
Key that includes the runtime type of every argument.

Expand Down Expand Up @@ -183,7 +184,7 @@ def maxsize(self) -> int:
return self.__cache.maxsize

@property
def getsizeof(self) -> typing.Callable[[KT, VT], int] | None:
def getsizeof(self) -> Callable[[KT, VT], int] | None:
"""Callable or None: The configured ``getsizeof`` function."""
return self.__cache.getsizeof

Expand Down Expand Up @@ -271,7 +272,7 @@ def insert(
value: VT,
*args: typing.Any,
**kwargs: typing.Any,
) -> typing.Optional[VT]:
) -> VT | None:
return self._guard()

def __setitem__(self, key: KT, value: VT) -> None:
Expand All @@ -285,7 +286,7 @@ def update(
) -> None:
return self._guard()

def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
def get(self, key: KT, default: DT | None = None) -> VT | DT:
return self.__cache.get(key, default)

def __getitem__(self, key: KT) -> VT:
Expand All @@ -294,13 +295,13 @@ def __getitem__(self, key: KT) -> VT:
def setdefault(
self,
key: KT,
default: typing.Optional[DT] = None,
default: DT | None = None,
*args: typing.Any,
**kwargs: typing.Any,
) -> typing.Optional[VT | DT]:
) -> VT | DT | None:
return self._guard()

def pop(self, key: KT, default: DT = None) -> typing.Union[VT, DT]:
def pop(self, key: KT, default: DT | None = None) -> VT | DT:
"""
Removes the specified key and returns the corresponding value.

Expand All @@ -319,7 +320,7 @@ def pop(self, key: KT, default: DT = None) -> typing.Union[VT, DT]:
def __delitem__(self, key: KT) -> None:
return self._guard()

def popitem(self) -> typing.Tuple[KT, VT]:
def popitem(self) -> tuple[KT, VT]:
return self._guard() # type: ignore[return-value]

def drain(self, n: int) -> int:
Expand Down Expand Up @@ -348,7 +349,7 @@ def clear(self, *, reuse: bool = False) -> None:
"""
return self._guard()

def items(self) -> typing.Iterable[typing.Tuple[KT, VT]]:
def items(self) -> typing.Iterable[tuple[KT, VT]]:
return self.__cache.items()

def values(self) -> typing.Iterable[VT]:
Expand All @@ -373,14 +374,14 @@ def __repr__(self) -> str:
def _cast_lock(
iscoroutinefunction: bool,
lock: (
typing.Type[AbstractContextManager]
| typing.Type[AbstractAsyncContextManager]
type[AbstractContextManager]
| type[AbstractAsyncContextManager]
| bool
| None
) = True,
) -> (
typing.Type[AbstractContextManager]
| typing.Type[AbstractAsyncContextManager]
type[AbstractContextManager]
| type[AbstractAsyncContextManager]
| None
):
import _thread
Expand Down Expand Up @@ -416,8 +417,8 @@ def _cast_lock(


def cached(
cache: BaseCacheImpl | dict | typing.Callable[..., BaseCacheImpl] | None = None,
key_maker: typing.Callable[..., typing.Hashable] = make_key,
cache: BaseCacheImpl | dict | Callable[..., BaseCacheImpl] | None = None,
key_maker: Callable[..., Hashable] = make_key,
clear_reuse: bool = False,
callback: _Callback | None = None,
copy_level: int = 1,
Expand All @@ -428,7 +429,7 @@ def cached(
| bool
| None
) = True,
) -> typing.Callable[[FT], FT]:
) -> Callable[[FT], FT]:
"""
Decorator to memoize function/method results.

Expand Down