Skip to content

@wrapt.lru_cache: recursion issue when overriding and caching cached method from parent class #342

Description

@nhuet

In my usecase, I have a method on a parent class with a large computation time that i want to cache. In a child class i am overriding it to add further computation and i decided also to cache it as it is also quite consuming.
I was using functools.lru_cache until i recently found out about some pitfalls as pointed out here and decided to use wrapt.lru_cache instead. But I now have a recursion issue that i did not have with the standard library decorator.

Here is a standalone example (you have to imagine the expensive computation though :)

import wrapt


class A:
    def __init__(self, a):
        self.a = a

    @wrapt.lru_cache
    def compute(self, x):
        print("compute via A")
        return self.a + x


class B(A):
    @wrapt.lru_cache
    def compute(self, x):
        print("compute via B")
        return super().compute(x) + 1


if __name__ == "__main__":
    b = B(1)
    print(b.compute(10))

RecursionError: maximum recursion depth exceeded while calling a Python object

and i noticed it never enters the parent class method. (It was working with @funcools.lru_cache)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions