From 2bbb3262f867e676bb7a774ff1f272ab36fd29fe Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Tue, 12 Nov 2024 15:31:15 +0300 Subject: [PATCH 1/3] NoCache class requires parentheses for decorator, unlike Cache Fixes #19 --- perscache/cache.py | 26 +++++++++++++------------- tests/test_nocache.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 tests/test_nocache.py diff --git a/perscache/cache.py b/perscache/cache.py index 83c5b27..0eff857 100644 --- a/perscache/cache.py +++ b/perscache/cache.py @@ -138,7 +138,7 @@ class NoCache: ``` cache = NoCache() if os.environ["DEBUG"] else Cache() - @cache.cache + @cache def function(): ... ``` @@ -147,24 +147,24 @@ def function(): def __repr__(self) -> str: return "" - @staticmethod - def __call__(*decorator_args, **decorator_kwargs): + def __call__(self, *decorator_args, **decorator_kwargs): """Will call the decorated function every time and return its result without any caching. """ + if decorator_args and callable(decorator_args[0]): + return self._decorator(decorator_args[0]) + return self._decorator - def _decorator(fn): - @functools.wraps(fn) - def _non_async_wrapper(*args, **kwargs): - return fn(*args, **kwargs) - - @functools.wraps(fn) - async def _async_wrapper(*args, **kwargs): - return await fn(*args, **kwargs) + def _decorator(self, fn): + @functools.wraps(fn) + def _non_async_wrapper(*args, **kwargs): + return fn(*args, **kwargs) - return _async_wrapper if is_async(fn) else _non_async_wrapper + @functools.wraps(fn) + async def _async_wrapper(*args, **kwargs): + return await fn(*args, **kwargs) - return _decorator + return _async_wrapper if is_async(fn) else _non_async_wrapper cache = __call__ # Alias for backwards compatibility. diff --git a/tests/test_nocache.py b/tests/test_nocache.py new file mode 100644 index 0000000..89b4fea --- /dev/null +++ b/tests/test_nocache.py @@ -0,0 +1,19 @@ +from perscache import NoCache + +def test_no_cache_with_parentheses(): + + cache = NoCache() + @cache() + def dummy_func(): + return 'NoCache with parentheses' + + assert dummy_func() == 'NoCache with parentheses' + +def test_no_cache_without_parentheses(): + cache = NoCache() + + @cache + def dummy_func(): + return 'NoCache without parentheses' + + assert dummy_func() == 'NoCache without parentheses' From 9fb8ad6edbc319bbb5d213ee120facb3b354bad5 Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Tue, 12 Nov 2024 15:38:24 +0300 Subject: [PATCH 2/3] Better tests --- tests/test_nocache.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_nocache.py b/tests/test_nocache.py index 89b4fea..01621cb 100644 --- a/tests/test_nocache.py +++ b/tests/test_nocache.py @@ -1,13 +1,19 @@ from perscache import NoCache +import asyncio def test_no_cache_with_parentheses(): - + counter = 0 cache = NoCache() @cache() def dummy_func(): + nonlocal counter + counter += 1 return 'NoCache with parentheses' assert dummy_func() == 'NoCache with parentheses' + assert dummy_func() == 'NoCache with parentheses' + assert counter == 2 + def test_no_cache_without_parentheses(): cache = NoCache() @@ -17,3 +23,23 @@ def dummy_func(): return 'NoCache without parentheses' assert dummy_func() == 'NoCache without parentheses' + + +async def test_no_cache_with_parentheses_async(): + cache = NoCache() + + @cache() + async def dummy_func(): + return 'NoCache with parentheses async' + + assert await dummy_func() == 'NoCache with parentheses async' + + +async def test_no_cache_without_parentheses_async(): + cache = NoCache() + + @cache + async def dummy_func(): + return 'NoCache without parentheses async' + + assert await dummy_func() == 'NoCache without parentheses async' From 23e81deaeaae15e2ead0b4f5d1c8f45a1c8d39e5 Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Tue, 12 Nov 2024 15:40:04 +0300 Subject: [PATCH 3/3] Update python versions --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 9dda3b7..28ce162 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: true matrix: - python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v3