mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-35634: Raise an error when first passed kwargs contains duplicated keys. (GH-11438)
This commit is contained in:
parent
58159ef856
commit
f1ec3cefad
3 changed files with 94 additions and 48 deletions
|
@ -316,6 +316,52 @@ not function
|
|||
...
|
||||
TypeError: dir() got multiple values for keyword argument 'b'
|
||||
|
||||
Test a kwargs mapping with duplicated keys.
|
||||
|
||||
>>> from collections.abc import Mapping
|
||||
>>> class MultiDict(Mapping):
|
||||
... def __init__(self, items):
|
||||
... self._items = items
|
||||
...
|
||||
... def __iter__(self):
|
||||
... return (k for k, v in self._items)
|
||||
...
|
||||
... def __getitem__(self, key):
|
||||
... for k, v in self._items:
|
||||
... if k == key:
|
||||
... return v
|
||||
... raise KeyError(key)
|
||||
...
|
||||
... def __len__(self):
|
||||
... return len(self._items)
|
||||
...
|
||||
... def keys(self):
|
||||
... return [k for k, v in self._items]
|
||||
...
|
||||
... def values(self):
|
||||
... return [v for k, v in self._items]
|
||||
...
|
||||
... def items(self):
|
||||
... return [(k, v) for k, v in self._items]
|
||||
...
|
||||
>>> g(**MultiDict([('x', 1), ('y', 2)]))
|
||||
1 () {'y': 2}
|
||||
|
||||
>>> g(**MultiDict([('x', 1), ('x', 2)]))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: g() got multiple values for keyword argument 'x'
|
||||
|
||||
>>> g(a=3, **MultiDict([('x', 1), ('x', 2)]))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: g() got multiple values for keyword argument 'x'
|
||||
|
||||
>>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)]))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: g() got multiple values for keyword argument 'x'
|
||||
|
||||
Another helper function
|
||||
|
||||
>>> def f2(*a, **b):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue