1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Allowed custom formatting of lazy() objects.

This allows for formatting of lazy objects which have a custom formatter
defined by overriding the default implementation from `object`.
This commit is contained in:
Nick Pope
2023-06-07 12:34:35 +01:00
committed by Mariusz Felisiak
parent fd97b0471b
commit e042024b28
2 changed files with 14 additions and 0 deletions

View File

@@ -246,6 +246,17 @@ class FunctionalTests(SimpleTestCase):
self.assertEqual(lazy_a() * 5, "aaaaa")
self.assertEqual(lazy_a() * lazy_5(), "aaaaa")
def test_lazy_format(self):
class QuotedString(str):
def __format__(self, format_spec):
value = super().__format__(format_spec)
return f"{value}"
lazy_f = lazy(lambda: QuotedString("Hello!"), QuotedString)
self.assertEqual(format(lazy_f(), ""), "“Hello!”")
f = lazy_f()
self.assertEqual(f"I said, {f}", "I said, “Hello!”")
def test_lazy_equality(self):
"""
== and != work correctly for Promises.