diff options
| author | Theo Alexiou <theofilosalexiou@gmail.com> | 2022-02-05 20:28:09 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-10 11:24:51 +0100 |
| commit | f9ec777a826816e20e68021c0e73b5a76be650af (patch) | |
| tree | 5c1eba3d3fad3e32361d44a8c6fff1559a1c11b9 | |
| parent | 4c76ffc2d6c77c850b4bef8d9acc197d11c47937 (diff) | |
Fixed #26287 -- Added support for addition operations to SimpleLazyObject.
| -rw-r--r-- | django/utils/functional.py | 6 | ||||
| -rw-r--r-- | docs/releases/4.1.txt | 2 | ||||
| -rw-r--r-- | tests/utils_tests/test_lazyobject.py | 11 |
3 files changed, 18 insertions, 1 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py index 9e1be0fe0f..2696dd49c5 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -432,6 +432,12 @@ class SimpleLazyObject(LazyObject): return result return copy.deepcopy(self._wrapped, memo) + __add__ = new_method_proxy(operator.add) + + @new_method_proxy + def __radd__(self, other): + return other + self + def partition(predicate, values): """ diff --git a/docs/releases/4.1.txt b/docs/releases/4.1.txt index 4b62cf09cf..6dd36e498c 100644 --- a/docs/releases/4.1.txt +++ b/docs/releases/4.1.txt @@ -294,7 +294,7 @@ URLs Utilities ~~~~~~~~~ -* ... +* ``SimpleLazyObject`` now supports addition operations. Validators ~~~~~~~~~~ diff --git a/tests/utils_tests/test_lazyobject.py b/tests/utils_tests/test_lazyobject.py index 9ccae595f4..0ff15469d4 100644 --- a/tests/utils_tests/test_lazyobject.py +++ b/tests/utils_tests/test_lazyobject.py @@ -317,6 +317,17 @@ class SimpleLazyObjectTestCase(LazyObjectTestCase): self.assertIsInstance(obj._wrapped, int) self.assertEqual(repr(obj), "<SimpleLazyObject: 42>") + def test_add(self): + obj1 = self.lazy_wrap(1) + self.assertEqual(obj1 + 1, 2) + obj2 = self.lazy_wrap(2) + self.assertEqual(obj2 + obj1, 3) + self.assertEqual(obj1 + obj2, 3) + + def test_radd(self): + obj1 = self.lazy_wrap(1) + self.assertEqual(1 + obj1, 2) + def test_trace(self): # See ticket #19456 old_trace_func = sys.gettrace() |
