Skip to content

Commit

Permalink
Fixed #28870 -- Added support for functools.partialmethod serializati…
Browse files Browse the repository at this point in the history
…on in migrations.
  • Loading branch information
sir-sigurd authored and timgraham committed Dec 6, 2017
1 parent b728ab2 commit 183fb7b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
9 changes: 6 additions & 3 deletions django/db/migrations/serializer.py
Expand Up @@ -171,8 +171,11 @@ def serialize(self):
imports.update(args_imports)
imports.update(keywords_imports)
return (
"functools.partial(%s, *%s, **%s)" % (
func_string, args_string, keywords_string,
'functools.%s(%s, *%s, **%s)' % (
self.value.__class__.__name__,
func_string,
args_string,
keywords_string,
),
imports,
)
Expand Down Expand Up @@ -340,7 +343,7 @@ def serializer_factory(value):
return BaseSimpleSerializer(value)
if isinstance(value, decimal.Decimal):
return DecimalSerializer(value)
if isinstance(value, functools.partial):
if isinstance(value, (functools.partial, functools.partialmethod)):
return FunctoolsPartialSerializer(value)
if isinstance(value, (types.FunctionType, types.BuiltinFunctionType, types.MethodType)):
return FunctionTypeSerializer(value)
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/2.1.txt
Expand Up @@ -150,7 +150,7 @@ Management Commands
Migrations
~~~~~~~~~~

* ...
* Added support for serialization of ``functools.partialmethod`` objects.

Models
~~~~~~
Expand Down
8 changes: 6 additions & 2 deletions docs/topics/migrations.txt
Expand Up @@ -661,15 +661,19 @@ Django can serialize the following:
- ``decimal.Decimal`` instances
- ``enum.Enum`` instances
- ``uuid.UUID`` instances
- ``functools.partial`` instances which have serializable ``func``, ``args``,
and ``keywords`` values.
- :func:`functools.partial` and :class:`functools.partialmethod` instances
which have serializable ``func``, ``args``, and ``keywords`` values.
- ``LazyObject`` instances which wrap a serializable value.
- Any Django field
- Any function or method reference (e.g. ``datetime.datetime.today``) (must be in module's top-level scope)
- 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>`)

.. versionchanged:: 2.1

Serialization support for :class:`functools.partialmethod` was added.

Django cannot serialize:

- Nested classes
Expand Down
8 changes: 8 additions & 0 deletions tests/migrations/test_writer.py
Expand Up @@ -520,6 +520,14 @@ def test_serialize_functools_partial(self):
self.assertEqual(result.args, value.args)
self.assertEqual(result.keywords, value.keywords)

def test_serialize_functools_partialmethod(self):
value = functools.partialmethod(datetime.timedelta, 1, seconds=2)
result = self.serialize_round_trip(value)
self.assertIsInstance(result, functools.partialmethod)
self.assertEqual(result.func, value.func)
self.assertEqual(result.args, value.args)
self.assertEqual(result.keywords, value.keywords)

def test_simple_migration(self):
"""
Tests serializing a simple migration.
Expand Down

0 comments on commit 183fb7b

Please sign in to comment.