diff --git a/bookwyrm/utils/cache.py b/bookwyrm/utils/cache.py index 5e896e621..df11f16de 100644 --- a/bookwyrm/utils/cache.py +++ b/bookwyrm/utils/cache.py @@ -1,17 +1,20 @@ """ Custom handler for caching """ -from typing import Any, Callable, Tuple, Union +from typing import Callable, Optional, ParamSpec, TypeVar, cast from django.core.cache import cache +Args = ParamSpec("Args") +Ret = TypeVar("Ret") + def get_or_set( cache_key: str, - function: Callable[..., Any], - *args: Tuple[Any, ...], - timeout: Union[float, None] = None -) -> Any: + function: Callable[Args, Ret], + *args: Args.args, + timeout: Optional[float] = None +) -> Ret: """Django's built-in get_or_set isn't cutting it""" - value = cache.get(cache_key) + value = cast(Optional[Ret], cache.get(cache_key)) if value is None: value = function(*args) cache.set(cache_key, value, timeout=timeout) diff --git a/mypy.ini b/mypy.ini index dce5698c4..ef5d2d78f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -25,4 +25,4 @@ ignore_errors = False [mypy-bookwyrm.templatetags.*] ignore_errors = False allow_untyped_calls = True -disable_error_code = attr-defined, arg-type +disable_error_code = attr-defined