From 484117b6261a2a519a572a0a7376bbab4fe15aa7 Mon Sep 17 00:00:00 2001 From: Joris Roovers Date: Sat, 31 Oct 2020 09:56:40 +0100 Subject: Remove Python 2 specific workarounds in caching decorator Python 3 allows for more elegant access to parent function variables using the 'nonlocal' keyword, which allows for cleanup of the caching decorator. Relates to #151 --- gitlint/cache.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/gitlint/cache.py b/gitlint/cache.py index a2b766c..1b6558f 100644 --- a/gitlint/cache.py +++ b/gitlint/cache.py @@ -13,7 +13,7 @@ class PropertyCache: return self._cache[cache_key] -def cache(original_func=None, cachekey=None): +def cache(original_func=None, cachekey=None): # pylint: disable=unused-argument """ Cache decorator. Caches function return values. Requires the parent class to extend and initialize PropertyCache. Usage: @@ -28,27 +28,23 @@ def cache(original_func=None, cachekey=None): ... """ - # Decorators with optional arguments are a bit convoluted in python, especially if you want to support both - # Python 2 and 3. See some of the links below for details. + # Decorators with optional arguments are a bit convoluted in python, see some of the links below for details. def cache_decorator(func): - - # If no specific cache key is given, use the function name as cache key - if not cache_decorator.cachekey: - cache_decorator.cachekey = func.__name__ + # Use 'nonlocal' keyword to access parent function variable: + # https://stackoverflow.com/a/14678445/381010 + nonlocal cachekey + if not cachekey: + cachekey = func.__name__ def wrapped(*args): def cache_func_result(): # Call decorated function and store its result in the cache - args[0]._cache[cache_decorator.cachekey] = func(*args) - return args[0]._try_cache(cache_decorator.cachekey, cache_func_result) + args[0]._cache[cachekey] = func(*args) + return args[0]._try_cache(cachekey, cache_func_result) return wrapped - # Passing parent function variables to child functions requires special voodoo in python2: - # https://stackoverflow.com/a/14678445/381010 - cache_decorator.cachekey = cachekey # attribute on the function - # To support optional kwargs for decorators, we need to check if a function is passed as first argument or not. # https://stackoverflow.com/a/24617244/381010 if original_func: -- cgit v1.2.3