diff options
author | Mario Corchero <mcorcherojim@bloomberg.net> | 2019-05-28 12:53:31 (GMT) |
---|---|---|
committer | Cheryl Sabella <cheryl.sabella@gmail.com> | 2019-05-28 12:53:30 (GMT) |
commit | 04530812e90e45a37ed84e83505d63db7edc1262 (patch) | |
tree | c26e4f6492e37209667b61943cb5a156c2f57aeb /Doc | |
parent | eb65e2443ac21739baf6d373abc7b4638b9d6927 (diff) | |
download | cpython-04530812e90e45a37ed84e83505d63db7edc1262.zip cpython-04530812e90e45a37ed84e83505d63db7edc1262.tar.gz cpython-04530812e90e45a37ed84e83505d63db7edc1262.tar.bz2 |
bpo-32299: Return patched dict when using patch.dict as a context manager (GH-11062)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/unittest.mock.rst | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 36ac24a..da6cdfe 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'}): |