Author mandolaerik
Recipients mandolaerik
Date 2021-04-21.12:19:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1619007557.11.0.871267775065.issue43905@roundup.psfhosted.org>
In-reply-to
Content
It seems that the 'dataclass.astuple' function does a deepcopy of all fields. This is not documented. Two problems:

1. Dictionary keys that rely on object identity are ruined:
    import dataclasses
    @dataclasses.dataclass
    class Foo:
        key: object
    key = object()
    lut = {key: 5}
    (y,) = dataclasses.astuple(Foo(x))
    # KeyError
    lut[y]

2. dataclasses can only be converted to a tuple if all fields are serializable:

    import dataclasses
    @dataclasses.dataclass
    class Foo:
        f: object
    foo = Foo(open('test.py'))
    dataclasses.astuple(foo)

->

TypeError: cannot pickle '_io.TextIOWrapper' object


In my use case, I just want a list of all fields. I can do the following as a workaround:
  (getattr(foo, field.name) for field in dataclasses.fields(foo))

Tested on Python 3.8.7 and 3.7.9.
History
Date User Action Args
2021-04-21 12:19:17mandolaeriksetrecipients: + mandolaerik
2021-04-21 12:19:17mandolaeriksetmessageid: <1619007557.11.0.871267775065.issue43905@roundup.psfhosted.org>
2021-04-21 12:19:17mandolaeriklinkissue43905 messages
2021-04-21 12:19:16mandolaerikcreate