This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author andrei.avk
Recipients Aaron Hall, andrei.avk, jedwards, josh.r, naught101, r.david.murray, tanzer@swing.co.at, zachrahan
Date 2021-07-10.15:06:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1625929612.18.0.915573838228.issue25457@roundup.psfhosted.org>
In-reply-to
Content
Some observations:

 - sort_keys arg does a deep sorting of nested dictionaries. It's a bit too much to ask users to do this type of preprocessing manually before dumping to json.

 - the error doesn't seem too onerous to me. 'unorderable types: str() < int()' If uncertain, a user can go to interactive shell and try `1 < "2"`, and then the issue is obvious.

 - to me, current behaviour seems preferable to silently guessing that users wants stringified sorting, especially since it can surface as a problem way down the line.

 - what makes this issue interesting is that in roughly half of cases (I'm guessing) the user will want object sorted and then cast to string and would be surprised if the reverse happened, and in the other half cases the user would want them stringified, then sorted, and would be surprised if that didn't happen.

It depends on the perspective: you may think of the json as a representation of a dict of objects, that just happen to be in json format; or you can think of it as a json document with string keys (of course) that just happen to come from a dict of objects. Both can be valid depending on the use case.

Given all of this, I propose keeping current behavior for the existing arg, and adding another arg for 'stringify then sort' behavior. Then we'll have no silent guessing and the "unorderable" type error message can point the user directly to the new argument.

If the user reads the docs before using this method, they will see two clear options with respective tradeoffs and decide which one to use.

So either by reading the docs or running into the error, the user will have a clear explanation and a clear and convenient solution.
History
Date User Action Args
2021-07-10 15:06:52andrei.avksetrecipients: + andrei.avk, zachrahan, r.david.murray, josh.r, jedwards, tanzer@swing.co.at, Aaron Hall, naught101
2021-07-10 15:06:52andrei.avksetmessageid: <1625929612.18.0.915573838228.issue25457@roundup.psfhosted.org>
2021-07-10 15:06:52andrei.avklinkissue25457 messages
2021-07-10 15:06:51andrei.avkcreate