Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,8 +1595,6 @@ class _patch_dict(object):
"""

def __init__(self, in_dict, values=(), clear=False, **kwargs):
if isinstance(in_dict, str):
in_dict = _importer(in_dict)
self.in_dict = in_dict
# support any argument supported by dict(...) constructor
self.values = dict(values)
Expand Down Expand Up @@ -1637,6 +1635,8 @@ def __enter__(self):

def _patch_dict(self):
values = self.values
if isinstance(self.in_dict, str):
self.in_dict = _importer(self.in_dict)
in_dict = self.in_dict
clear = self.clear

Expand Down
3 changes: 3 additions & 0 deletions Lib/unittest/test/testmock/support.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
target = {'foo': 'FOO'}


def is_instance(obj, klass):
"""Version of is_instance that doesn't access __class__"""
return issubclass(type(obj), klass)
Expand Down
17 changes: 17 additions & 0 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,23 @@ def test():
test()


def test_patch_dict_decorator_resolution(self):
# bpo-35512: Ensure that patch with a string target resolves to
# the new dictionary during function call
original = support.target.copy()

@patch.dict('unittest.test.testmock.support.target', {'bar': 'BAR'})
def test():
self.assertEqual(support.target, {'foo': 'BAZ', 'bar': 'BAR'})

try:
support.target = {'foo': 'BAZ'}
test()
self.assertEqual(support.target, {'foo': 'BAZ'})
finally:
support.target = original


def test_patch_descriptor(self):
# would be some effort to fix this - we could special case the
# builtin descriptors: classmethod, property, staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`unittest.mock.patch.dict` used as a decorator with string target
resolves the target during function call instead of during decorator
construction. Patch by Karthikeyan Singaravelan.