]> granicus.if.org Git - python/commitdiff
bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
authorDong-hee Na <donghee.na92@gmail.com>
Sat, 23 Jun 2018 14:46:32 +0000 (23:46 +0900)
committerEric V. Smith <ericvsmith@users.noreply.github.com>
Sat, 23 Jun 2018 14:46:32 +0000 (10:46 -0400)
Lib/dataclasses.py
Lib/test/test_dataclasses.py
Misc/NEWS.d/next/Library/2018-06-10-15-14-17.bpo-33805.5LAz5a.rst [new file with mode: 0644]

index 96bf6e1df47a0df430af59da7a86d9832d7fdfa9..ad7bf0f7593a13d2c6f35280f826e6040881f9bf 100644 (file)
@@ -1173,6 +1173,9 @@ def replace(obj, **changes):
             continue
 
         if f.name not in changes:
+            if f._field_type is _FIELD_INITVAR:
+                raise ValueError(f"InitVar {f.name!r} "
+                                 'must be specified with replace()')
             changes[f.name] = getattr(obj, f.name)
 
     # Create the new object, which calls __init__() and
index 929793119d72b60008f2f5a298f7f95ae4cdde0a..d9556c7ff9cecca5f87a08b5b7e318692f3877e7 100755 (executable)
@@ -3024,6 +3024,22 @@ class TestReplace(unittest.TestCase):
 
         replace(c, x=5)
 
+    def test_initvar_is_specified(self):
+        @dataclass
+        class C:
+            x: int
+            y: InitVar[int]
+
+            def __post_init__(self, y):
+                self.x *= y
+
+        c = C(1, 10)
+        self.assertEqual(c.x, 10)
+        with self.assertRaisesRegex(ValueError, r"InitVar 'y' must be "
+                                    "specified with replace()"):
+            replace(c, x=3)
+        c = replace(c, x=3, y=5)
+        self.assertEqual(c.x, 15)
     ## def test_initvar(self):
     ##     @dataclass
     ##     class C:
diff --git a/Misc/NEWS.d/next/Library/2018-06-10-15-14-17.bpo-33805.5LAz5a.rst b/Misc/NEWS.d/next/Library/2018-06-10-15-14-17.bpo-33805.5LAz5a.rst
new file mode 100644 (file)
index 0000000..74bcf6d
--- /dev/null
@@ -0,0 +1 @@
+Improve error message of dataclasses.replace() when an InitVar is not specified