Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test. Drop unnecessary default argument.
  • Loading branch information
rhettinger committed May 10, 2020
commit bb42c0566b7c793a99fb80ad4a8c4303ea53b3b7
2 changes: 1 addition & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def cache_clear():

def cache(user_function, /):
Comment thread
rhettinger marked this conversation as resolved.
'Simple lightweight unbounded cache. Sometimes called "memoize".'
return lru_cache(maxsize=None, typed=False)(user_function)
return lru_cache(maxsize=None)(user_function)


################################################################################
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,25 @@ def check_order_with_hash_seed(seed):
self.assertEqual(run1, run2)


class TestCache:
# This tests that the pass-through is working as designed.
# The underlying functionality is tested in TestLRU.

def test_cache(self):
@self.module.cache
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
self.assertEqual([fib(n) for n in range(16)],
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
self.assertEqual(fib.cache_info(),
self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
fib.cache_clear()
self.assertEqual(fib.cache_info(),
self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))


class TestLRU:

def test_lru(self):
Expand Down