[New-bugs-announce] [issue43905] dataclasses.astuple does deepcopy on all fields
Erik Carstensen
report at bugs.python.org
Wed Apr 21 08:19:17 EDT 2021
New submission from Erik Carstensen <mandolaerik at gmail.com>:
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.
----------
components: Library (Lib)
messages: 391516
nosy: mandolaerik
priority: normal
severity: normal
status: open
title: dataclasses.astuple does deepcopy on all fields
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43905>
_______________________________________
More information about the New-bugs-announce
mailing list