bpo-35634: Raise an error when first passed kwargs contains duplicated keys. (GH-11438)

This commit is contained in:
Serhiy Storchaka 2019-01-12 10:12:24 +02:00 committed by GitHub
parent 58159ef856
commit f1ec3cefad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 48 deletions

View file

@ -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):