asyncache icon indicating copy to clipboard operation
asyncache copied to clipboard

Lock type definition

Open pmirandaa opened this issue 2 years ago • 1 comments

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.

pmirandaa avatar Mar 24 '23 19:03 pmirandaa

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"

tekumara avatar Feb 21 '24 01:02 tekumara