Skip to content

Commit

Permalink
fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelyenvoy committed Dec 14, 2020
1 parent 5bad6a5 commit 247a53f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions memoization/memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def custom_key_maker_wrapper(args, kwargs):
# Create wrapper
wrapper = _create_cached_wrapper(user_function, max_size, ttl, algorithm,
thread_safe, order_independent, custom_key_maker_wrapper)
wrapper.__signature__ = inspect.signature(user_function) # copy the signature of user_function to the wrapper
return update_wrapper(wrapper, user_function) # update wrapper to make it look like the original function


Expand Down
46 changes: 44 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from itertools import chain
from threading import Thread
from threading import Lock
import inspect

from memoization import cached, CachingAlgorithmFlag
from memoization.caching.general.keys_order_dependent import make_key
Expand Down Expand Up @@ -161,6 +162,41 @@ def f22(a=1, *b, c=2, **d):
return a, b, c, d


def f23(a=1, *b, c=2, **d):
exec_times['f23'] += 1
return a, b, c, d


@cached
def f24(a=1, *b, c=2, **d):
exec_times['f24'] += 1
return a, b, c, d


@cached()
def f25(a=1, *b, c=2, **d):
exec_times['f25'] += 1
return a, b, c, d


@cached(max_size=5, algorithm=CachingAlgorithmFlag.FIFO)
def f26(a=1, *b, c=2, **d):
exec_times['f26'] += 1
return a, b, c, d


@cached(max_size=5, algorithm=CachingAlgorithmFlag.LRU)
def f27(a=1, *b, c=2, **d):
exec_times['f27'] += 1
return a, b, c, d


@cached(max_size=5, algorithm=CachingAlgorithmFlag.LFU)
def f28(a=1, *b, c=2, **d):
exec_times['f28'] += 1
return a, b, c, d


################################################################################
# Test entry point
################################################################################
Expand Down Expand Up @@ -349,6 +385,13 @@ def test_memoization_for_custom_key_maker_function(self):
def test_memoization_for_custom_key_maker_lambda(self):
self._general_custom_key_maker_for_all_kinds_of_args_test(f19, general_custom_key_maker)

def test_memoization_must_preserve_type_signature(self):
self.assertEqual(inspect.getfullargspec(f23), inspect.getfullargspec(f24))
self.assertEqual(inspect.getfullargspec(f23), inspect.getfullargspec(f25))
self.assertEqual(inspect.getfullargspec(f23), inspect.getfullargspec(f26))
self.assertEqual(inspect.getfullargspec(f23), inspect.getfullargspec(f27))
self.assertEqual(inspect.getfullargspec(f23), inspect.getfullargspec(f28))

def _general_test(self, tested_function, algorithm, hits, misses, in_cache, not_in_cache):
# clear
exec_times[tested_function.__name__] = 0
Expand Down Expand Up @@ -512,7 +555,6 @@ def call_tested_function(arg, kwargs):

key = make_key((arg,), kwargs)
call_tested_function(arg, kwargs)
time.sleep(0.25) # wait for a short time

info = tested_function.cache_info()
self.assertEqual(info.hits, 0)
Expand All @@ -529,7 +571,7 @@ def call_tested_function(arg, kwargs):
self.assertIn(key, tested_function._cache)
self.assertEqual(exec_times[tested_function.__name__], 1)

time.sleep(0.35) # wait until the cache expires
time.sleep(0.6) # wait until the cache expires

info = tested_function.cache_info()
self.assertEqual(info.current_size, 1)
Expand Down

0 comments on commit 247a53f

Please sign in to comment.