summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Turek <kamil.turek@hotmail.com>2021-03-14 04:15:44 +0100
committerGitHub <noreply@github.com>2021-03-13 19:15:44 -0800
commit9923df96413a0b480a34ec1d537b66ca0eeb0fdc (patch)
tree1612d5da49a8ddf65ec81139a79d548786cfc1ea
parent9c376bc1c4c8bcddb0bc4196b79ec8c75da494a8 (diff)
downloadcpython-git-9923df96413a0b480a34ec1d537b66ca0eeb0fdc.tar.gz
bpo-43245: Add keyword argument support to ChainMap.new_child() (GH-24788)
-rw-r--r--Doc/library/collections.rst12
-rw-r--r--Lib/collections/__init__.py7
-rw-r--r--Lib/test/test_collections.py4
-rw-r--r--Misc/NEWS.d/next/Library/2021-03-08-22-14-37.bpo-43245.nXL-MC.rst1
4 files changed, 18 insertions, 6 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 540db20023..723c9da7be 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -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
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
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
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
- creating subcontexts that can be updated without altering values in any
- of the parent mappings.
+ is equivalent to: ``ChainMap({}, *d.maps)``. If any keyword arguments
+ are specified, they update passed map or new empty dict. This method
+ is used for creating subcontexts that can be updated without altering
+ values in any of the parent mappings.
.. versionchanged:: 3.4
The optional ``m`` parameter was added.
+ .. versionchanged:: 3.10
+ Keyword arguments support was added.
+
.. attribute:: parents
Property returning a new :class:`ChainMap` containing all of the maps in
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 6404ea9022..89c73bbf2c 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -1010,12 +1010,15 @@ class ChainMap(_collections_abc.MutableMapping):
__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.
If no map is provided, an empty dict is used.
+ Keyword arguments update the map or new empty dict.
'''
if m is None:
- m = {}
+ m = kwargs
+ elif kwargs:
+ m.update(kwargs)
return self.__class__(m, *self.maps)
@property
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index d1c305a4a3..30303f00ba 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -249,6 +249,10 @@ class TestChainMap(unittest.TestCase):
for k, v in dict(a=1, B=20, C=30, z=100).items(): # check get
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):
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))
diff --git a/Misc/NEWS.d/next/Library/2021-03-08-22-14-37.bpo-43245.nXL-MC.rst b/Misc/NEWS.d/next/Library/2021-03-08-22-14-37.bpo-43245.nXL-MC.rst
new file mode 100644
index 0000000000..394318fb1e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-03-08-22-14-37.bpo-43245.nXL-MC.rst
@@ -0,0 +1 @@
+Add keyword arguments support to ``ChainMap.new_child()``. \ No newline at end of file