Bläddra i källkod

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.
Nick Pope 1 år sedan
förälder
incheckning
c131949e3e
2 ändrade filer med 22 tillägg och 1 borttagningar
  1. 6 1
      docs/topics/migrations.txt
  2. 16 0
      tests/migrations/test_writer.py

+ 6 - 1
docs/topics/migrations.txt

@@ -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>`)

+ 16 - 0
tests/migrations/test_writer.py

@@ -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)