Skip to content

Commit 4652285

Browse files
committed
ENH(TST): test workaround for decorating static and class methods
I am not yet 100% sure but I think it is something which could be addressed within joblib itself: to account for peculiarity of Python"s @{class,static}method helpers which are classes themselves which proxy (instead of just simply decorating) the __func__ of underlying function/method.
1 parent 39bb67c commit 4652285

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/fscacher/tests/test_cache.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,54 @@ def f3(): # nesting call into f2
8484
assert f3() == 3
8585

8686

87+
#
88+
# @classmethod and @staticmethod do not "decorate well enough":
89+
# Somehow joblib inspection of them ends up with the instances
90+
# of their classes and not underlying decorated methods.
91+
#
92+
93+
#
94+
# For classmethod to be "fscached" we would need to have class
95+
# re-importable, thus the test class (and cache) are defined at
96+
# module level
97+
#
98+
_public_cache = PersistentCache(name=_cache_name)
99+
100+
101+
class _public_klass(object):
102+
counter = 0
103+
@_public_cache.memoize
104+
@classmethod
105+
def f(cls):
106+
cls.counter += 1
107+
return cls.counter
108+
109+
110+
_public_klass.f = classmethod(_public_klass.f)
111+
112+
113+
def test_memoize_classmethod(cache):
114+
for _ in range(3):
115+
assert _public_klass.f() == 1
116+
_public_cache.clear()
117+
118+
119+
def test_memoize_staticmethod(cache):
120+
121+
mem = []
122+
123+
class klass(object):
124+
@cache.memoize
125+
# @staticmethod
126+
def f():
127+
mem.append(len(mem))
128+
return len(mem)
129+
klass.f = staticmethod(klass.f)
130+
131+
for _ in range(2):
132+
assert klass.f() == 1
133+
134+
87135
def test_memoize_path(cache, tmp_path):
88136
calls = []
89137

0 commit comments

Comments
 (0)