Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 350a3d0

Browse files
committed
Change copy.replace() to raise TypeError for unkown fields for named tuples
1 parent 9e6cd98 commit 350a3d0

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

Lib/collections/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def _make(cls, iterable):
457457
def _replace(self, /, **kwds):
458458
result = self._make(_map(kwds.pop, field_names, self))
459459
if kwds:
460-
raise ValueError(f'Got unexpected field name(s): {list(kwds)!r}')
460+
raise TypeError(f'Got unexpected field name(s): {list(kwds)!r}')
461461
return result
462462

463463
_replace.__doc__ = (f'Return a new {typename} object replacing specified '

Objects/structseq.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,11 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
388388
if (n_unnamed_fields < 0) {
389389
return NULL;
390390
}
391+
391392
tup = _PyTuple_FromArray(self->ob_item, n_visible_fields);
392393
if (!tup) {
393394
goto error;
394395
}
395-
396396
dict = PyDict_New();
397397
if (!dict) {
398398
goto error;
@@ -428,7 +428,7 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
428428
if (PyDict_Size(kwargs) > 0) {
429429
PyObject *names = PyDict_Keys(kwargs);
430430
if (names) {
431-
PyErr_Format(PyExc_ValueError, "Got unexpected field name(s): %R", names);
431+
PyErr_Format(PyExc_TypeError, "Got unexpected field name(s): %R", names);
432432
Py_DECREF(names);
433433
}
434434
goto error;

0 commit comments

Comments
 (0)