[issue34363] dataclasses.asdict() mishandles dataclass instance attributes that are instances of subclassed typing.NamedTuple

Neil Girdhar report at bugs.python.org
Fri Aug 10 00:27:00 EDT 2018


Neil Girdhar <mistersheik at gmail.com> added the comment:

Sorry if I'm intruding here, but I really dislike that we are doing isinstance versus list, tuple, and dict.  And I dislike even more type(x) in (list, tuple).  I think the ideal thing to do would have been to check versus abstract base classes like isinstance(x, Sequence) and isinstance(x, Mapping).  The advantage to this is flexibility with user-defined types.

The problem seems to be that the abstract base classes don't promise anything about the constructor.  It seems like some Sequence types accept an Iterable (e.g. tuple), but others like NamedTuple want positional arguments.  I think this promise should be encoded in some way.

Maybe a third solution is to have NamedTuple special-cased out:

isinstance(x, Sequence) and not isinstance(x, NamedTuple)

If there are other Sequence types that do this (are there?) then an abstract base class could be created.

This solution has the benefit of playing the most nicely with user-defined classes.

----------
nosy: +NeilGirdhar

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34363>
_______________________________________


More information about the Python-bugs-list mailing list