Skip to content

bpo-34160: Make sorting attributes in XML serialization optional. - #10452

Closed
serhiy-storchaka wants to merge 5 commits into
python:masterfrom
serhiy-storchaka:xml-sort_attrs
Closed

serhiy-storchaka wants to merge 5 commits into
python:masterfrom
serhiy-storchaka:xml-sort_attrs

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Nov 10, 2018 •

Copy link
Copy Markdown
Member

@nedbat

nedbat commented Nov 10, 2018

Copy link
Copy Markdown
Member

Wow, that was fast! :)

@@ -1 +1,2 @@
ElementTree and minidom now preserve the attribute order specified by the user.
Added support for the *sort_attrs* argument in methods for serializing XML in
modules :mod:`xml.etree.ElementTree` and :mod:`xml.dom.minidom`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to note both changes: 1) now by default the attribute order is preserved, and 2) there's an option to sort them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the default was not changed. I think it is better first add an option, and change the default in next the version. This will make supporting two consequent versions (3.7-3.8 or 3.8-3.9) easier.

@Mariatta Mariatta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments about the documentation. I'm actually unsure about some of these..

For the :class:`Document` node, an additional keyword argument *encoding* can
be used to specify the encoding field of the XML header.

If *sort_attrs* is true (by default), then element attributes will be

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If *sort_attrs* is true (by default), then element attributes will be
If *sort_attrs* is ``True`` (the default value), then element attributes will be

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the current wording is more correct. Element attributes will be sorted not only when sort_attrs is True, but when its boolean value is true.

"True" is a concrete bool instance, "true" is used when the boolean value should be true, and not just bool instances are accepted.

Comment thread Doc/library/xml.dom.minidom.rst Outdated
Comment thread Doc/library/xml.etree.elementtree.rst Outdated
Comment thread Doc/library/xml.etree.elementtree.rst Outdated
of elements that contain no content. If ``True`` (the default), they are
emitted as a single self-closed tag, otherwise they are emitted as a pair
of start/end tags.
If *sort_attrs* is true (by default), then element attributes will be

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If *sort_attrs* is true (by default), then element attributes will be
If *sort_attrs* is ``True`` (the default value), then element attributes will be

Mariatta and others added 3 commits November 24, 2018 10:37
Co-Authored-By: serhiy-storchaka <storchaka@gmail.com>
Co-Authored-By: serhiy-storchaka <storchaka@gmail.com>
Co-Authored-By: serhiy-storchaka <storchaka@gmail.com>
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you for your review Mariatta.

Although this PR was purposed for demonstration. It should not be merged as is.

Comment thread Doc/library/xml.dom.minidom.rst
Comment thread Doc/library/xml.dom.minidom.rst Outdated
@rhettinger

rhettinger commented Dec 23, 2018 •

Copy link
Copy Markdown
Contributor

I would like to vote against this much API expansion. We will have to live it forever. IMO, we ought to have a single canonicalization tool and everything else should just be order preserving.

AFAICT, the only original reason for having sorting was to produce a deterministic order, not because anyone has ever desired alphabetical ordering. From a user's point of view, there would almost never be a reason to set sort_attrs=True, so being presented with the option would just be noise.

FWIW, at least twice a month, I teach people how to use these APIs and it is already an uphill battle to work through all the existing options. We should be careful with API expansion, because it becomes a permanent promise that is difficult to take away. Each option becomes yet another thing to learn and remember.

I did a quick scan of XML APIs in other languages (such as Go) and did not see a sort_attrs option. That should be an indicator that this isn't an essential feature in this problem domain. The existence of canonicalization tools does indicate how the problem is approached elsewhere.

AFAICT, for applications to test XML generation, the best practices are either to verify that the XML round-trips (like when we test obj==pickle.loads(pickle.dumps(obj)) or to use a standards compliant canonicalization tool.

@scoder

scoder commented Apr 16, 2019

Copy link
Copy Markdown
Contributor

Rejected, as discussed in the ticket.

@scoder scoder closed this Apr 16, 2019
@serhiy-storchaka
serhiy-storchaka deleted the xml-sort_attrs branch July 1, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants