From ae94077e7d884c26fc92b4b0d248a3b4a541ee44 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 26 Apr 2020 21:42:07 +0300 Subject: Made proxy class in lazy() prepare eagerly. Previously, the proxy class was prepared lazily: lazy_identity = lazy(identity, int) lazy_identity(10) # prepared here lazy_identity(10) This has a slight advantage that if the lazy doesn't end up getting used, the preparation work is skipped, however that's not very likely. Besides this laziness, it is also inconsistent in that the methods which are wrapped directly (__str__ etc.) are prepared already when __proxy__ is defined, and there is a weird half-initialized state. This change it so that everything is prepared already on the first line of the example above. --- tests/utils_tests/test_functional.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'tests/utils_tests/test_functional.py') diff --git a/tests/utils_tests/test_functional.py b/tests/utils_tests/test_functional.py index d49c2a5b49..0fc6ee97da 100644 --- a/tests/utils_tests/test_functional.py +++ b/tests/utils_tests/test_functional.py @@ -1,5 +1,3 @@ -from unittest import mock - from django.test import SimpleTestCase from django.utils.functional import cached_property, classproperty, lazy from django.utils.version import PY312 @@ -273,14 +271,10 @@ class FunctionalTests(SimpleTestCase): lazy_obj = lazy(lambda: original_object, bytes) self.assertEqual(repr(original_object), repr(lazy_obj())) - def test_lazy_class_preparation_caching(self): - # lazy() should prepare the proxy class only once i.e. the first time - # it's used. - lazified = lazy(lambda: 0, int) - __proxy__ = lazified().__class__ - with mock.patch.object(__proxy__, "__prepare_class__") as mocked: - lazified() - mocked.assert_not_called() + 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) -- cgit v1.3