diff options
| author | Chesco Igual <kreymagno@gmail.com> | 2016-06-04 16:13:00 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-06-04 19:13:00 -0400 |
| commit | ffd18732f3ee9e6f0374aff9ccf350d85187fac2 (patch) | |
| tree | a89559d9a896de0d4d07f72dfa112b5fcb0d6669 | |
| parent | 2f9c4e2b6fab3ce08cfbebff79d758196250790c (diff) | |
Fixed #24781 -- Fixed repr() for lazy objects.
| -rw-r--r-- | django/utils/functional.py | 3 | ||||
| -rw-r--r-- | tests/utils_tests/test_functional.py | 15 |
2 files changed, 18 insertions, 0 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py index f674a9bba2..ee301879df 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -75,6 +75,9 @@ def lazy(func, *resultclasses): (func, self.__args, self.__kw) + resultclasses ) + def __repr__(self): + return repr(self.__cast()) + @classmethod def __prepare_class__(cls): for resultclass in resultclasses: diff --git a/tests/utils_tests/test_functional.py b/tests/utils_tests/test_functional.py index 66a6f59cf6..2cf399f892 100644 --- a/tests/utils_tests/test_functional.py +++ b/tests/utils_tests/test_functional.py @@ -130,3 +130,18 @@ class FunctionalTestCase(unittest.TestCase): self.assertEqual(lazy_a(), lazy_b()) self.assertNotEqual(lazy_b(), lazy_c()) + + def test_lazy_repr_text(self): + original_object = 'Lazy translation text' + lazy_obj = lazy(lambda: original_object, six.text_type) + self.assertEquals(repr(original_object), repr(lazy_obj())) + + def test_lazy_repr_int(self): + original_object = 15 + lazy_obj = lazy(lambda: original_object, int) + self.assertEquals(repr(original_object), repr(lazy_obj())) + + def test_lazy_repr_bytes(self): + original_object = b'J\xc3\xbcst a str\xc3\xadng' + lazy_obj = lazy(lambda: original_object, bytes) + self.assertEquals(repr(original_object), repr(lazy_obj())) |
