mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 19:11:09 +00:00
Use typing.ParamSpec to type get_or_set()
This commit is contained in:
parent
4852f7ff1e
commit
93396c0f2c
2 changed files with 10 additions and 7 deletions
|
@ -1,17 +1,20 @@
|
||||||
""" Custom handler for caching """
|
""" 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
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
Args = ParamSpec("Args")
|
||||||
|
Ret = TypeVar("Ret")
|
||||||
|
|
||||||
|
|
||||||
def get_or_set(
|
def get_or_set(
|
||||||
cache_key: str,
|
cache_key: str,
|
||||||
function: Callable[..., Any],
|
function: Callable[Args, Ret],
|
||||||
*args: Tuple[Any, ...],
|
*args: Args.args,
|
||||||
timeout: Union[float, None] = None
|
timeout: Optional[float] = None
|
||||||
) -> Any:
|
) -> Ret:
|
||||||
"""Django's built-in get_or_set isn't cutting it"""
|
"""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:
|
if value is None:
|
||||||
value = function(*args)
|
value = function(*args)
|
||||||
cache.set(cache_key, value, timeout=timeout)
|
cache.set(cache_key, value, timeout=timeout)
|
||||||
|
|
2
mypy.ini
2
mypy.ini
|
@ -25,4 +25,4 @@ ignore_errors = False
|
||||||
[mypy-bookwyrm.templatetags.*]
|
[mypy-bookwyrm.templatetags.*]
|
||||||
ignore_errors = False
|
ignore_errors = False
|
||||||
allow_untyped_calls = True
|
allow_untyped_calls = True
|
||||||
disable_error_code = attr-defined, arg-type
|
disable_error_code = attr-defined
|
||||||
|
|
Loading…
Reference in a new issue