bpo-35512: Resolve string target to patch.dict decorator during funct… · python/cpython@a875ea5 (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1620,8 +1620,6 @@ class _patch_dict(object):
1620 1620 """
1621 1621
1622 1622 def __init__(self, in_dict, values=(), clear=False, **kwargs):
1623 -if isinstance(in_dict, str):
1624 -in_dict = _importer(in_dict)
1625 1623 self.in_dict = in_dict
1626 1624 # support any argument supported by dict(...) constructor
1627 1625 self.values = dict(values)
@@ -1662,6 +1660,8 @@ def __enter__(self):
1662 1660
1663 1661 def _patch_dict(self):
1664 1662 values = self.values
1663 +if isinstance(self.in_dict, str):
1664 +self.in_dict = _importer(self.in_dict)
1665 1665 in_dict = self.in_dict
1666 1666 clear = self.clear
1667 1667
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1 +target = {'foo': 'FOO'}
2 +
3 +
1 4 def is_instance(obj, klass):
2 5 """Version of is_instance that doesn't access __class__"""
3 6 return issubclass(type(obj), klass)
Original file line number Diff line number Diff line change
@@ -664,6 +664,23 @@ def test():
664 664 test()
665 665
666 666
667 +def test_patch_dict_decorator_resolution(self):
668 +# bpo-35512: Ensure that patch with a string target resolves to
669 +# the new dictionary during function call
670 +original = support.target.copy()
671 +
672 +@patch.dict('unittest.test.testmock.support.target', {'bar': 'BAR'})
673 +def test():
674 +self.assertEqual(support.target, {'foo': 'BAZ', 'bar': 'BAR'})
675 +
676 +try:
677 +support.target = {'foo': 'BAZ'}
678 +test()
679 +self.assertEqual(support.target, {'foo': 'BAZ'})
680 +finally:
681 +support.target = original
682 +
683 +
667 684 def test_patch_descriptor(self):
668 685 # would be some effort to fix this - we could special case the
669 686 # builtin descriptors: classmethod, property, staticmethod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 +:func:`unittest.mock.patch.dict` used as a decorator with string target
2 +resolves the target during function call instead of during decorator
3 +construction. Patch by Karthikeyan Singaravelan.