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
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.)
The text was updated successfully, but these errors were encountered:
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:
According to cachetools’ documentation,
However, this is currently not reflected in the typeshed stubs.
cached()
andcachedmethod()
are declared to returnIdentityFunction
, 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.Code example
I’m not sure if this is the “intended” way to use the memoizing attributes of a cached method (passing in
c
for theself
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.)The text was updated successfully, but these errors were encountered: