python / python/typeshed

cachetools memoizing attributes (cache, key, lock) are not available

Offen
#13,395 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stubs: false positive
Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

According to cachetools’ documentation,

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.

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.)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die typeshed-Stubs für cachetools.cached() und cachedmethod() zu finden, und vergleiche dann deren aktuellen Rückgabetypen mit den dokumentierten Memoizing-Attributen von cachetools. Füge gegebenenfalls Typing-Unterstützung für cache, cache_key, cache_lock und cache_clear hinzu und überprüfe die bereitgestellten Beispiele für cached-Funktionen und cached-Methoden mit den entsprechenden Typprüfungen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
developer-experience
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.