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 zachrahan
Recipients josh.r, tanzer@swing.co.at, zachrahan
Date 2017-06-29.02:50:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1498704649.33.0.0490832578159.issue25457@psf.upfronthosting.co.za>
In-reply-to
Content
This one just bit me too. It seems that if JSON serialization accepts non-string dict keys, it should make sure to accept them in all circumstances. Currently, there is an error *only* with mixed-type dicts, *only* when sort_keys=True.

In addition, the error raised in such cases is especially unhelpful. Running the following:
json.dumps({3:1, 'foo':'bar'}, sort_keys=True)

produces a stack trace that terminates in a function defined in C, with this error:
TypeError: '<' not supported between instances of 'str' and 'int'

That error doesn't give non-experts very much to go on...!

The fix is reasonably simple: coerce dict keys to strings *before* trying to sort the keys, not after. The only fuss in making such a patch is that the behavior has to be fixed in both _json.c and in json/encode.py.

The only other consistent behavior would be to disallow non-string keys, but that behavior is at this point very well entrenched. So it only makes sense that encoding should be patched to not fail in corner cases.
History
Date User Action Args
2017-06-29 02:50:49zachrahansetrecipients: + zachrahan, josh.r, tanzer@swing.co.at
2017-06-29 02:50:49zachrahansetmessageid: <1498704649.33.0.0490832578159.issue25457@psf.upfronthosting.co.za>
2017-06-29 02:50:49zachrahanlinkissue25457 messages
2017-06-29 02:50:48zachrahancreate