Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Django] #36383: Improve migration serialization of functools.partial and functools.partialmethod

8 views
Skip to first unread message

Django

unread,
May 11, 2025, 5:14:09 PMMay 11
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Type:
| Cleanup/optimization
Status: new | Component:
| Migrations
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Currently, the migration serialization framework serializes
`functools.partial()` and `functools.partialmethod()` in a quite
confusing, hard-to-read way. For example, a source object constructed
with:

{{{#!python
functools.partial(datetime.timedelta, 1, seconds=2)
}}}

…is serialized as:

{{{#!python
functools.partial(datetime.timedelta, *(1,), **{'seconds': 2})
}}}

The source includes unnecessary unpacking of a tuple into positional
arguments and a dict into keyword arguments.

The tuple and dict are included even when arguments are empty, for
example:

{{{#!python
functools.partial(int)
}}}

…is serialized as:

{{{#!python
functools.partial(int, *(), **{})
}}}

The result is quite a advanced Python syntax, the kind that I believe
deters people from inspecting and understanding their migration files.

It seems partial serialization has been like this since support was added
in #25185.

I propose that we reuse the logic for deconstructible objects.

While working on this ticket, I realized one gap in this idea: the `**{}`
syntax allows keyword arguments that are not Python identifiers, but that
is not supported by the current deconstructable serialization. Therefore,
the associated PR also adds support for that.
--
Ticket URL: <https://code.djangoproject.com/ticket/36383>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 11, 2025, 5:21:30 PMMay 11
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Johnson):

* owner: (none) => Adam Johnson
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:1>

Django

unread,
May 12, 2025, 4:01:16 AMMay 12
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Thank you for the suggestion. I think this is a desirable clean up which
makes the serialization logic more consistent
--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:2>

Django

unread,
May 12, 2025, 5:05:05 PMMay 12
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:3>

Django

unread,
May 13, 2025, 8:42:29 PMMay 13
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by nessita <124304+nessita@…>):

In [changeset:"0f94ecd49d1af92d47bfa690ca39d4d71b5091cb" 0f94ecd4]:
{{{#!CommitTicketReference repository=""
revision="0f94ecd49d1af92d47bfa690ca39d4d71b5091cb"
Refs #36383, #26151 -- Corrected spelling of DeconstructibleSerializer.

"Deconstructible" is the spelling that Django has settled on, such as
for `django.utils.deconstruct`. This commit normalizes a
previously-inconsistent class to match the rest of the codebase.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:4>

Django

unread,
May 13, 2025, 8:42:29 PMMay 13
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nessita <124304+nessita@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"6e36f7f78415e454dcde1d9ee33f2d2c7fdfdfad" 6e36f7f7]:
{{{#!CommitTicketReference repository=""
revision="6e36f7f78415e454dcde1d9ee33f2d2c7fdfdfad"
Fixed #36383 -- Improved migration serialization for functools.partial
objects.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:7>

Django

unread,
May 13, 2025, 8:42:29 PMMay 13
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by nessita <124304+nessita@…>):

In [changeset:"57fdc104d26df0a060f637f2128d830bfcc8e4f8" 57fdc104]:
{{{#!CommitTicketReference repository=""
revision="57fdc104d26df0a060f637f2128d830bfcc8e4f8"
Refs #36383 -- Added extra tests for serializing functools.partial in
tests/migrations/test_writer.py.

This includes a test helper to better assert over the expected output.

Co-authored-by: Natalia <124304+...@users.noreply.github.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:6>

Django

unread,
May 13, 2025, 8:42:30 PMMay 13
to django-...@googlegroups.com
#36383: Improve migration serialization of functools.partial and
functools.partialmethod
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by nessita <124304+nessita@…>):

In [changeset:"4647e2b8663cbd22a07af70bf0f8540946763851" 4647e2b]:
{{{#!CommitTicketReference repository=""
revision="4647e2b8663cbd22a07af70bf0f8540946763851"
Refs #36383 -- Extended DeconstructibleSerializer to support non-
identifier keyword arguments.

In Python, keyword arguments must normally be valid identifiers (i.e.,
variable names that follow Python's naming rules). However, Python dicts
can have keys that aren't valid identifiers, like "foo-bar" or "123foo".

This commit ensures that keyword arguments that are nt valid
identifiers, are properly handled when deconstructing an object.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36383#comment:5>
Reply all
Reply to author
Forward
0 new messages