From 775b796d8d13841059850d73986d5dcc2e593077 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 12 Mar 2021 09:00:39 +0100 Subject: [PATCH] Refs #32508 -- Raised ValueError instead of using "assert" in lazy(). --- django/utils/functional.py | 6 ++++-- tests/utils_tests/test_functional.py | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django/utils/functional.py b/django/utils/functional.py index 5c8a0c233f..5f12aa08ff 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -119,8 +119,10 @@ def lazy(func, *resultclasses): setattr(cls, method_name, meth) cls._delegate_bytes = bytes in resultclasses cls._delegate_text = str in resultclasses - assert not (cls._delegate_bytes and cls._delegate_text), ( - "Cannot call lazy() with both bytes and text return types.") + if cls._delegate_bytes and cls._delegate_text: + raise ValueError( + 'Cannot call lazy() with both bytes and text return types.' + ) if cls._delegate_text: cls.__str__ = cls.__text_cast elif cls._delegate_bytes: diff --git a/tests/utils_tests/test_functional.py b/tests/utils_tests/test_functional.py index 595479a503..b870f98ea2 100644 --- a/tests/utils_tests/test_functional.py +++ b/tests/utils_tests/test_functional.py @@ -224,6 +224,12 @@ class FunctionalTests(SimpleTestCase): lazified() mocked.assert_not_called() + def test_lazy_bytes_and_str_result_classes(self): + lazy_obj = lazy(lambda: 'test', str, bytes) + msg = 'Cannot call lazy() with both bytes and text return types.' + with self.assertRaisesMessage(ValueError, msg): + lazy_obj() + def test_classproperty_getter(self): class Foo: foo_attr = 123