cachetools memoizing attributes (cache, key, lock) are not available
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 5.1k
- Fork
- 2.1k
- Merge medio
- 1g 19h
- PR unite (30g)
- 82
Descrizione
According to cachetools’ documentation,
The decorator’s cache, key and lock parameters are also available as
cache,cache_keyandcache_lockattributes of the memoizing wrapper function. […] For the common use case of clearing or invalidating the cache, the decorator also provides acache_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.
Code example
import cachetools
import threading
## cached function
@cachetools.cached(cache=cachetools.LRUCache(maxsize=10),
key=lambda x, y: y,
lock=threading.Lock())
def fun(x: str, y: str) -> str:
return y
print(fun('ignored', 'hello world'))
with fun.cache_lock:
print(fun.cache[fun.cache_key('ignored', 'hello world')])
fun.cache_clear()
## cached method
class Class:
def __init__(self):
self.cache = cachetools.LRUCache(maxsize=10)
self.lock = threading.Lock()
@cachetools.cachedmethod(cache=lambda self: self.cache,
key=lambda self, x, y: y,
lock=lambda self: self.lock)
def meth(self, x: str, y: str) -> str:
return y
c = Class()
print(c.meth('ignored', 'hello world'))
with c.meth.cache_lock(c):
print(c.meth.cache(c)[c.meth.cache_key(c, 'ignored', 'hello world')])
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.)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia individuando gli stub di typeshed per cachetools.cached() e cachedmethod(), quindi confronta i relativi tipi restituiti attuali con gli attributi di memoizzazione documentati di cachetools. Aggiungi il supporto al typing per cache, cache_key, cache_lock e cache_clear dove appropriato, quindi verifica gli esempi forniti di cached-function e cached-method con i test di type-checking pertinenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- developer-experience
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100