summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_functional.py
diff options
context:
space:
mode:
authorRan Benita <ran@unusedvar.com>2020-04-26 21:42:07 +0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-06-12 05:45:44 +0200
commitae94077e7d884c26fc92b4b0d248a3b4a541ee44 (patch)
tree20a12d79e4402a5cb717908597f2d2dadf2c621d /tests/utils_tests/test_functional.py
parentb214845f0f8d973e527e41730ca5770fad51ec5d (diff)
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.
Diffstat (limited to 'tests/utils_tests/test_functional.py')
-rw-r--r--tests/utils_tests/test_functional.py14
1 files changed, 4 insertions, 10 deletions
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)