Skip to content

Commit

Permalink
chore: Update pre-commit hooks to latest versions and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bart6114 committed Jun 30, 2024
1 parent 300cc98 commit 4824004
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.6.0
hooks:
- id: end-of-file-fixer
exclude: "LICENSE"
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.3
rev: v0.5.0
hooks:
# Run the linter.
- id: ruff
Expand Down
8 changes: 4 additions & 4 deletions expiring_lru_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _expired(cached_func: Callable) -> bool:

def lru_cache(
expires_after: Optional[int] = None,
*args: Union[int, bool],
**kwargs: Union[int, bool],
*lru_args: Union[int, bool],
**lru_kwargs: Union[int, bool],
) -> Callable:
"""
LRU caching with expiration period.
Expand All @@ -56,14 +56,14 @@ def lru_cache(
"""

def decorate(func: Callable) -> Callable:
cached_func = _init_cache(func, expires_after, *args, **kwargs)
cached_func = _init_cache(func, expires_after, *lru_args, **lru_kwargs)

@functools.wraps(func)
def wrapper(*args: Union[int, bool], **kwargs: Union[int, bool]) -> Callable:
nonlocal cached_func
if _expired(cached_func):
logging.debug("Resetting cache")
cached_func = _init_cache(func, expires_after, *args, **kwargs)
cached_func = _init_cache(func, expires_after, *lru_args, **lru_kwargs)
return cached_func(*args, **kwargs)

wrapper.cache_info = lambda: cached_func.cache_info()
Expand Down
29 changes: 28 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Basic testing."""

from datetime import datetime
from datetime import datetime, timedelta
from functools import partial
from time import sleep
from typing import Callable
Expand Down Expand Up @@ -31,6 +31,33 @@ def test_expiration_basic() -> None:
assert any(el != res[0] for el in res)


def test_expiration_with_func_args() -> None:
"""Test with expiration."""

@lru_cache(expires_after=2)
def get_time(secs: int):
# get current time and add secs
curr_time = datetime.now()
return curr_time + timedelta(seconds=secs)

p = partial(get_time, 2)
res = call_every_x_secs(p, 4)
assert any(el != res[0] for el in res)


def test_expiration_with_func_args_as_partial() -> None:
"""Test with expiration."""

def get_time(secs: int):
# get current time and add secs
curr_time = datetime.now()
return curr_time + timedelta(seconds=secs)

p = partial(lru_cache(expires_after=2), partial(get_time, 2))
res = call_every_x_secs(p, 4)
assert any(el != res[0] for el in res)


def test_has_cache_info() -> None:
"""Test if cache has info."""
cf = lru_cache()(lambda: 1)
Expand Down

0 comments on commit 4824004

Please sign in to comment.