bpo-34160: Make sorting attributes in XML serialization optional. by serhiy-storchaka · Pull Request #10452 · python/cpython
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.