FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.9] bpo-43905: Expand dataclasses.astuple() and asdict() docs (GH-26154) by miss-islington · Pull Request #29852 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rst  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 18 additions & 4 deletions Doc/library/dataclasses.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ Module-level decorators, classes, and functions
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 @@ -309,21 +312,32 @@ Module-level decorators, classes, and functions
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)

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Back | FazBrowse Home | New Git URL