python-memoization icon indicating copy to clipboard operation
python-memoization copied to clipboard

type signature preservation does not support class arguments

Open bmc-msft opened this issue 4 years ago • 3 comments
trafficstars

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

bmc-msft avatar Dec 31 '20 22:12 bmc-msft

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

bmc-msft avatar Dec 31 '20 22:12 bmc-msft

Came across this one today also.

pelson avatar Jan 06 '21 16:01 pelson

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.

lonelyenvoy avatar Aug 01 '21 20:08 lonelyenvoy