Message391516
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. |
|
Date |
User |
Action |
Args |
2021-04-21 12:19:17 | mandolaerik | set | recipients:
+ mandolaerik |
2021-04-21 12:19:17 | mandolaerik | set | messageid: <1619007557.11.0.871267775065.issue43905@roundup.psfhosted.org> |
2021-04-21 12:19:17 | mandolaerik | link | issue43905 messages |
2021-04-21 12:19:16 | mandolaerik | create | |
|