From f9ec777a826816e20e68021c0e73b5a76be650af Mon Sep 17 00:00:00 2001 From: Theo Alexiou Date: Sat, 5 Feb 2022 20:28:09 +0100 Subject: [PATCH] Fixed #26287 -- Added support for addition operations to SimpleLazyObject. --- django/utils/functional.py | 6 ++++++ docs/releases/4.1.txt | 2 +- tests/utils_tests/test_lazyobject.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) 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), "") + 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()