From efd1d3b224089c1073b9c8283d06e01aa4c0c501 Mon Sep 17 00:00:00 2001 From: Thomas Kemmer Date: Mon, 15 Jul 2024 20:12:54 +0200 Subject: [PATCH] Fix #256: Deprecate @mru_cache decorator. --- docs/index.rst | 5 +++++ src/cachetools/func.py | 4 ++++ tests/test_func.py | 14 ++++++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index d4a0279..8df4ed9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -604,6 +604,11 @@ all the decorators in this module are thread-safe by default. saves up to `maxsize` results based on a Most Recently Used (MRU) algorithm. + .. deprecated:: 5.4 + + The `mru_cache` decorator has been deprecated due to lack of use. + Please choose a decorator based on some other algorithm. + .. decorator:: rr_cache(user_function) rr_cache(maxsize=128, choice=random.choice, typed=False) diff --git a/src/cachetools/func.py b/src/cachetools/func.py index 0c09a60..3eafddf 100644 --- a/src/cachetools/func.py +++ b/src/cachetools/func.py @@ -82,6 +82,10 @@ def mru_cache(maxsize=128, typed=False): up to `maxsize` results based on a Most Recently Used (MRU) algorithm. """ + from warnings import warn + + warn("@mru_cache is deprecated", DeprecationWarning, stacklevel=2) + if maxsize is None: return _cache({}, None, typed) elif callable(maxsize): diff --git a/tests/test_func.py b/tests/test_func.py index 72e7589..d375c28 100644 --- a/tests/test_func.py +++ b/tests/test_func.py @@ -100,30 +100,32 @@ def __eq__(self, other): class FIFODecoratorTest(unittest.TestCase, DecoratorTestMixin): - DECORATOR = staticmethod(cachetools.func.fifo_cache) class LFUDecoratorTest(unittest.TestCase, DecoratorTestMixin): - DECORATOR = staticmethod(cachetools.func.lfu_cache) class LRUDecoratorTest(unittest.TestCase, DecoratorTestMixin): - DECORATOR = staticmethod(cachetools.func.lru_cache) class MRUDecoratorTest(unittest.TestCase, DecoratorTestMixin): + def decorator(self, maxsize, **kwargs): + import warnings - DECORATOR = staticmethod(cachetools.func.mru_cache) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + d = cachetools.func.mru_cache(maxsize, **kwargs) + self.assertEqual(len(w), 1) + self.assertIs(w[0].category, DeprecationWarning) + return d class RRDecoratorTest(unittest.TestCase, DecoratorTestMixin): - DECORATOR = staticmethod(cachetools.func.rr_cache) class TTLDecoratorTest(unittest.TestCase, DecoratorTestMixin): - DECORATOR = staticmethod(cachetools.func.ttl_cache)