hephex / asyncache

Helpers to use cachetools with async functions
MIT License
94 stars 12 forks source link

Lock type definition #17

Open pmirandaa opened 1 year ago

pmirandaa commented 1 year ago

Hi, reading your code I saw that you have the lock defined as AbstractContextManager

def cached(
    cache: Optional[MutableMapping[_KT, Any]],
    # ignoring the mypy error to be consistent with the type used
    # in https://github.com/python/typeshed/tree/master/stubs/cachetools
    key: Callable[..., _KT] = keys.hashkey,  # type:ignore
    lock: Optional["AbstractContextManager[Any]"] = None,
) -> IdentityFunction:

I think it should be AbstractAsyncContextManager because we can't do async with lock with something that is not async.

tekumara commented 8 months ago

Here's an MRE

from asyncio import Lock

from asyncache import cached
from cachetools import LRUCache

@cached(cache=LRUCache(maxsize=640*1024, getsizeof=len), lock=Lock())
def get_pep(num):
    ...

vscode/pyright will error with:

error: Argument of type "Lock" cannot be assigned to parameter "lock" of type "AbstractContextManager[Any] | None" in function "cached"