You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dataclasses.astuple (and .asdict) do deepcopy on all fields #88071
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/ericvsmith'closed_at=<Date2021-11-29.18:34:32.678>created_at=<Date2021-04-21.12:19:17.095>labels= ['3.11', 'type-bug', '3.9', '3.10', 'docs']
title='dataclasses.astuple (and .asdict) do deepcopy on all fields'updated_at=<Date2021-11-29.18:44:26.912>user='https://bugs.python.org/mandolaerik'
Unfortunately this can't be changed, although I suppose it should be documented.
In general I think this API was a mistake, and should not have been added. There are just too many cases where it doesn't do what you want, or where it fails.
I'd like to deprecate it and remove it (along with asdict), but I fear that would be too disruptive.
Your approach seems reasonable to me for your use case.
Why deepcopy is used at all? It is a very specific feature which should not be used by default. If you want to make a deep copy of fields, you can call copy.deepcopy() explicitly.
The reason for the deep copying was to support changing a hierarchy of dataclasses into something that could be JSON serialized. But it didn't really work out. It recurses into dataclasses, namedtuples, lists, tuples, and dicts, and deep copies everything else.
As I said, it's a design flaw.
mandolaerik commented on May 20, 2021
mandolaerikmannequin
MannequinAuthor
More actions
Would it make sense to make dataclasses iterable, like so?
Activity
mandolaerik commentedon Apr 21, 2021
It seems that the 'dataclass.astuple' function does a deepcopy of all fields. This is not documented. Two problems:
Dictionary keys that rely on object identity are ruined:
dataclasses can only be converted to a tuple if all fields are serializable:
->
In my use case, I just want a list of all fields. I can do the following as a workaround:
Tested on Python 3.8.7 and 3.7.9.
ericvsmith commentedon Apr 21, 2021
Unfortunately this can't be changed, although I suppose it should be documented.
In general I think this API was a mistake, and should not have been added. There are just too many cases where it doesn't do what you want, or where it fails.
I'd like to deprecate it and remove it (along with asdict), but I fear that would be too disruptive.
Your approach seems reasonable to me for your use case.
serhiy-storchaka commentedon Apr 21, 2021
Why deepcopy is used at all? It is a very specific feature which should not be used by default. If you want to make a deep copy of fields, you can call copy.deepcopy() explicitly.
or
ericvsmith commentedon Apr 21, 2021
The reason for the deep copying was to support changing a hierarchy of dataclasses into something that could be JSON serialized. But it didn't really work out. It recurses into dataclasses, namedtuples, lists, tuples, and dicts, and deep copies everything else.
As I said, it's a design flaw.
mandolaerik commentedon May 20, 2021
Would it make sense to make dataclasses iterable, like so?
With that in place, deprecating astuple would maybe be less disruptive?
ericvsmith commentedon May 20, 2021
No, iteration is explicitly a non-goal of PEP-557. See the section on namedtuple for why: https://www.python.org/dev/peps/pep-0557/#why-not-just-use-namedtuple
akulakov commentedon Jun 21, 2021
I've added a PR here: #26154
15 remaining items
miss-islington commentedon Nov 29, 2021
New changeset c1f93f0 by andrei kulakov in branch 'main':
bpo-43905: Expand dataclasses.astuple() and asdict() docs (GH-26154)
c1f93f0
miss-islington commentedon Nov 29, 2021
New changeset 376b24e by Miss Islington (bot) in branch '3.9':
bpo-43905: Expand dataclasses.astuple() and asdict() docs (GH-26154)
376b24e
miss-islington commentedon Nov 29, 2021
New changeset 32f1491 by Miss Islington (bot) in branch '3.10':
bpo-43905: Expand dataclasses.astuple() and asdict() docs (GH-26154)
32f1491
ericvsmith commentedon Nov 29, 2021
Thanks, @andrei.avk!
akulakov commentedon Nov 29, 2021
Thank you for reviewing Eric!
gh-88071: Update docstrings of dataclass' astuple and asdict (#101806)
pythongh-88071: Update docstrings of dataclass' astuple and asdict (p…