[Python-checkins] bpo-32299: Return patched dict when using patch.dict as a context manager (GH-11062)

Cheryl Sabella webhook-mailer at python.org
Tue May 28 08:53:36 EDT 2019


https://github.com/python/cpython/commit/04530812e90e45a37ed84e83505d63db7edc1262
commit: 04530812e90e45a37ed84e83505d63db7edc1262
branch: master
author: Mario Corchero <mcorcherojim at bloomberg.net>
committer: Cheryl Sabella <cheryl.sabella at gmail.com>
date: 2019-05-28T08:53:30-04:00
summary:

bpo-32299: Return patched dict when using patch.dict as a context manager (GH-11062)

files:
A Misc/NEWS.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
M Doc/library/unittest.mock.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testpatch.py

diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 36ac24afa2bc..da6cdfe648b0 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -1556,15 +1556,24 @@ patch.dict
     decorator. When used as a class decorator :func:`patch.dict` honours
     ``patch.TEST_PREFIX`` for choosing which methods to wrap.
 
+    .. versionchanged:: 3.8
+
+        :func:`patch.dict` now returns the patched dictionary when used as a context
+        manager.
+
 :func:`patch.dict` can be used to add members to a dictionary, or simply let a test
 change a dictionary, and ensure the dictionary is restored when the test
 ends.
 
     >>> foo = {}
-    >>> with patch.dict(foo, {'newkey': 'newvalue'}):
+    >>> with patch.dict(foo, {'newkey': 'newvalue'}) as patched_foo:
     ...     assert foo == {'newkey': 'newvalue'}
+    ...     assert patched_foo == {'newkey': 'newvalue'}
+    ...     # You can add, update or delete keys of foo (or patched_foo, it's the same dict)
+    ...     patched_foo['spam'] = 'eggs'
     ...
     >>> assert foo == {}
+    >>> assert patched_foo == {}
 
     >>> import os
     >>> with patch.dict('os.environ', {'newkey': 'newvalue'}):
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index fac4535747c4..055fbb350ce8 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1730,6 +1730,7 @@ def decorate_class(self, klass):
     def __enter__(self):
         """Patch the dict."""
         self._patch_dict()
+        return self.in_dict
 
 
     def _patch_dict(self):
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
index 3295c5b2420e..27914a9d7178 100644
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -619,6 +619,13 @@ def test():
         self.assertEqual(foo.values, original)
 
 
+    def test_patch_dict_as_context_manager(self):
+        foo = {'a': 'b'}
+        with patch.dict(foo, a='c') as patched:
+            self.assertEqual(patched, {'a': 'c'})
+        self.assertEqual(foo, {'a': 'b'})
+
+
     def test_name_preserved(self):
         foo = {}
 
diff --git a/Misc/NEWS.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst b/Misc/NEWS.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
new file mode 100644
index 000000000000..4e1afa9a43ea
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-12-13-17-49-56.bpo-32299.eqAPWs.rst
@@ -0,0 +1,2 @@
+Changed :func:`unittest.mock.patch.dict` to return the patched
+dictionary when used as context manager. Patch by Vadim Tsander.



More information about the Python-checkins mailing list