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.

Author p-ganssle
Recipients eric.smith, levkivskyi, p-ganssle, remi.lapeyre, wrmsr
Date 2019-09-24.14:25:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1569335111.77.0.0707655824516.issue35540@roundup.psfhosted.org>
In-reply-to
Content
Considering that `namedtuple` is special-cased, I think it's reasonable to special-case `defaultdict` as well, though it may be worth considering more general solutions that will also work for things other than the standard library. One would be to solve this the same way that other "subclasses may have a different constructor" problems are solved (e.g. `float`, `int`, formerly `datetime`) and ignore the subclass (or selectively ignore it if it's a problem), for example changing _asdict_inner to something like this:

if isinstance(obj, dict):
    new_keys = tuple((_asdict_inner(k, dict_factory),
                      _asdict_inner(v, dict_factory))
                      for k, v in obj.items())

    try:
        return type(obj)(new_keys)
    except Exception:
        return dict(new_keys)

Another more general alternative would be to add a type registry for `asdict`, either as an additional parameter or with a new transformer class of some sort. I created a quick proof of concept for this in GH-16356 to see one way it could look.

In any case I think it's quite unfortunate that we can't easily just support anything that has a __deepcopy__ defined. There may be some crazy solution that involves passing a class with a custom __getitem__ to the `memo` argument of copy.deepcopy, but if it's even possible (haven't thought about it enough) I'm not sure it's *advisable*.
History
Date User Action Args
2019-09-24 14:25:11p-gansslesetrecipients: + p-ganssle, eric.smith, levkivskyi, wrmsr, remi.lapeyre
2019-09-24 14:25:11p-gansslesetmessageid: <1569335111.77.0.0707655824516.issue35540@roundup.psfhosted.org>
2019-09-24 14:25:11p-gansslelinkissue35540 messages
2019-09-24 14:25:11p-gansslecreate