Memoization of method working on python 3.6 but not on 3.7.3

i've never said this about python before, but this honestly looks like a bug. i have no idea why it's happening, because all this stuff is in underlying C.

but here's what i'm seeing, attempting to peer into the black box:

i added some simple printing to your code:

def memoize_method(func):
    # From stackoverflow.com/questions/33672412/python-functools-lru-cache-with-class-methods-release-object
    def wrapped_func(self, *args, **kwargs):
        self_weak = weakref.ref(self)

        print('wrapping func')
        @lru_cache()
        def cached_method(*args_, **kwargs_):
            print('in cached_method', args_, kwargs_, id(cached_method))
            return func(self_weak(), *args_, **kwargs_)

        setattr(self, func.__name__, cached_method)
        return cached_method(*args, **kwargs)

    return wrapped_func

then i tested the function like this:

def test_memoization_arg_call():
    obj = MyClass()
    for _ in range(5):
        print(id(obj.randint), obj.randint(1), obj.randint.cache_info(), id(obj.randint))
    print()
    for _ in range(5):
        print(id(obj.randint), obj.randint(2), obj.randint.cache_info(), id(obj.randint))

here's the output:

==================================
wrapping func
in cached_method (1,) {} 4525448992
4521585800 668415661 CacheInfo(hits=0, misses=1, maxsize=128, currsize=1) 4525448992
in cached_method (1,) {} 4525448992
4525448992 920166498 CacheInfo(hits=0, misses=2, maxsize=128, currsize=2) 4525448992
4525448992 920166498 CacheInfo(hits=1, misses=2, maxsize=128, currsize=2) 4525448992
4525448992 920166498 CacheInfo(hits=2, misses=2, maxsize=128, currsize=2) 4525448992
4525448992 920166498 CacheInfo(hits=3, misses=2, maxsize=128, currsize=2) 4525448992

in cached_method (2,) {} 4525448992
4525448992 690871031 CacheInfo(hits=3, misses=3, maxsize=128, currsize=3) 4525448992
4525448992 690871031 CacheInfo(hits=4, misses=3, maxsize=128, currsize=3) 4525448992
4525448992 690871031 CacheInfo(hits=5, misses=3, maxsize=128, currsize=3) 4525448992
4525448992 690871031 CacheInfo(hits=6, misses=3, maxsize=128, currsize=3) 4525448992
4525448992 690871031 CacheInfo(hits=7, misses=3, maxsize=128, currsize=3) 4525448992

the interesting thing here is that it seems like it mis-caches the first positional args call. this doesn't happen with kwargs, and if you call a kwargs call first, it won't mis-cache that or any following pos args calls (which, for whatever reason, means your kwargs test is working). the important lines are this:

==================================
wrapping func
in cached_method (1,) {} 4525448992
4521585800 668415661 CacheInfo(hits=0, misses=1, maxsize=128, currsize=1) 4525448992
in cached_method (1,) {} 4525448992
4525448992 920166498 CacheInfo(hits=0, misses=2, maxsize=128, currsize=2) 4525448992
4525448992 920166498 CacheInfo(hits=1, misses=2, maxsize=128, currsize=2) 4525448992

you can see that i'm in function cached_method with id 4525448992 twice with the exact same args/kwargs, but it's not caching. it even shows the misses themselves in CacheInfo (first, the cache is empty. second, it can't find (1,) for some reason). that's all in C, so i don't know how to fix it...

i guess the best answer is to use another lru_cache method and wait for the devs to fix whatever's happening here.

edit: btw, great question.


This is a bug specifically in the Python 3.7.3 minor release. It was not present in Python 3.7.2, and it should not be present in Python 3.7.4 or 3.8.0. It was filed as Python issue 36650.

At C level, calls with no keyword arguments and calls with an empty **kwargs dict are handled differently. Depending on details of how a function is implemented, the function may receive NULL for kwargs instead of an empty kwargs dict. The C accelerator for functools.lru_cache treated calls with NULL kwargs differently from calls with an empty kwargs dict, leading to the bug you see here.

With the method cache recipe you're using, the first call to a method will always pass an empty kwargs dict to the C-level LRU wrapper, whether or not any keyword arguments were used, because of the return cached_method(*args, **kwargs) in wrapped_func. Subsequent calls may pass a NULL kwargs dict, because they no longer go through wrapped_func. This is why you could not reproduce the bug with test_memoization_kwarg_call; the first call has to pass no keyword arguments.


I have a simpler solution about the problem:

pip install methodtools

Then,

import random
from methodtools import lru_cache


class MyClass:
    @lru_cache()
    def randint(self, param):
        return random.randint(0, int(1E9))


def test_memoization_kwarg_call():
    obj = MyClass()
    assert obj.randint(param=1) == obj.randint(param=1)
    assert obj.randint(1) == obj.randint(1)

I am sorry that this is not the answer for "why" but if you are also intrested in fixing the problem. This is tested with 3.7.3.