Skip to content

Commit

Permalink
bpo-43905: Expand dataclasses.astuple() and asdict() docs (pythonGH-2…
Browse files Browse the repository at this point in the history
…6154)

Expanded ``astuple()`` docs, warning about deepcopy being applied
and providing a workaround.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit c1f93f0)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
  • Loading branch information
miss-islington and akulakov committed Nov 29, 2021
1 parent af39cfa commit 32f1491
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ Module contents
Converts the dataclass ``instance`` to a dict (by using the
factory function ``dict_factory``). Each dataclass is converted
to a dict of its fields, as ``name: value`` pairs. dataclasses, dicts,
lists, and tuples are recursed into. For example::
lists, and tuples are recursed into. Other objects are copied with
:func:`copy.deepcopy`.

Example of using :func:`asdict` on nested dataclasses::

@dataclass
class Point:
Expand All @@ -341,21 +344,32 @@ Module contents
c = C([Point(0, 0), Point(10, 4)])
assert asdict(c) == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}

Raises :exc:`TypeError` if ``instance`` is not a dataclass instance.
To create a shallow copy, the following workaround may be used::

dict((field.name, getattr(instance, field.name)) for field in fields(instance))

:func:`asdict` raises :exc:`TypeError` if ``instance`` is not a dataclass
instance.

.. function:: astuple(instance, *, tuple_factory=tuple)

Converts the dataclass ``instance`` to a tuple (by using the
factory function ``tuple_factory``). Each dataclass is converted
to a tuple of its field values. dataclasses, dicts, lists, and
tuples are recursed into.
tuples are recursed into. Other objects are copied with
:func:`copy.deepcopy`.

Continuing from the previous example::

assert astuple(p) == (10, 20)
assert astuple(c) == ([(0, 0), (10, 4)],)

Raises :exc:`TypeError` if ``instance`` is not a dataclass instance.
To create a shallow copy, the following workaround may be used::

tuple(getattr(instance, field.name) for field in dataclasses.fields(instance))

:func:`astuple` raises :exc:`TypeError` if ``instance`` is not a dataclass
instance.

.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Expanded :func:`~dataclasses.astuple` and :func:`~dataclasses.asdict` docs,
warning about deepcopy being applied and providing a workaround.

0 comments on commit 32f1491

Please sign in to comment.