Skip to content
Permalink
Browse files

Fixed #30498 -- Fixed proxy class caching in lazy().

lazy() should prepare the proxy class only once (the first time it's
used) not on every call.

Regression in b4e76f3.
  • Loading branch information...
bluetech authored and felixxm committed May 3, 2019
1 parent b711eaf commit a2c31e12da272acc76f3a3a0157fae9a7f6477ac
Showing with 12 additions and 1 deletion.
  1. +1 −1 django/utils/functional.py
  2. +11 −0 tests/utils_tests/test_functional.py
@@ -79,7 +79,7 @@ def __init__(self, args, kw):
self.__kw = kw
if not self.__prepared:
self.__prepare_class__()
self.__prepared = True
self.__class__.__prepared = True

def __reduce__(self):
return (
@@ -1,3 +1,5 @@
from unittest import mock

from django.test import SimpleTestCase
from django.utils.functional import cached_property, lazy

@@ -207,3 +209,12 @@ 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_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()

0 comments on commit a2c31e1

Please sign in to comment.
You can’t perform that action at this time.