1717DT = typing .TypeVar ("DT" )
1818FT = 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
644649def 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
0 commit comments