Skip to content

gh-110275: Named tuple's __replace__() now raises TypeError for invalid arguments #110299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def _make(cls, iterable):
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 '
Expand Down
6 changes: 1 addition & 5 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,8 @@ def test_instance(self):
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')
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ class PointFromClass(NamedTuple):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Named tuple's methods ``_replace()`` and ``__replace__()`` now raise
TypeError instead of ValueError for invalid keyword arguments.