Skip to content

Commit

Permalink
ENH(TST): test workaround for decorating static and class methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yarikoptic committed Mar 18, 2021
1 parent 39bb67c commit ab0c1f1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/fscacher/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,54 @@ def f3(): # nesting call into f2
assert f3() == 3


#
# @classmethod and @staticmethod do not "decorate well enough":
# Somehow joblib inspection of them ends up with the instances
# of their classes and not underlying decorated methods.
#

#
# For classmethod to be "fscached" we would need to have class
# re-importable, thus the test class (and cache) are defined at
# module level
#
_public_cache = PersistentCache(name=_cache_name)


class _public_klass(object):
counter = 0
@_public_cache.memoize
# @classmethod
def f(cls):
cls.counter += 1
return cls.counter


_public_klass.f = classmethod(_public_klass.f)


def test_memoize_classmethod(cache):
for _ in range(3):
assert _public_klass.f() == 1
_public_cache.clear()


def test_memoize_staticmethod(cache):

mem = []

class klass(object):
@cache.memoize
# @staticmethod
def f():
mem.append(len(mem))
return len(mem)
klass.f = staticmethod(klass.f)

for _ in range(2):
assert klass.f() == 1


def test_memoize_path(cache, tmp_path):
calls = []

Expand Down

0 comments on commit ab0c1f1

Please sign in to comment.