[issue9767] Failures in json doc examples

New submission from Terry J. Reedy <tjreedy@udel.edu>: I ran doctest on LibRef 17.2 json saved as .txt. There are 4 failures: 2 are clearly doc issues, the other 2 I do not know. I hope someone reads this who is already familiar DOC PATCHES dumps(2 + 1j, cls=ComplexEncoder) should be json.dumps(2 + 1j, cls=ComplexEncoder) and JSONEncoder().encode({"foo": ["bar", "baz"]}) should be json.JSONEncoder().encode({"foo": ["bar", "baz"]}) Needing similar additions in example code (not caught by doctest) are return JSONEncoder.default(self, o) for chunk in JSONEncoder().iterencode(bigobject): Those fixes leave 2 failures I an unclear about and hope a json expert will comment on. 1.''' Failed example: print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)) Expected: { "4": 5, "6": 7 } Got: { "4": 5, "6": 7 } ''' The difference is that json puts a trailing space after '5,'. Should it? or should one be added to the doc? 2. ''' Failed example: list(ComplexEncoder().iterencode(2 + 1j)) Expected: ['[', '2.0', ', ', '1.0', ']'] Got: ['[2.0', ', 1.0', ']'] ''' Without knowing how the default iterencode interacts with the overwridden .default() method, I have no idea. ---------- assignee: docs@python components: Documentation, Library (Lib) messages: 115512 nosy: docs@python, terry.reedy priority: normal severity: normal status: open title: Failures in json doc examples type: behavior _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue9767> _______________________________________

Georg Brandl <georg@python.org> added the comment: r84481 should fix all except the whitespace one. I don't think this is worth fixing; I will not add a trailing whitespace to the docs, and adding a "doctest: NORMALIZE_WHITESPACE" directive does not help readability, especially since nobody actually runs these doctest-like examples. ---------- nosy: +georg.brandl resolution: -> fixed status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue9767> _______________________________________

Terry J. Reedy <tjreedy@udel.edu> added the comment: Well, anyone who does try that example will see the correct output and not notice the trailing whitespace difference. It will only be an issue if and when automated doc doctests are run repeatedly. Worry about it then. Thanks for the quick response. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue9767> _______________________________________

Georg Brandl <georg@python.org> added the comment: FWIW if something is fixed, it should be the json output. However, I can imagine (without looking at the code) that this would mean a lot of special casing. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue9767> _______________________________________

R. David Murray added the comment: See issue 16333 for the bug report against the json trailing whitespace. ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue9767> _______________________________________
participants (3)
-
Georg Brandl
-
R. David Murray
-
Terry J. Reedy