bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
(cherry picked from commit 3d70f7aef614c396f516b5fccedeebe98598714d)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 9297931..d9556c7 100755
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -3024,6 +3024,22 @@
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: