You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cachetools memoizing attributes (cache, key, lock) are not available #13395
The decorator’s cache, key and lock parameters are also available as cache, cache_key and cache_lock attributes of the memoizing wrapper function. […] For the common use case of clearing or invalidating the cache, the decorator also provides a cache_clear()[.]
However, this is currently not reflected in the typeshed stubs. cached() and cachedmethod() are declared to return IdentityFunction, i.e. a decorator that takes the function-to-be-cached and returns a callable with identical signature, without the additional properties and functions. It would be great if support for this was added.
I’m not sure if this is the “intended” way to use the memoizing attributes of a cached method (passing in c for the self argument explicitly), but it seems to work at runtime, at least. (Personally, I’d be fine with support just for @cached functions, but I figured it makes sense to include @cachedmethod in the example too.)
I tried to implement this myself, but I’m afraid I’m not good enough at Python generics and type magic, sorry 😔 but maybe someone else can figure it out!
I think the core reasons why this is difficult to do (at least for cachedmethod) are the same as why lru_cache in the stdlib isn't properly typed: #11280 and https://discuss.python.org/t/allow-self-
I think fixing cachedtools.cached wouldn't be too hard, something like this would work for the example you provided:
Note that the cachedmethod half has changed (c no longer passed through) because it turns out the thing I was wondering about in the issue description bothered the cachetools folks too and in v7 they fixed it 👍
gh13395.py:13: error: Item "None" of "AbstractContextManager[Any, bool | None] | None" has no attribute "__enter__" [union-attr]
gh13395.py:13: error: Item "None" of "AbstractContextManager[Any, bool | None] | None" has no attribute "__exit__" [union-attr]
gh13395.py:14: error: Value of type "MutableMapping[Any, Any] | None" is not indexable [index]
gh13395.py:32: error: Item "None" of "AbstractContextManager[Any, bool | None] | None" has no attribute "__enter__" [union-attr]
gh13395.py:32: error: Item "None" of "AbstractContextManager[Any, bool | None] | None" has no attribute "__exit__" [union-attr]
gh13395.py:33: error: Value of type "MutableMapping[Any, Any] | None" is not indexable [index]
Found 6 errors in 1 file (checked 1 source file)
This could be avoided by adding more generics, so the cache and lock types can be inferred to be non-None iff they were passed in as non-None. For the cache itself, this is less necessary because cache=None is now deprecated and will be removed in the next major release, but for the lock it would still be nice.
Activity
lucaswerkmeister commentedon Jan 12, 2025
I tried to implement this myself, but I’m afraid I’m not good enough at Python generics and type magic, sorry 😔 but maybe someone else can figure it out!
max-muoto commentedon Jan 27, 2025
I think the core reasons why this is difficult to do (at least for
cachedmethod) are the same as whylru_cachein the stdlib isn't properly typed: #11280 and https://discuss.python.org/t/allow-self-I think fixing
cachedtools.cachedwouldn't be too hard, something like this would work for the example you provided:(Ignoring the
cachetype)tqa236 commentedon Oct 24, 2026
I think this issue is fixed by #15657 (and before that, #14770)
lucaswerkmeister commentedon Oct 24, 2026
Well, the code example still doesn’t quite work, just for different reasons:
Code example
Note that the
cachedmethodhalf has changed (cno longer passed through) because it turns out the thing I was wondering about in the issue description bothered the cachetools folks too and in v7 they fixed it 👍This could be avoided by adding more generics, so the cache and lock types can be inferred to be non-
Noneiff they were passed in as non-None. For the cache itself, this is less necessary becausecache=Noneis now deprecated and will be removed in the next major release, but for the lock it would still be nice.