Refs #34822 -- Added tests for serializing decorated functions in migrations.

Functions decorated with a decorator that is properly wrapped, e.g. by
using `@functools.wraps`, are already supported.
This commit is contained in:
Nick Pope 2023-09-13 15:35:01 +01:00 committed by Natalia
parent 0e540fca13
commit c131949e3e
2 changed files with 22 additions and 1 deletions

View File

@ -783,7 +783,12 @@ Django can serialize the following:
- ``LazyObject`` instances which wrap a serializable value.
- Enumeration types (e.g. ``TextChoices`` or ``IntegerChoices``) instances.
- Any Django field
- Any function or method reference (e.g. ``datetime.datetime.today``) (must be in module's top-level scope)
- Any function or method reference (e.g. ``datetime.datetime.today``) (must be
in module's top-level scope)
- Functions may be decorated if wrapped properly, i.e. using
:func:`functools.wraps`
- Unbound methods used from within the class body
- Any class reference (must be in module's top-level scope)
- Anything with a custom ``deconstruct()`` method (:ref:`see below <custom-deconstruct-method>`)

View File

@ -77,6 +77,19 @@ class IntFlagEnum(enum.IntFlag):
B = 2
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return wrapper
@decorator
def function_with_decorator():
pass
class OperationWriterTests(SimpleTestCase):
def test_empty_signature(self):
operation = custom_migration_operations.operations.TestOperation()
@ -566,6 +579,9 @@ class WriterTests(SimpleTestCase):
self.assertEqual(string, "models.SET(42)")
self.serialize_round_trip(models.SET(42))
def test_serialize_decorated_functions(self):
self.assertSerializedEqual(function_with_decorator)
def test_serialize_datetime(self):
self.assertSerializedEqual(datetime.datetime.now())
self.assertSerializedEqual(datetime.datetime.now)