Refs #32508 -- Raised ValueError instead of using "assert" in lazy().

This commit is contained in:
Hasan Ramezani 2021-03-12 09:00:39 +01:00 committed by Mariusz Felisiak
parent 2cd0ccef04
commit 775b796d8d
2 changed files with 10 additions and 2 deletions

View File

@ -119,8 +119,10 @@ def lazy(func, *resultclasses):
setattr(cls, method_name, meth) setattr(cls, method_name, meth)
cls._delegate_bytes = bytes in resultclasses cls._delegate_bytes = bytes in resultclasses
cls._delegate_text = str in resultclasses cls._delegate_text = str in resultclasses
assert not (cls._delegate_bytes and cls._delegate_text), ( if cls._delegate_bytes and cls._delegate_text:
"Cannot call lazy() with both bytes and text return types.") raise ValueError(
'Cannot call lazy() with both bytes and text return types.'
)
if cls._delegate_text: if cls._delegate_text:
cls.__str__ = cls.__text_cast cls.__str__ = cls.__text_cast
elif cls._delegate_bytes: elif cls._delegate_bytes:

View File

@ -224,6 +224,12 @@ class FunctionalTests(SimpleTestCase):
lazified() lazified()
mocked.assert_not_called() 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): def test_classproperty_getter(self):
class Foo: class Foo:
foo_attr = 123 foo_attr = 123