]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-110275: Named tuple's __replace__() now raises TypeError for invalid arguments...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 4 Dec 2023 11:30:32 +0000 (13:30 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Dec 2023 11:30:32 +0000 (13:30 +0200)
Doc/library/collections.rst
Lib/collections/__init__.py
Lib/test/test_collections.py
Lib/test/test_copy.py
Misc/NEWS.d/next/Library/2023-11-08-16-11-04.gh-issue-110275.Bm6GwR.rst [new file with mode: 0644]

index 17dd6da7479e5046009bf2b71ca46c6485824329..233b2c6a771f4a1a8e08d898062fcc17cdfe70e1 100644 (file)
@@ -981,6 +981,10 @@ field names, the method and attribute names start with an underscore.
 
     Named tuples are also supported by generic function :func:`copy.replace`.
 
+    .. versionchanged:: 3.13
+       Raise :exc:`TypeError` instead of :exc:`ValueError` for invalid
+       keyword arguments.
+
 .. attribute:: somenamedtuple._fields
 
     Tuple of strings listing the field names.  Useful for introspection
index a461550ea40da74da9c112d77b8a83f5f45bcbef..2e527dfd810c43ba5a7f361747e26c70084e9c4b 100644 (file)
@@ -457,7 +457,7 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
     def _replace(self, /, **kwds):
         result = self._make(_map(kwds.pop, field_names, self))
         if kwds:
-            raise ValueError(f'Got unexpected field names: {list(kwds)!r}')
+            raise TypeError(f'Got unexpected field names: {list(kwds)!r}')
         return result
 
     _replace.__doc__ = (f'Return a new {typename} object replacing specified '
index bb8b352518ef3ecf217cd81266f3925c5568ca46..7e6f811e17cfa2d25154f54b1d6891186d5e3696 100644 (file)
@@ -488,12 +488,8 @@ class TestNamedTuple(unittest.TestCase):
         self.assertEqual(p._replace(x=1), (1, 22))      # test _replace method
         self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
 
-        try:
+        with self.assertRaises(TypeError):
             p._replace(x=1, error=2)
-        except ValueError:
-            pass
-        else:
-            self._fail('Did not detect an incorrect fieldname')
 
         # verify that field string can have commas
         Point = namedtuple('Point', 'x, y')
index 60735ba89a80eef5983c9ea78ae297034f8ab029..89102373759ca0ed97831d7e74bcc09c984a9747 100644 (file)
@@ -952,7 +952,7 @@ class TestReplace(unittest.TestCase):
                 self.assertEqual(copy.replace(p, x=1), (1, 22))
                 self.assertEqual(copy.replace(p, y=2), (11, 2))
                 self.assertEqual(copy.replace(p, x=1, y=2), (1, 2))
-                with self.assertRaisesRegex(ValueError, 'unexpected field name'):
+                with self.assertRaisesRegex(TypeError, 'unexpected field name'):
                     copy.replace(p, x=1, error=2)
 
     def test_dataclass(self):
diff --git a/Misc/NEWS.d/next/Library/2023-11-08-16-11-04.gh-issue-110275.Bm6GwR.rst b/Misc/NEWS.d/next/Library/2023-11-08-16-11-04.gh-issue-110275.Bm6GwR.rst
new file mode 100644 (file)
index 0000000..194dd5c
--- /dev/null
@@ -0,0 +1,2 @@
+Named tuple's methods ``_replace()`` and ``__replace__()`` now raise
+TypeError instead of ValueError for invalid keyword arguments.