lonelyenvoy / python-memoization

A powerful caching library for Python, with TTL support and multiple algorithm options.
MIT License
230 stars 15 forks source link

cached does not preserve type signature #16

Closed bmc-msft closed 3 years ago

bmc-msft commented 3 years ago

Using cached does not preserve the type signature.

from memoization import cached
import inspect

def foo(a: str) -> int:
    return int(a)

def bar(a: str) -> int:
    return int(a)

@cached
def baz(a: str) -> int:
    return int(a)

assert inspect.getfullargspec(foo) == inspect.getfullargspec(bar), "foo != bar"
assert inspect.getfullargspec(foo) == inspect.getfullargspec(baz), "foo != baz"

Expected: No output

Actual result:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: foo != baz

This prevents using type validation tools, such as mypy, from being used to validate the usage of methods wrapped in cached.

lonelyenvoy commented 3 years ago

Resolved in v0.3.2. Now available at PyPI or GitHub Releases.

I also added full support for static type checking and it should work fine with validation tools. Thanks for the issue and your interest in this work.