Skip to content

Commit 91a8ded

Browse files
committed
Fix & test utils
1 parent 9747a14 commit 91a8ded

4 files changed

Lines changed: 487 additions & 16 deletions

File tree

cachebox/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
from ._cachebox import VTTLCache as VTTLCache
99
from ._core import __version__ as __version__
1010
from ._core import _small_offset_feature as _small_offset_feature
11-
12-
# Utils
11+
from .utils import EVENT_HIT as EVENT_HIT
12+
from .utils import EVENT_MISS as EVENT_MISS
1313
from .utils import Frozen as Frozen
1414
from .utils import cached as cached
15+
from .utils import clear_cached_cache as clear_cached_cache
16+
from .utils import get_cached_cache as get_cached_cache
17+
from .utils import get_cached_cache_info as get_cached_cache_info
18+
from .utils import get_cached_callback as get_cached_callback
1519
from .utils import is_cached as is_cached
16-
17-
# Key maker functions
1820
from .utils import make_hash_key as make_hash_key
1921
from .utils import make_key as make_key
2022
from .utils import make_typed_key as make_typed_key
21-
22-
# Postprocess functions
2323
from .utils import postprocess_copy as postprocess_copy
2424
from .utils import postprocess_copy_mutables as postprocess_copy_mutables
2525
from .utils import postprocess_deepcopy as postprocess_deepcopy

cachebox/utils.py

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
DT = typing.TypeVar("DT")
1818
FT = typing.TypeVar("FT", bound=typing.Callable[..., typing.Any])
1919

20-
_PostProcess: typing.TypeAlias = typing.Callable[[typing.Any], typing.Any] | None
20+
_PostProcess: typing.TypeAlias = typing.Callable[[typing.Any], typing.Any]
21+
_Callback: typing.TypeAlias = typing.Callable[[int, typing.Any, typing.Any], typing.Any]
2122

2223

2324
_COPY_TYPES = frozenset((dict, list, set))
@@ -380,7 +381,7 @@ def _cached_wrapper(
380381
key_maker: typing.Callable[[tuple, dict], typing.Hashable],
381382
clear_reuse: bool,
382383
callback: typing.Callable[[int, typing.Any, typing.Any], None] | None,
383-
postprocess: _PostProcess,
384+
postprocess: _PostProcess | None,
384385
):
385386
cache_is_fn = callable(cache)
386387

@@ -481,8 +482,8 @@ def _async_cached_wrapper(
481482
cache: BaseCacheImpl | typing.Callable,
482483
key_maker: typing.Callable[..., typing.Hashable],
483484
clear_reuse: bool,
484-
callback: typing.Callable | None,
485-
postprocess: _PostProcess,
485+
callback: _Callback | None,
486+
postprocess: _PostProcess | None,
486487
):
487488
cache_is_fn = callable(cache)
488489
_make_key = (
@@ -577,8 +578,8 @@ def cached(
577578
cache: BaseCacheImpl | dict | typing.Callable[..., BaseCacheImpl] | None = None,
578579
key_maker: typing.Callable[..., typing.Hashable] = make_key,
579580
clear_reuse: bool = False,
580-
callback: typing.Callable[[int, typing.Any, typing.Any], typing.Any] | None = None,
581-
postprocess: _PostProcess = postprocess_copy_mutables,
581+
callback: _Callback | None = None,
582+
postprocess: _PostProcess | None = postprocess_copy_mutables,
582583
) -> typing.Callable[[FT], FT]:
583584
"""
584585
Decorator to memoize function/method results.
@@ -603,7 +604,11 @@ def cached(
603604
* :func:`postprocess_deepcopy` - deep-copy.
604605
* :func:`postprocess_deepcopy_mutables` - deep-copy only `dict`, `list` and `set`.
605606
606-
Pass ``cachebox__ignore=True`` at call-time to bypass the cache.
607+
Note:
608+
Pass ``cachebox__ignore=True`` at call-time to bypass the cache.
609+
If *cache* isn't a lambda/function, these attributes will be attached to
610+
your function: ``cache`` (property), ``cache_info`` (callable), ``clear_cache`` (callable),
611+
and ``callback`` (property).
607612
608613
Examples::
609614
@@ -642,5 +647,66 @@ def decorator(func: FT) -> FT:
642647

643648

644649
def is_cached(func: object) -> bool:
645-
"""Return ``True`` if *func* was decorated with :func:`cached`."""
650+
"""
651+
Return ``True`` if *func* was decorated with :func:`cached`.
652+
653+
Args:
654+
func: an object or function to check.
655+
"""
646656
return hasattr(func, "cache") and isinstance(func.cache, BaseCacheImpl) # type: ignore[union-attr]
657+
658+
659+
def get_cached_cache(cached_func: object) -> BaseCacheImpl:
660+
"""
661+
A way to get ``cached_func.cache``, without type-hint warnings.
662+
663+
Args:
664+
cached_func: a function decorated with :func:`cached`.
665+
666+
Warning:
667+
If *func* wasn't decorated with :func:`cached`, or you passed a lambda/function as *cache*
668+
to :func:`cached` decorator, raises ``AttributeError``.
669+
"""
670+
return cached_func.cache # type: ignore
671+
672+
673+
def get_cached_cache_info(cached_func: object) -> CacheInfo:
674+
"""
675+
A way to get ``cached_func.cache_info()``, without type-hint warnings.
676+
677+
Args:
678+
cached_func: a function decorated with :func:`cached`.
679+
680+
Warning:
681+
If *func* wasn't decorated with :func:`cached`, or you passed a lambda/function as *cache*
682+
to :func:`cached` decorator, raises ``AttributeError``.
683+
"""
684+
return cached_func.cache_info() # type: ignore
685+
686+
687+
def get_cached_callback(cached_func: object) -> _Callback | None:
688+
"""
689+
A way to get ``cached_func.callback``, without type-hint warnings.
690+
691+
Args:
692+
cached_func: a function decorated with :func:`cached`.
693+
694+
Warning:
695+
If *func* wasn't decorated with :func:`cached`, or you passed a lambda/function as *cache*
696+
to :func:`cached` decorator, raises ``AttributeError``.
697+
"""
698+
return cached_func.callback # type: ignore
699+
700+
701+
def clear_cached_cache(cached_func: object) -> BaseCacheImpl:
702+
"""
703+
A way to call ``cached_func.cache_clear()``, without type-hint warnings.
704+
705+
Args:
706+
cached_func: a function decorated with :func:`cached`.
707+
708+
Warning:
709+
If *func* wasn't decorated with :func:`cached`, or you passed a lambda/function as *cache*
710+
to :func:`cached` decorator, raises ``AttributeError``.
711+
"""
712+
return cached_func.cache_clear() # type: ignore

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ maturin
22
pytest
33
hypothesis
44
pytest-benchmark
5-
pygal
5+
pytest-asyncio

0 commit comments

Comments
 (0)