lonelyenvoy / python-memoization

A powerful caching library for Python, with TTL support and multiple algorithm options.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

type signature preservation does not support class arguments

bmc-msft opened this issue · comments

Example source:

from memoization import cached
import inspect

class A:
    @cached(ttl=1)
    def b(self, name: str) -> int:
        return len(name) * 2

    def a(self) -> bool:
        return self.b('hello') == 10

Using mypy for the above example gives the error:

/tmp/as-class.py:10: error: Too few arguments for "b" of "A"
/tmp/as-class.py:10: error: Argument 1 to "b" of "A" has incompatible type "str"; expected "A"

If you comment out the @cached line, mypy gives the response:

Success: no issues found in 1 source file

Using the example I provided in #16, @cached preserves the arguments as I expected.

Came across this one today also.

According to Guido van Rossum (python/typing#824), this should be a longstanding issue in mypy (python/mypy#10805). Until now, I did not find any workaround, but I will continue to work on it.

If anyone encounters this issue, I recommend using @bmc-msft's way to disable the cache before mypy checks and enable it afterwards.