[Python-checkins] bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867)

miss-islington webhook-mailer at python.org
Mon Apr 5 15:41:10 EDT 2021


https://github.com/python/cpython/commit/75220674c07abfc90c2cd7862d04cfa2e2354450
commit: 75220674c07abfc90c2cd7862d04cfa2e2354450
branch: master
author: Zackery Spytz <zspytz at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-04-05T12:41:01-07:00
summary:

bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867)



Co-Authored-By: Claudiu Popa <pcmanticore at gmail.com>

Automerge-Triggered-By: GH:ericvsmith

files:
A Misc/NEWS.d/next/Library/2020-06-13-23-33-32.bpo-36470.oi6Kdb.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 422a95cebe853..3de2ec04ad370 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1300,7 +1300,7 @@ class C:
             continue
 
         if f.name not in changes:
-            if f._field_type is _FIELD_INITVAR:
+            if f._field_type is _FIELD_INITVAR and f.default is MISSING:
                 raise ValueError(f"InitVar {f.name!r} "
                                  'must be specified with replace()')
             changes[f.name] = getattr(obj, f.name)
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 0bfed41b369d1..4f5c3c8aab167 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -3251,6 +3251,24 @@ def __post_init__(self, y):
         c = replace(c, x=3, y=5)
         self.assertEqual(c.x, 15)
 
+    def test_initvar_with_default_value(self):
+        @dataclass
+        class C:
+            x: int
+            y: InitVar[int] = None
+            z: InitVar[int] = 42
+
+            def __post_init__(self, y, z):
+                if y is not None:
+                    self.x += y
+                if z is not None:
+                    self.x += z
+
+        c = C(x=1, y=10, z=1)
+        self.assertEqual(replace(c), C(x=12))
+        self.assertEqual(replace(c, y=4), C(x=12, y=4, z=42))
+        self.assertEqual(replace(c, y=4, z=1), C(x=12, y=4, z=1))
+
     def test_recursive_repr(self):
         @dataclass
         class C:
diff --git a/Misc/NEWS.d/next/Library/2020-06-13-23-33-32.bpo-36470.oi6Kdb.rst b/Misc/NEWS.d/next/Library/2020-06-13-23-33-32.bpo-36470.oi6Kdb.rst
new file mode 100644
index 0000000000000..9b6ab994f3b6a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-13-23-33-32.bpo-36470.oi6Kdb.rst
@@ -0,0 +1,2 @@
+Fix dataclasses with ``InitVar``\s and :func:`~dataclasses.replace()`. Patch
+by Claudiu Popa.



More information about the Python-checkins mailing list