python / python/typeshed

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

Abierto
#13,395 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stubs: false positive
Lenguaje dominante
Python
Estrellas
5.1k
Forks
2.1k
Merge medio
1 d 19 h
PR fusionados (30 d)
82

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza localizando los stubs de typeshed para cachetools.cached() y cachedmethod(), y compara después sus tipos de retorno actuales con los atributos de memoización documentados de cachetools. Añade compatibilidad de tipado para cache, cache_key, cache_lock y cache_clear cuando corresponda, y verifica los ejemplos proporcionados de cached-function y cached-method con las pruebas de comprobación de tipos pertinentes.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
developer-experience
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.