Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update pre-commit hooks to latest versions and add tests #11

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading