summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarthikeyan Singaravelan <tir.karthi@gmail.com>2020-01-24 18:44:29 +0530
committerChris Withers <chris@withers.org>2020-01-29 19:12:17 +0000
commit5de813ca343c6bc4bc990b6d1a3df1a0407ddaa6 (patch)
tree78ef26a9a87be2fb3eb345e1d39a7376fab4c1f2
parent11df596649d109d3d36f3a5062bd32f42493808a (diff)
downloadmock-5de813ca343c6bc4bc990b6d1a3df1a0407ddaa6.tar.gz
bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784)
Backports: 66b00a9d3aacf6ed49412f48743e4913104a2bb3 Signed-off-by: Chris Withers <chris@withers.org>
-rw-r--r--NEWS.d/2019-10-14-21-14-55.bpo-38473.uXpVld.rst2
-rw-r--r--mock/mock.py4
-rw-r--r--mock/tests/testmock.py29
3 files changed, 35 insertions, 0 deletions
diff --git a/NEWS.d/2019-10-14-21-14-55.bpo-38473.uXpVld.rst b/NEWS.d/2019-10-14-21-14-55.bpo-38473.uXpVld.rst
new file mode 100644
index 0000000..de80e89
--- /dev/null
+++ b/NEWS.d/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.
diff --git a/mock/mock.py b/mock/mock.py
index f924205..e453ec4 100644
--- a/mock/mock.py
+++ b/mock/mock.py
@@ -819,6 +819,10 @@ class NonCallableMock(Base):
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/mock/tests/testmock.py b/mock/tests/testmock.py
index c97d7fc..7574002 100644
--- a/mock/tests/testmock.py
+++ b/mock/tests/testmock.py
@@ -1924,6 +1924,35 @@ class MockTest(unittest.TestCase):
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()):