You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Named tuple's _replace() method should raise TypeError for unexpected keyword arguments #110275
When you call a function with incorrect key arguments, you get a TypeError. But it is not always so with the _replace() method of a named tuple class created by collections.namedtuple().
>>> from collections import namedtuple
>>> P = namedtuple('P', 'x y')
>>> p = P(1, 2)
>>> p._replace(z=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/collections/__init__.py", line 460, in _replace
raise ValueError(f'Got unexpected field names: {list(kwds)!r}')
ValueError: Got unexpected field names: ['z']
It is not even consistent with constructor which raises TypeError:
>>> P(x=1, y=2, z=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: P.__new__() got an unexpected keyword argument 'z'
I think that _replace() also should raise TypeError for unexpected keyword arguments.
I wish you would just make the new code consistent with the existing replace implementations. Aim for the least invasive and least disruptive way to add your new function.
But if you're going to persist forcing through a TypeError, then go ahead and change _replace() as well. Better to have the two be harmonious and not introduce a permanent wart.
What do you think about other changes that I took the opportunity to do in __replace__?
Rename **kwds to **changes.
Only include the first unexpected keyword argument name in the error message instead of the list. "Got unexpected field name: 'z'" instead of "Got unexpected field names: ['z']".
Activity
rhettinger commentedon Oct 3, 2023
Let's leave these APIs alone. They are old, stable, and have never caused an issue for real users.
pythongh-110275: Named tuple's __replace__() now raise TypeError for …
serhiy-storchaka commentedon Oct 3, 2023
Well, then I'll only change
__replace__().rhettinger commentedon Oct 4, 2023
I wish you would just make the new code consistent with the existing replace implementations. Aim for the least invasive and least disruptive way to add your new function.
But if you're going to persist forcing through a
TypeError, then go ahead and change_replace()as well. Better to have the two be harmonious and not introduce a permanent wart.serhiy-storchaka commentedon Oct 9, 2023
What do you think about other changes that I took the opportunity to do in
__replace__?**kwdsto**changes.Got unexpected field name: 'z'" instead of "Got unexpected field names: ['z']".They are less important, so I do not persist.
gh-110275: Named tuple's __replace__() now raises TypeError for inval…
pythongh-110275: Named tuple's __replace__() now raises TypeError for…
pythongh-110275: Named tuple's __replace__() now raises TypeError for…