zachrahan added the comment:
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.
----------
nosy: +zachrahan
versions: +Python 3.7 -Python 3.5
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue25457>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com