8000 bpo-43245: Add keyword argument support to ChainMap.new_child() (GH-2… · python/cpython@9923df9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9923df9

Browse files
authored
bpo-43245: Add keyword argument support to ChainMap.new_child() (GH-24788)
1 parent 9c376bc commit 9923df9

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Doc/library/collections.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,23 @@ The class can be used to simulate nested scopes and is useful in templating.
7272
be modified to change which mappings are searched. The list should
7373
always contain at least one mapping.
7474

75-
.. method:: new_child(m=None)
75+
.. method:: new_child(m=None, **kwargs)
7676

7777
Returns a new :class:`ChainMap` containing a new map followed by
7878
all of the maps in the current instance. If ``m`` is specified,
7979
it becomes the new map at the front of the list of mappings; if not
8080
specified, an empty dict is used, so that a call to ``d.new_child()``
81-
is equivalent to: ``ChainMap({}, *d.maps)``. This method is used for
82-
creating subcontexts that can be updated without altering values in any
83-
of the parent mappings.
81+
is equivalent to: ``ChainMap({}, *d.maps)``. If any keyword arguments
82+
are specified, they update passed map or new empty dict. This method
83+
is used for creating subcontexts that can be updated without altering
84+
values in any of the parent mappings.
8485

8586
.. versionchanged:: 3.4
8687
The optional ``m`` parameter was added.
8788

89+
.. versionchanged:: 3.10
90+
Keyword arguments support was added.
91+
8892
.. attribute:: parents
8993

9094
Property returning a new :class:`ChainMap` containing all of the maps in

Lib/collections/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,12 +1010,15 @@ def copy(self):
10101010

10111011
__copy__ = copy
10121012

1013-
def new_child(self, m=None): # like Django's Context.push()
1013+
def new_child(self, m=None, **kwargs): # like Django's Context.push()
10141014
'''New ChainMap with a new map followed by all previous maps.
10151015
If no map is provided, an empty dict is used.
1016+
Keyword arguments update the map or new empty dict.
10161017
'''
10171018
if m is None:
1018-
m = {}
1019+
m = kwargs
1020+
elif kwargs:
1021+
m.update(kwargs)
10191022
return self.__class__(m, *self.maps)
10201023

10211024
@property

Lib/test/test_collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def __contains__(self, key):
249249
for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get
250250
self.assertEqual(d.get(k, 100), v)
251251

252+
c = ChainMap({'a': 1, 'b': 2})
253+
d = c.new_child(b=20, c=30)
254+
self.assertEqual(d.maps, [{'b': 20, 'c': 30}, {'a': 1, 'b': 2}])
255+
252256
def test_union_operators(self):
253257
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
254258
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add keyword arguments support to ``ChainMap.new_child()``.

0 commit comments

Comments
 (0)
0