Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
leshchenko1979 committed Nov 12, 2024
2 parents 717bfac + 7fcd34b commit bb90a3d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions perscache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class NoCache:
```
cache = NoCache() if os.environ["DEBUG"] else Cache()
@cache.cache
@cache
def function():
...
```
Expand All @@ -147,24 +147,24 @@ def function():
def __repr__(self) -> str:
return "<NoCache>"

@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.

Expand Down
45 changes: 45 additions & 0 deletions tests/test_nocache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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()

@cache
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'

0 comments on commit bb90a3d

Please sign in to comment.