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.

classification
Title: Failures in json doc examples
Type: behavior Stage:
Components: Documentation, Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, georg.brandl, r.david.murray, terry.reedy
Priority: normal Keywords:

Created on 2010-09-03 21:32 by terry.reedy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg115512 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-09-03 21:32
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.
msg115522 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-09-03 22:38
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.
msg115541 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-09-03 23:19
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.
msg115627 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-09-05 07:06
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.
msg173896 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-10-26 22:11
See issue 16333 for the bug report against the json trailing whitespace.
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 53976
2012-10-26 22:11:59r.david.murraysetnosy: + r.david.murray
messages: + msg173896
2010-09-05 07:06:06georg.brandlsetmessages: + msg115627
2010-09-03 23:19:20terry.reedysetmessages: + msg115541
2010-09-03 22:38:21georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg115522

resolution: fixed
2010-09-03 21:32:51terry.reedycreate