[Python-checkins] bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784) (GH-18167)
Karthikeyan Singaravelan
webhook-mailer at python.org
Sat Jan 25 09:53:12 EST 2020
https://github.com/python/cpython/commit/a5906b2bfce9560568dee1dcc3550e74e742dd34
commit: a5906b2bfce9560568dee1dcc3550e74e742dd34
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Karthikeyan Singaravelan <tir.karthi at gmail.com>
date: 2020-01-25T20:23:08+05:30
summary:
bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784) (GH-18167)
If an autospecced object is attached using attach_mock the
child would be a function with mock object as attribute from
which signature has to be derived.
(cherry picked from commit 66b00a9d3aacf6ed49412f48743e4913104a2bb3)
Co-authored-by: Karthikeyan Singaravelan <tir.karthi at gmail.com>
files:
A Misc/NEWS.d/next/Library/2019-10-14-21-14-55.bpo-38473.uXpVld.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index e92ccf168dbbd..34f2dd77ec6cd 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -825,6 +825,10 @@ def _get_call_signature_from_name(self, name):
if child is None or isinstance(child, _SpecState):
break
else:
+ # If an autospecced object is attached using attach_mock the
+ # child would be a function with mock object as attribute from
+ # which signature has to be derived.
+ child = _extract_mock(child)
children = child._mock_children
sig = child._spec_signature
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 6dc2725427ec5..1cde45e9aea55 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1914,6 +1914,35 @@ def test_attach_mock_patch_autospec(self):
self.assertEqual(mock_func.mock._extract_mock_name(), 'mock.child')
+ def test_attach_mock_patch_autospec_signature(self):
+ with mock.patch(f'{__name__}.Something.meth', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_meth')
+ obj = Something()
+ obj.meth(1, 2, 3, d=4)
+ manager.assert_has_calls([call.attach_meth(mock.ANY, 1, 2, 3, d=4)])
+ obj.meth.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
+ mocked.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
+
+ with mock.patch(f'{__name__}.something', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_func')
+ something(1)
+ manager.assert_has_calls([call.attach_func(1)])
+ something.assert_has_calls([call(1)])
+ mocked.assert_has_calls([call(1)])
+
+ with mock.patch(f'{__name__}.Something', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_obj')
+ obj = Something()
+ obj.meth(1, 2, 3, d=4)
+ manager.assert_has_calls([call.attach_obj(),
+ call.attach_obj().meth(1, 2, 3, d=4)])
+ obj.meth.assert_has_calls([call(1, 2, 3, d=4)])
+ mocked.assert_has_calls([call(), call().meth(1, 2, 3, d=4)])
+
+
def test_attribute_deletion(self):
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
NonCallableMock()):
diff --git a/Misc/NEWS.d/next/Library/2019-10-14-21-14-55.bpo-38473.uXpVld.rst b/Misc/NEWS.d/next/Library/2019-10-14-21-14-55.bpo-38473.uXpVld.rst
new file mode 100644
index 0000000000000..de80e89e00e2d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-10-14-21-14-55.bpo-38473.uXpVld.rst
@@ -0,0 +1,2 @@
+Use signature from inner mock for autospecced methods attached with
+:func:`unittest.mock.attach_mock`. Patch by Karthikeyan Singaravelan.
More information about the Python-checkins
mailing list