summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-10-04 06:20:14 (GMT)
committerGitHub <noreply@github.com>2023-10-04 06:20:14 (GMT)
commit5b9a3fd6a0ce3c347463e6192a59c15f5fcb0043 (patch)
treed5eababb628c9a8d66bc5e8386a3add2a843c5c3 /Lib
parentbfe7e72522565f828f43c2591fea84a7981ee048 (diff)
downloadcpython-5b9a3fd6a0ce3c347463e6192a59c15f5fcb0043.zip
cpython-5b9a3fd6a0ce3c347463e6192a59c15f5fcb0043.tar.gz
cpython-5b9a3fd6a0ce3c347463e6192a59c15f5fcb0043.tar.bz2
gh-110273: dataclasses.replace() now raise TypeError for all invalid arguments (GH-110274)
dataclasses.replace() now raises TypeError instead of ValueError if specify keyword argument for a field declared with init=False or miss keyword argument for required InitVar field.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/dataclasses.py10
-rw-r--r--Lib/test/test_dataclasses/__init__.py12
2 files changed, 11 insertions, 11 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 84f8d68..31dc6f8 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1567,15 +1567,15 @@ def _replace(obj, /, **changes):
if not f.init:
# Error if this field is specified in changes.
if f.name in changes:
- raise ValueError(f'field {f.name} is declared with '
- 'init=False, it cannot be specified with '
- 'replace()')
+ raise TypeError(f'field {f.name} is declared with '
+ f'init=False, it cannot be specified with '
+ f'replace()')
continue
if f.name not in changes:
if f._field_type is _FIELD_INITVAR and f.default is MISSING:
- raise ValueError(f"InitVar {f.name!r} "
- 'must be specified with replace()')
+ raise TypeError(f"InitVar {f.name!r} "
+ f'must be specified with replace()')
changes[f.name] = getattr(obj, f.name)
# Create the new object, which calls __init__() and
diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py
index 7c07dfc..f629d7b 100644
--- a/Lib/test/test_dataclasses/__init__.py
+++ b/Lib/test/test_dataclasses/__init__.py
@@ -3965,9 +3965,9 @@ class TestReplace(unittest.TestCase):
self.assertEqual((c1.x, c1.y, c1.z, c1.t), (3, 2, 10, 100))
- with self.assertRaisesRegex(ValueError, 'init=False'):
+ with self.assertRaisesRegex(TypeError, 'init=False'):
replace(c, x=3, z=20, t=50)
- with self.assertRaisesRegex(ValueError, 'init=False'):
+ with self.assertRaisesRegex(TypeError, 'init=False'):
replace(c, z=20)
replace(c, x=3, z=20, t=50)
@@ -4020,10 +4020,10 @@ class TestReplace(unittest.TestCase):
self.assertEqual((c1.x, c1.y), (5, 10))
# Trying to replace y is an error.
- with self.assertRaisesRegex(ValueError, 'init=False'):
+ with self.assertRaisesRegex(TypeError, 'init=False'):
replace(c, x=2, y=30)
- with self.assertRaisesRegex(ValueError, 'init=False'):
+ with self.assertRaisesRegex(TypeError, 'init=False'):
replace(c, y=30)
def test_classvar(self):
@@ -4056,8 +4056,8 @@ class TestReplace(unittest.TestCase):
c = C(1, 10)
self.assertEqual(c.x, 10)
- with self.assertRaisesRegex(ValueError, r"InitVar 'y' must be "
- "specified with replace()"):
+ with self.assertRaisesRegex(TypeError, r"InitVar 'y' must be "
+ r"specified with replace\(\)"):
replace(c, x=3)
c = replace(c, x=3, y=5)
self.assertEqual(c.x, 15)