[issue16057] Subclasses of JSONEncoder should not be insturcted to call JSONEncoder.decode

New submission from Justin Lebar: The JSONEncoder documentation says we can implement our own encoder as:
Later on, we give the following example of how to implement the default method in a subclass of json.JSONEncoder: def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) return JSONEncoder.default(self, o) These are both incorrect, as a quick reading of the source will reveal. JSONEncoder.default() throws for all input values. We should s/JSONEncoder.default/JSONEncoder.encode/ here, I think. ---------- assignee: docs@python components: Documentation messages: 171363 nosy: Justin.Lebar, docs@python priority: normal severity: normal status: open title: Subclasses of JSONEncoder should not be insturcted to call JSONEncoder.decode versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti, petri.lehtinen stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: The implementation clearly says that default method should return a serializable object or calls the base implementation to raise TypeError. So I don't think any of the examples is a bug. ---------- nosy: +kushaldas _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Justin Lebar added the comment: Ah, I see. The examples do what you think they should do, but not for the reason you think they should do it -- the JSON encoding logic calls the encoder's encode() method before calling its default() method. I still think the examples could be improved, perhaps by adding a comment to the effect of # Raises a TypeError. before the call to JSONEncoder.default(). Explicit is better than implicit, after all. :) Thanks for looking at this. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: Ok, I will submit a patch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: Patch to update docstring and documentation with explicit saying that calling JSONEncoder.default raises a TypeError ---------- keywords: +patch Added file: http://bugs.python.org/file27408/explicit_json_doc_update_for_encoder_defaul... _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: The previous patch should be back committed to all 3.x and 2.7 branch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Changes by Éric Araujo <merwok@netwok.org>: ---------- keywords: +needs review stage: needs patch -> patch review versions: +Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Roundup Robot added the comment: New changeset 5d56e1214e95 by R David Murray in branch '3.2': #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/5d56e1214e95 New changeset 5f76e7db97ac by R David Murray in branch '3.3': Merge #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/5f76e7db97ac New changeset 406c6fd7e753 by R David Murray in branch 'default': Merge #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/406c6fd7e753 New changeset ef8ea052bcc4 by R David Murray in branch '2.7': #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/ef8ea052bcc4 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

R. David Murray added the comment: Thanks Kushal. ---------- nosy: +r.david.murray resolution: -> fixed stage: patch review -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti, petri.lehtinen stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: The implementation clearly says that default method should return a serializable object or calls the base implementation to raise TypeError. So I don't think any of the examples is a bug. ---------- nosy: +kushaldas _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Justin Lebar added the comment: Ah, I see. The examples do what you think they should do, but not for the reason you think they should do it -- the JSON encoding logic calls the encoder's encode() method before calling its default() method. I still think the examples could be improved, perhaps by adding a comment to the effect of # Raises a TypeError. before the call to JSONEncoder.default(). Explicit is better than implicit, after all. :) Thanks for looking at this. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: Ok, I will submit a patch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: Patch to update docstring and documentation with explicit saying that calling JSONEncoder.default raises a TypeError ---------- keywords: +patch Added file: http://bugs.python.org/file27408/explicit_json_doc_update_for_encoder_defaul... _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Kushal Das added the comment: The previous patch should be back committed to all 3.x and 2.7 branch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Changes by Éric Araujo <merwok@netwok.org>: ---------- keywords: +needs review stage: needs patch -> patch review versions: +Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

Roundup Robot added the comment: New changeset 5d56e1214e95 by R David Murray in branch '3.2': #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/5d56e1214e95 New changeset 5f76e7db97ac by R David Murray in branch '3.3': Merge #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/5f76e7db97ac New changeset 406c6fd7e753 by R David Murray in branch 'default': Merge #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/406c6fd7e753 New changeset ef8ea052bcc4 by R David Murray in branch '2.7': #16057: Clarify why the base method default is called in custom encoders. http://hg.python.org/cpython/rev/ef8ea052bcc4 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________

R. David Murray added the comment: Thanks Kushal. ---------- nosy: +r.david.murray resolution: -> fixed stage: patch review -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue16057> _______________________________________
participants (6)
-
Ezio Melotti
-
Justin Lebar
-
Kushal Das
-
R. David Murray
-
Roundup Robot
-
Éric Araujo