mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Issue #22138: Fix mock.patch behavior when patching descriptors. Restore
original values after patching. Patch contributed by Sean McCully.
This commit is contained in:
parent
cbe6356c42
commit
81bc927da7
3 changed files with 33 additions and 1 deletions
|
@ -1817,5 +1817,31 @@ class PatchTest(unittest.TestCase):
|
|||
self.assertEqual(stopped, ["three", "two", "one"])
|
||||
|
||||
|
||||
def test_special_attrs(self):
|
||||
def foo(x=0):
|
||||
"""TEST"""
|
||||
return x
|
||||
with patch.object(foo, '__defaults__', (1, )):
|
||||
self.assertEqual(foo(), 1)
|
||||
self.assertEqual(foo(), 0)
|
||||
|
||||
with patch.object(foo, '__doc__', "FUN"):
|
||||
self.assertEqual(foo.__doc__, "FUN")
|
||||
self.assertEqual(foo.__doc__, "TEST")
|
||||
|
||||
with patch.object(foo, '__module__', "testpatch2"):
|
||||
self.assertEqual(foo.__module__, "testpatch2")
|
||||
self.assertEqual(foo.__module__, 'unittest.test.testmock.testpatch')
|
||||
|
||||
with patch.object(foo, '__annotations__', dict([('s', 1, )])):
|
||||
self.assertEqual(foo.__annotations__, dict([('s', 1, )]))
|
||||
self.assertEqual(foo.__annotations__, dict())
|
||||
|
||||
def foo(*a, x=0):
|
||||
return x
|
||||
with patch.object(foo, '__kwdefaults__', dict([('x', 1, )])):
|
||||
self.assertEqual(foo(), 1)
|
||||
self.assertEqual(foo(), 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue