mirror of
https://github.com/django/django.git
synced 2025-06-06 12:09:11 +00:00
Fixed #34822 -- Added support for serializing functions decorated with functools.lru_cache in migrations.
`@functools.cache` and `@functools.lru_cache` return an object of type `functools._lru_cache_wrapper` which prevented the migrations serializer from working. Simply using the existing `FunctionTypeSerializer` for this additional type works as expected.
This commit is contained in:
parent
c131949e3e
commit
f92e68c30a
@ -350,6 +350,7 @@ class Serializer:
|
|||||||
types.FunctionType,
|
types.FunctionType,
|
||||||
types.BuiltinFunctionType,
|
types.BuiltinFunctionType,
|
||||||
types.MethodType,
|
types.MethodType,
|
||||||
|
functools._lru_cache_wrapper,
|
||||||
): FunctionTypeSerializer,
|
): FunctionTypeSerializer,
|
||||||
collections.abc.Iterable: IterableSerializer,
|
collections.abc.Iterable: IterableSerializer,
|
||||||
(COMPILED_REGEX_TYPE, RegexObject): RegexSerializer,
|
(COMPILED_REGEX_TYPE, RegexObject): RegexSerializer,
|
||||||
|
@ -401,7 +401,9 @@ Management Commands
|
|||||||
Migrations
|
Migrations
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
|
|
||||||
* ...
|
* Serialization of functions decorated with :func:`functools.cache` or
|
||||||
|
:func:`functools.lru_cache` is now supported without the need to write a
|
||||||
|
custom serializer.
|
||||||
|
|
||||||
Models
|
Models
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
@ -788,6 +788,8 @@ Django can serialize the following:
|
|||||||
|
|
||||||
- Functions may be decorated if wrapped properly, i.e. using
|
- Functions may be decorated if wrapped properly, i.e. using
|
||||||
:func:`functools.wraps`
|
:func:`functools.wraps`
|
||||||
|
- The :func:`functools.cache` and :func:`functools.lru_cache` decorators are
|
||||||
|
explicitly supported
|
||||||
|
|
||||||
- Unbound methods used from within the class body
|
- Unbound methods used from within the class body
|
||||||
- Any class reference (must be in module's top-level scope)
|
- Any class reference (must be in module's top-level scope)
|
||||||
@ -797,6 +799,11 @@ Django can serialize the following:
|
|||||||
|
|
||||||
Serialization support for ``enum.Flag`` was added.
|
Serialization support for ``enum.Flag`` was added.
|
||||||
|
|
||||||
|
.. versionchanged:: 5.0
|
||||||
|
|
||||||
|
Serialization support for functions decorated with :func:`functools.cache`
|
||||||
|
or :func:`functools.lru_cache` was added.
|
||||||
|
|
||||||
Django cannot serialize:
|
Django cannot serialize:
|
||||||
|
|
||||||
- Nested classes
|
- Nested classes
|
||||||
|
@ -90,6 +90,16 @@ def function_with_decorator():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@functools.cache
|
||||||
|
def function_with_cache():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=10)
|
||||||
|
def function_with_lru_cache():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class OperationWriterTests(SimpleTestCase):
|
class OperationWriterTests(SimpleTestCase):
|
||||||
def test_empty_signature(self):
|
def test_empty_signature(self):
|
||||||
operation = custom_migration_operations.operations.TestOperation()
|
operation = custom_migration_operations.operations.TestOperation()
|
||||||
@ -581,6 +591,8 @@ class WriterTests(SimpleTestCase):
|
|||||||
|
|
||||||
def test_serialize_decorated_functions(self):
|
def test_serialize_decorated_functions(self):
|
||||||
self.assertSerializedEqual(function_with_decorator)
|
self.assertSerializedEqual(function_with_decorator)
|
||||||
|
self.assertSerializedEqual(function_with_cache)
|
||||||
|
self.assertSerializedEqual(function_with_lru_cache)
|
||||||
|
|
||||||
def test_serialize_datetime(self):
|
def test_serialize_datetime(self):
|
||||||
self.assertSerializedEqual(datetime.datetime.now())
|
self.assertSerializedEqual(datetime.datetime.now())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user