mypy: Improve typing of cache_with_key and cache decorators.

Fixes #1348.
This commit is contained in:
neiljp (Neil Pilgrim)
2017-10-23 19:52:04 -07:00
committed by Tim Abbott
parent 2e69d4d420
commit c063ba72a2

View File

@@ -24,6 +24,7 @@ if False:
# they cannot be imported at runtime due to cyclic dependency. # they cannot be imported at runtime due to cyclic dependency.
FuncT = TypeVar('FuncT', bound=Callable[..., Any]) FuncT = TypeVar('FuncT', bound=Callable[..., Any])
ReturnT = TypeVar('ReturnT') # Useful for matching return types via Callable[..., ReturnT]
class NotFoundInCache(Exception): class NotFoundInCache(Exception):
pass pass
@@ -136,9 +137,7 @@ def get_cache_with_key(keyfunc, cache_name=None):
return decorator return decorator
def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None): def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None):
# type: (Any, Optional[str], Optional[int], Optional[str]) -> Any # type: (Callable[..., Text], Optional[str], Optional[int], Optional[str]) -> Callable[[Callable[..., ReturnT]], Callable[..., ReturnT]]
# This function can't be typed perfectly because returning a generic function
# isn't supported in mypy - https://github.com/python/mypy/issues/1551.
"""Decorator which applies Django caching to a function. """Decorator which applies Django caching to a function.
Decorator argument is a function which computes a cache key Decorator argument is a function which computes a cache key
@@ -147,10 +146,10 @@ def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None)
other uses of caching.""" other uses of caching."""
def decorator(func): def decorator(func):
# type: (Callable[..., Any]) -> (Callable[..., Any]) # type: (Callable[..., ReturnT]) -> Callable[..., ReturnT]
@wraps(func) @wraps(func)
def func_with_caching(*args, **kwargs): def func_with_caching(*args, **kwargs):
# type: (*Any, **Any) -> Any # type: (*Any, **Any) -> ReturnT
key = keyfunc(*args, **kwargs) key = keyfunc(*args, **kwargs)
val = cache_get(key, cache_name=cache_name) val = cache_get(key, cache_name=cache_name)
@@ -277,7 +276,7 @@ def generic_bulk_cached_fetch(cache_key_function, # type: Callable[[ObjKT], Tex
if cache_keys[object_id] in cached_objects) if cache_keys[object_id] in cached_objects)
def cache(func): def cache(func):
# type: (FuncT) -> FuncT # type: (Callable[..., ReturnT]) -> Callable[..., ReturnT]
"""Decorator which applies Django caching to a function. """Decorator which applies Django caching to a function.
Uses a key based on the function's name, filename, and Uses a key based on the function's name, filename, and