summaryrefslogtreecommitdiff
path: root/django/utils/functional.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/functional.py')
-rw-r--r--django/utils/functional.py32
1 files changed, 23 insertions, 9 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py
index ea46cff20f..9e1be0fe0f 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -13,13 +13,14 @@ class cached_property:
A cached property can be made out of an existing method:
(e.g. ``url = cached_property(get_absolute_url)``).
"""
+
name = None
@staticmethod
def func(instance):
raise TypeError(
- 'Cannot use cached_property instance without calling '
- '__set_name__() on it.'
+ "Cannot use cached_property instance without calling "
+ "__set_name__() on it."
)
def __init__(self, func, name=None):
@@ -33,7 +34,7 @@ class cached_property:
stacklevel=2,
)
self.real_func = func
- self.__doc__ = getattr(func, '__doc__')
+ self.__doc__ = getattr(func, "__doc__")
def __set_name__(self, owner, name):
if self.name is None:
@@ -62,6 +63,7 @@ class classproperty:
Decorator that converts a method with a single cls argument into a property
that can be accessed directly from the class.
"""
+
def __init__(self, method=None):
self.fget = method
@@ -78,6 +80,7 @@ class Promise:
Base class for the proxy class created in the closure of the lazy function.
It's used to recognize promises in code.
"""
+
pass
@@ -96,6 +99,7 @@ def lazy(func, *resultclasses):
called on the result of that function. The function is not evaluated
until one of the methods on the result is called.
"""
+
__prepared = False
def __init__(self, args, kw):
@@ -108,7 +112,7 @@ def lazy(func, *resultclasses):
def __reduce__(self):
return (
_lazy_proxy_unpickle,
- (func, self.__args, self.__kw) + resultclasses
+ (func, self.__args, self.__kw) + resultclasses,
)
def __repr__(self):
@@ -129,7 +133,7 @@ def lazy(func, *resultclasses):
cls._delegate_text = str in resultclasses
if cls._delegate_bytes and cls._delegate_text:
raise ValueError(
- 'Cannot call lazy() with both bytes and text return types.'
+ "Cannot call lazy() with both bytes and text return types."
)
if cls._delegate_text:
cls.__str__ = cls.__text_cast
@@ -144,6 +148,7 @@ def lazy(func, *resultclasses):
# applies the given magic method of the result type.
res = func(*self.__args, **self.__kw)
return getattr(res, method_name)(*args, **kw)
+
return __wrapper__
def __text_cast(self):
@@ -233,10 +238,15 @@ def keep_lazy(*resultclasses):
@wraps(func)
def wrapper(*args, **kwargs):
- if any(isinstance(arg, Promise) for arg in itertools.chain(args, kwargs.values())):
+ if any(
+ isinstance(arg, Promise)
+ for arg in itertools.chain(args, kwargs.values())
+ ):
return lazy_func(*args, **kwargs)
return func(*args, **kwargs)
+
return wrapper
+
return decorator
@@ -255,6 +265,7 @@ def new_method_proxy(func):
if self._wrapped is empty:
self._setup()
return func(self._wrapped, *args)
+
return inner
@@ -297,7 +308,9 @@ class LazyObject:
"""
Must be implemented by subclasses to initialize the wrapped object.
"""
- raise NotImplementedError('subclasses of LazyObject must provide a _setup() method')
+ raise NotImplementedError(
+ "subclasses of LazyObject must provide a _setup() method"
+ )
# Because we have messed with __class__ below, we confuse pickle as to what
# class we are pickling. We're going to have to initialize the wrapped
@@ -376,6 +389,7 @@ class SimpleLazyObject(LazyObject):
Designed for compound objects of unknown type. For builtins or objects of
known type, use django.utils.functional.lazy.
"""
+
def __init__(self, func):
"""
Pass in a callable that returns the object to be wrapped.
@@ -385,7 +399,7 @@ class SimpleLazyObject(LazyObject):
callable can be safely run more than once and will return the same
value.
"""
- self.__dict__['_setupfunc'] = func
+ self.__dict__["_setupfunc"] = func
super().__init__()
def _setup(self):
@@ -398,7 +412,7 @@ class SimpleLazyObject(LazyObject):
repr_attr = self._setupfunc
else:
repr_attr = self._wrapped
- return '<%s: %r>' % (type(self).__name__, repr_attr)
+ return "<%s: %r>" % (type(self).__name__, repr_attr)
def __copy__(self):
if self._wrapped is empty: