From 75a662640d20a40e11d9d7bdcc5887888e2ec54d Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sat, 15 May 2021 18:05:39 -0400 Subject: [PATCH 1/8] Update dataclasses.rst Expanded ``astuple()`` docs. --- Doc/library/dataclasses.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index e203a1a2132df9..0e8f56e14ee31f 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -355,6 +355,12 @@ Module contents assert astuple(p) == (10, 20) assert astuple(c) == ([(0, 0), (10, 4)],) + + The tuple is a :func:`copy.deepcopy` of contained objects, which may + be an issue in some corner cases. To create a shallow copy, the + following workaround may be used:: + + tuple(getattr(instance, field.name) for field in dataclasses.fields(instance)) Raises :exc:`TypeError` if ``instance`` is not a dataclass instance. From 1b80a7a52789fd41e7f8a5810112447042a0637a Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Mon, 24 May 2021 00:52:22 -0400 Subject: [PATCH 2/8] Moved 'deepcopy' sentence to the first paragraph. --- Doc/library/dataclasses.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 0e8f56e14ee31f..7b6c571785f0c0 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -349,16 +349,15 @@ Module contents 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)],) - The tuple is a :func:`copy.deepcopy` of contained objects, which may - be an issue in some corner cases. To create a shallow copy, the - following workaround may be used:: + To create a shallow copy, the following workaround may be used:: tuple(getattr(instance, field.name) for field in dataclasses.fields(instance)) From 390274cb76b0f1370639bc7369d9fd576dd75b3b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 24 May 2021 05:00:13 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst diff --git a/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst b/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst new file mode 100644 index 00000000000000..b474c362ef6b73 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst @@ -0,0 +1,2 @@ +Expanded ``dataclasses.astuple()`` docs, warning about deepcopy being applied +and providing a workaround. \ No newline at end of file From be3feaf9e5e78a9228957397447f68cf34e1afb5 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sun, 28 Nov 2021 20:33:56 -0500 Subject: [PATCH 4/8] fix trailing whitespace --- Doc/library/dataclasses.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 7b6c571785f0c0..24913569b09ef9 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -356,9 +356,9 @@ Module contents assert astuple(p) == (10, 20) assert astuple(c) == ([(0, 0), (10, 4)],) - + To create a shallow copy, the following workaround may be used:: - + tuple(getattr(instance, field.name) for field in dataclasses.fields(instance)) Raises :exc:`TypeError` if ``instance`` is not a dataclass instance. From f0e5b875885d278871392196d3133dfd6234c56d Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sun, 28 Nov 2021 20:37:23 -0500 Subject: [PATCH 5/8] fix one more trailing space --- Doc/library/dataclasses.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 24913569b09ef9..05289f716e24b8 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -349,7 +349,7 @@ Module contents 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. Other objects are copied with + tuples are recursed into. Other objects are copied with :func:`copy.deepcopy`. Continuing from the previous example:: From f9f273490e43d965150825e0eda80f13a638c10a Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sun, 28 Nov 2021 23:06:10 -0500 Subject: [PATCH 6/8] added deepcopy note and workaround to `asdict` --- Doc/library/dataclasses.rst | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 05289f716e24b8..34b558568ff02d 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -325,7 +325,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: @@ -342,7 +345,12 @@ 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) @@ -361,7 +369,8 @@ Module contents tuple(getattr(instance, field.name) for field in dataclasses.fields(instance)) - Raises :exc:`TypeError` if ``instance`` is not a dataclass 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) From fbcc54a907572b782c830f7c7ade46f7fe100377 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sun, 28 Nov 2021 23:07:59 -0500 Subject: [PATCH 7/8] remove tildes in links --- Doc/library/dataclasses.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 34b558568ff02d..547037b39a0643 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -328,7 +328,7 @@ Module contents lists, and tuples are recursed into. Other objects are copied with :func:`copy.deepcopy`. - Example of using :func:`~asdict` on nested dataclasses:: + Example of using :func:`asdict` on nested dataclasses:: @dataclass class Point: @@ -349,7 +349,7 @@ Module contents dict((field.name, getattr(instance, field.name)) for field in fields(instance)) - :func:`~asdict` raises :exc:`TypeError` if ``instance`` is not a dataclass + :func:`asdict` raises :exc:`TypeError` if ``instance`` is not a dataclass instance. .. function:: astuple(instance, *, tuple_factory=tuple) @@ -369,7 +369,7 @@ Module contents tuple(getattr(instance, field.name) for field in dataclasses.fields(instance)) - :func:`~astuple` raises :exc:`TypeError` if ``instance`` is not a dataclass + :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) From 4a8a2a73e41df9bd2790f5b2a37f37adb13a5491 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Mon, 29 Nov 2021 09:08:30 -0500 Subject: [PATCH 8/8] update news --- .../Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst b/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst index b474c362ef6b73..760e1eea0deb70 100644 --- a/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst +++ b/Misc/NEWS.d/next/Documentation/2021-05-24-05-00-12.bpo-43905.tBIndE.rst @@ -1,2 +1,2 @@ -Expanded ``dataclasses.astuple()`` docs, warning about deepcopy being applied -and providing a workaround. \ No newline at end of file +Expanded :func:`~dataclasses.astuple` and :func:`~dataclasses.asdict` docs, +warning about deepcopy being applied and providing a workaround.