[issue12657] Cannot override JSON encoding of basic type subclasses

Barry A. Warsaw report at bugs.python.org
Fri Jul 29 20:28:08 CEST 2011


New submission from Barry A. Warsaw <barry at python.org>:

I found this out while experimenting with enum types that inherit from int.  The json library provides for extending the encoder to handle non-basic types; in those cases, your class's .default() method is called.  However, it is impossible to override how basic types are encoded.

Worse, if you subclass int, you cannot override how instances of your subclass get encoded, because _iterencode() does isinstance() checks.  So enum values which subclass int cannot be properly encoded.

I think the isinstance checks should be changed to explicit type equality tests, e.g. `type(o) is int`.

----------
components: Library (Lib)
messages: 141406
nosy: barry
priority: normal
severity: normal
status: open
title: Cannot override JSON encoding of basic type subclasses
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12657>
_______________________________________


More information about the Python-bugs-list mailing list