bpo-43245: Add keyword argument support to ChainMap.new_child() (GH-24788)

This commit is contained in:
Kamil Turek 2021-03-14 04:15:44 +01:00 committed by GitHub
parent 9c376bc1c4
commit 9923df9641
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 6 deletions

View file

@ -72,19 +72,23 @@ The class can be used to simulate nested scopes and is useful in templating.
be modified to change which mappings are searched. The list should be modified to change which mappings are searched. The list should
always contain at least one mapping. always contain at least one mapping.
.. method:: new_child(m=None) .. method:: new_child(m=None, **kwargs)
Returns a new :class:`ChainMap` containing a new map followed by Returns a new :class:`ChainMap` containing a new map followed by
all of the maps in the current instance. If ``m`` is specified, all of the maps in the current instance. If ``m`` is specified,
it becomes the new map at the front of the list of mappings; if not it becomes the new map at the front of the list of mappings; if not
specified, an empty dict is used, so that a call to ``d.new_child()`` specified, an empty dict is used, so that a call to ``d.new_child()``
is equivalent to: ``ChainMap({}, *d.maps)``. This method is used for is equivalent to: ``ChainMap({}, *d.maps)``. If any keyword arguments
creating subcontexts that can be updated without altering values in any are specified, they update passed map or new empty dict. This method
of the parent mappings. is used for creating subcontexts that can be updated without altering
values in any of the parent mappings.
.. versionchanged:: 3.4 .. versionchanged:: 3.4
The optional ``m`` parameter was added. The optional ``m`` parameter was added.
.. versionchanged:: 3.10
Keyword arguments support was added.
.. attribute:: parents .. attribute:: parents
Property returning a new :class:`ChainMap` containing all of the maps in Property returning a new :class:`ChainMap` containing all of the maps in

View file

@ -1010,12 +1010,15 @@ def copy(self):
__copy__ = copy __copy__ = copy
def new_child(self, m=None): # like Django's Context.push() def new_child(self, m=None, **kwargs): # like Django's Context.push()
'''New ChainMap with a new map followed by all previous maps. '''New ChainMap with a new map followed by all previous maps.
If no map is provided, an empty dict is used. If no map is provided, an empty dict is used.
Keyword arguments update the map or new empty dict.
''' '''
if m is None: if m is None:
m = {} m = kwargs
elif kwargs:
m.update(kwargs)
return self.__class__(m, *self.maps) return self.__class__(m, *self.maps)
@property @property

View file

@ -249,6 +249,10 @@ def __contains__(self, key):
for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get
self.assertEqual(d.get(k, 100), v) self.assertEqual(d.get(k, 100), v)
c = ChainMap({'a': 1, 'b': 2})
d = c.new_child(b=20, c=30)
self.assertEqual(d.maps, [{'b': 20, 'c': 30}, {'a': 1, 'b': 2}])
def test_union_operators(self): def test_union_operators(self):
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4)) cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4)) cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))

View file

@ -0,0 +1 @@
Add keyword arguments support to ``ChainMap.new_child()``.