lazy also finds base class methods in the proxy object class base def basemethodself pass class klazzbase pass t lazylambda klazz klazz self assertinbasemethod dirt def testlazybaseclassoverrideself docstring is preserved it s cached the correct value is returned state isn t shared between instances it behaves like a property when there s no instance othervalue doesn t become a property cachedproperty caches its value and behaves like a property class class cachedproperty def valueself here is the docstring return 1 object def othervalueself cachedproperty caches its value and behaves like a property on mangled methods or when the name kwarg isn t set here is the docstring return 1 object def othervalueself disallow this case because the decorated function wouldn t be cached typemsg cannot assign the same cachedproperty to two different names a and b if py312 errortype typeerror msg typemsg else errortype runtimeerror msg error calling setname with self assertraisesmessageerrortype msg as ctx class reusedcachedproperty cachedproperty def aself pass b a if not py312 self assertequalstrctx exception context strtypeerrortypemsg def testcachedpropertyreusesamenameself counter 0 cachedproperty def cpself nonlocal counter counter 1 return counter class a cp cp class b cp cp a a b b self assertequala cp 1 self assertequalb cp 2 self assertequala cp 1 def testcachedpropertysetnamenotcalledself cp cachedpropertylambda s none class foo pass foo cp cp msg cannot use cachedproperty instance without calling setname on it with self assertraisesmessagetypeerror msg foo cp def testlazyaddintself lazy4 lazylambda 4 int lazy5 lazylambda 5 int self assertequal4 lazy5 9 self assertequallazy4 5 9 self assertequallazy4 lazy5 9 def testlazyaddlistself lazy4 lazylambda 4 list lazy5 lazylambda 5 list self assertequal4 lazy5 4 5 self assertequallazy4 5 4 5 self assertequallazy4 lazy5 4 5 def testlazyaddstrself lazya lazylambda a str lazyb lazylambda b str self assertequala lazyb ab self assertequallazya b ab self assertequallazya lazyb ab def testlazymodintself lazy4 lazylambda 4 int lazy5 lazylambda 5 int self assertequal4 lazy5 4 self assertequallazy4 5 4 self assertequallazy4 lazy5 4 def testlazymodstrself lazya lazylambda as str lazyb lazylambda b str self assertequalas lazyb ab self assertequallazya b ab self assertequallazya lazyb ab def testlazymulintself lazy4 lazylambda 4 int lazy5 lazylambda 5 int self assertequal4 lazy5 20 self assertequallazy4 5 20 self assertequallazy4 lazy5 20 def testlazymullistself lazy4 lazylambda 4 list lazy5 lazylambda 5 int self assertequal4 lazy5 4 4 4 4 4 self assertequallazy4 5 4 4 4 4 4 self assertequallazy4 lazy5 4 4 4 4 4 def testlazymulstrself lazya lazylambda a str lazy5 lazylambda 5 int self assertequala lazy5 aaaaa self assertequallazya 5 aaaaa self assertequallazya lazy5 aaaaa def testlazyformatself class quotedstringstr def formatself formatspec value super formatformatspec return fvalue lazyf lazylambda quotedstringhello quotedstring self assertequalformatlazyf hello f lazyf self assertequalfi said f i said hello def testlazyequalityself lazya lazylambda 4 int lazyb lazylambda 4 int lazyc lazylambda 5 int self assertequallazya lazyb self assertnotequallazyb lazyc def testlazyreprtextself originalobject lazy translation text lazyobj lazylambda originalobject str self assertequalreproriginalobject reprlazyobj def testlazyreprintself originalobject 15 lazyobj lazylambda originalobject int self assertequalreproriginalobject reprlazyobj def testlazyreprbytesself originalobject bjxc3xbcst a strxc3xadng lazyobj lazylambda originalobject bytes self assertequalreproriginalobject reprlazyobj def testlazyregularmethodself originalobject 15 lazyobj lazylambda originalobject int self assertequaloriginalobject bitlength lazyobj bitlength def testlazybytesandstrresultclassesself lazyobj lazylambda test str bytes self assertequalstrlazyobj test def testlazystrcastmixedresulttypesself lazyvalue lazylambda 1 str list self assertequalstrlazyvalue 1 def testlazystrcastmixedbytesresulttypesself lazyvalue lazylambda 1 bytes list self assertequalstrlazyvalue 1 def testclasspropertygetterself class foo fooattr 123 def initself self fooattr 456 classproperty def foocls return cls fooattr class bar bar classproperty bar getter def barcls return 123 self assertequalfoo foo 123 self assertequalfoo foo 123 self assertequalbar bar 123 self assertequalbar bar 123 def testclasspropertyoverridegetterself class foo classproperty def foocls return 123 foo getter def foocls return 456 self assertequalfoo foo 456 self assertequalfoo foo 456 lazy also finds base class methods in the proxy object lazy finds the correct overridden method implementation docstring is preserved it s cached the correct value is returned state isn t shared between instances it behaves like a property when there s no instance other_value doesn t become a property cached_property caches its value and behaves like a property here is the docstring here is the docstring here is the docstring cached_property caches its value and behaves like a property on mangled methods or when the name kwarg isn t set here is the docstring here is the docstring disallow this case because the decorated function wouldn t be cached reusing a cached_property on different classes under the same name is allowed and work correctly for promises
from django.test import SimpleTestCase
from django.utils.functional import cached_property, classproperty, lazy
from django.utils.version import PY312


class FunctionalTests(SimpleTestCase):
    def test_lazy(self):
        t = lazy(lambda: tuple(range(3)), list, tuple)
        for a, b in zip(t(), range(3)):
            self.assertEqual(a, b)

    def test_lazy_base_class(self):
        

        class Base:
            def base_method(self):
                pass

        class Klazz(Base):
            pass

        t = lazy(lambda: Klazz(), Klazz)()
        self.assertIn("base_method", dir(t))

    def test_lazy_base_class_override(self):
        

        class Base:
            def method(self):
                return "Base"

        class Klazz(Base):
            def method(self):
                return "Klazz"

        t = lazy(lambda: Klazz(), Base)()
        self.assertEqual(t.method(), "Klazz")

    def test_lazy_object_to_string(self):
        class Klazz:
            def __str__(self):
                return "Î am ā Ǩlâzz."

            def __bytes__(self):
                return b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz."

        t = lazy(lambda: Klazz(), Klazz)()
        self.assertEqual(str(t), "Î am ā Ǩlâzz.")
        self.assertEqual(bytes(t), b"\xc3\x8e am \xc4\x81 binary \xc7\xa8l\xc3\xa2zz.")

    def assertCachedPropertyWorks(self, attr, Class):
        with self.subTest(attr=attr):

            def get(source):
                return getattr(source, attr)

            obj = Class()

            class SubClass(Class):
                pass

            subobj = SubClass()
            
            self.assertEqual(get(Class).__doc__, "Here is the docstring...")
            self.assertEqual(get(SubClass).__doc__, "Here is the docstring...")
            
            self.assertEqual(get(obj), get(obj))
            self.assertEqual(get(subobj), get(subobj))
            
            self.assertEqual(get(obj)[0], 1)
            self.assertEqual(get(subobj)[0], 1)
            
            obj2 = Class()
            subobj2 = SubClass()
            self.assertNotEqual(get(obj), get(obj2))
            self.assertNotEqual(get(subobj), get(subobj2))
            
            self.assertIsInstance(get(Class), cached_property)
            self.assertIsInstance(get(SubClass), cached_property)
            
            self.assertTrue(callable(obj.other_value))
            self.assertTrue(callable(subobj.other_value))

    def test_cached_property(self):
        

        class Class:
            @cached_property
            def value(self):
                
                return 1, object()

            @cached_property
            def __foo__(self):
                
                return 1, object()

            def other_value(self):
                
                return 1, object()

            other = cached_property(other_value)

        attrs = ["value", "other", "__foo__"]
        for attr in attrs:
            self.assertCachedPropertyWorks(attr, Class)

    def test_cached_property_auto_name(self):
        

        class Class:
            @cached_property
            def __value(self):
                
                return 1, object()

            def other_value(self):
                
                return 1, object()

            other = cached_property(other_value)

        attrs = ["_Class__value", "other"]
        for attr in attrs:
            self.assertCachedPropertyWorks(attr, Class)

    def test_cached_property_reuse_different_names(self):
        
        type_msg = (
            "Cannot assign the same cached_property to two different names ('a' and "
            "'b')."
        )
        if PY312:
            error_type = TypeError
            msg = type_msg
        else:
            error_type = RuntimeError
            msg = "Error calling __set_name__"

        with self.assertRaisesMessage(error_type, msg) as ctx:

            class ReusedCachedProperty:
                @cached_property
                def a(self):
                    pass

                b = a

        if not PY312:
            self.assertEqual(str(ctx.exception.__context__), str(TypeError(type_msg)))

    def test_cached_property_reuse_same_name(self):
        
        counter = 0

        @cached_property
        def _cp(_self):
            nonlocal counter
            counter += 1
            return counter

        class A:
            cp = _cp

        class B:
            cp = _cp

        a = A()
        b = B()
        self.assertEqual(a.cp, 1)
        self.assertEqual(b.cp, 2)
        self.assertEqual(a.cp, 1)

    def test_cached_property_set_name_not_called(self):
        cp = cached_property(lambda s: None)

        class Foo:
            pass

        Foo.cp = cp
        msg = (
            "Cannot use cached_property instance without calling __set_name__() on it."
        )
        with self.assertRaisesMessage(TypeError, msg):
            Foo().cp

    def test_lazy_add_int(self):
        lazy_4 = lazy(lambda: 4, int)
        lazy_5 = lazy(lambda: 5, int)
        self.assertEqual(4 + lazy_5(), 9)
        self.assertEqual(lazy_4() + 5, 9)
        self.assertEqual(lazy_4() + lazy_5(), 9)

    def test_lazy_add_list(self):
        lazy_4 = lazy(lambda: [4], list)
        lazy_5 = lazy(lambda: [5], list)
        self.assertEqual([4] + lazy_5(), [4, 5])
        self.assertEqual(lazy_4() + [5], [4, 5])
        self.assertEqual(lazy_4() + lazy_5(), [4, 5])

    def test_lazy_add_str(self):
        lazy_a = lazy(lambda: "a", str)
        lazy_b = lazy(lambda: "b", str)
        self.assertEqual("a" + lazy_b(), "ab")
        self.assertEqual(lazy_a() + "b", "ab")
        self.assertEqual(lazy_a() + lazy_b(), "ab")

    def test_lazy_mod_int(self):
        lazy_4 = lazy(lambda: 4, int)
        lazy_5 = lazy(lambda: 5, int)
        self.assertEqual(4 % lazy_5(), 4)
        self.assertEqual(lazy_4() % 5, 4)
        self.assertEqual(lazy_4() % lazy_5(), 4)

    def test_lazy_mod_str(self):
        lazy_a = lazy(lambda: "a%s", str)
        lazy_b = lazy(lambda: "b", str)
        self.assertEqual("a%s" % lazy_b(), "ab")
        self.assertEqual(lazy_a() % "b", "ab")
        self.assertEqual(lazy_a() % lazy_b(), "ab")

    def test_lazy_mul_int(self):
        lazy_4 = lazy(lambda: 4, int)
        lazy_5 = lazy(lambda: 5, int)
        self.assertEqual(4 * lazy_5(), 20)
        self.assertEqual(lazy_4() * 5, 20)
        self.assertEqual(lazy_4() * lazy_5(), 20)

    def test_lazy_mul_list(self):
        lazy_4 = lazy(lambda: [4], list)
        lazy_5 = lazy(lambda: 5, int)
        self.assertEqual([4] * lazy_5(), [4, 4, 4, 4, 4])
        self.assertEqual(lazy_4() * 5, [4, 4, 4, 4, 4])
        self.assertEqual(lazy_4() * lazy_5(), [4, 4, 4, 4, 4])

    def test_lazy_mul_str(self):
        lazy_a = lazy(lambda: "a", str)
        lazy_5 = lazy(lambda: 5, int)
        self.assertEqual("a" * lazy_5(), "aaaaa")
        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):
        
        lazy_a = lazy(lambda: 4, int)
        lazy_b = lazy(lambda: 4, int)
        lazy_c = lazy(lambda: 5, int)

        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, str)
        self.assertEqual(repr(original_object), repr(lazy_obj()))

    def test_lazy_repr_int(self):
        original_object = 15
        lazy_obj = lazy(lambda: original_object, int)
        self.assertEqual(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.assertEqual(repr(original_object), repr(lazy_obj()))

    def test_lazy_regular_method(self):
        original_object = 15
        lazy_obj = lazy(lambda: original_object, int)
        self.assertEqual(original_object.bit_length(), lazy_obj().bit_length())

    def test_lazy_bytes_and_str_result_classes(self):
        lazy_obj = lazy(lambda: "test", str, bytes)
        self.assertEqual(str(lazy_obj()), "test")

    def test_lazy_str_cast_mixed_result_types(self):
        lazy_value = lazy(lambda: [1], str, list)()
        self.assertEqual(str(lazy_value), "[1]")

    def test_lazy_str_cast_mixed_bytes_result_types(self):
        lazy_value = lazy(lambda: [1], bytes, list)()
        self.assertEqual(str(lazy_value), "[1]")

    def test_classproperty_getter(self):
        class Foo:
            foo_attr = 123

            def __init__(self):
                self.foo_attr = 456

            @classproperty
            def foo(cls):
                return cls.foo_attr

        class Bar:
            bar = classproperty()

            @bar.getter
            def bar(cls):
                return 123

        self.assertEqual(Foo.foo, 123)
        self.assertEqual(Foo().foo, 123)
        self.assertEqual(Bar.bar, 123)
        self.assertEqual(Bar().bar, 123)

    def test_classproperty_override_getter(self):
        class Foo:
            @classproperty
            def foo(cls):
                return 123

            @foo.getter
            def foo(cls):
                return 456

        self.assertEqual(Foo.foo, 456)
        self.assertEqual(Foo().foo, 456)