Skip to content

Commit a5906b2

Browse files
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 66b00a9) Co-authored-by: Karthikeyan Singaravelan <[email protected]>
1 parent a234492 commit a5906b2

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Lib/unittest/mock.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ def _get_call_signature_from_name(self, name):
825825
if child is None or isinstance(child, _SpecState):
826826
break
827827
else:
828+
# If an autospecced object is attached using attach_mock the
829+
# child would be a function with mock object as attribute from
830+
# which signature has to be derived.
831+
child = _extract_mock(child)
828832
children = child._mock_children
829833
sig = child._spec_signature
830834

Lib/unittest/test/testmock/testmock.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,35 @@ def test_attach_mock_patch_autospec(self):
19141914
self.assertEqual(mock_func.mock._extract_mock_name(), 'mock.child')
19151915

19161916

1917+
def test_attach_mock_patch_autospec_signature(self):
1918+
with mock.patch(f'{__name__}.Something.meth', autospec=True) as mocked:
1919+
manager = Mock()
1920+
manager.attach_mock(mocked, 'attach_meth')
1921+
obj = Something()
1922+
obj.meth(1, 2, 3, d=4)
1923+
manager.assert_has_calls([call.attach_meth(mock.ANY, 1, 2, 3, d=4)])
1924+
obj.meth.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
1925+
mocked.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
1926+
1927+
with mock.patch(f'{__name__}.something', autospec=True) as mocked:
1928+
manager = Mock()
1929+
manager.attach_mock(mocked, 'attach_func')
1930+
something(1)
1931+
manager.assert_has_calls([call.attach_func(1)])
1932+
something.assert_has_calls([call(1)])
1933+
mocked.assert_has_calls([call(1)])
1934+
1935+
with mock.patch(f'{__name__}.Something', autospec=True) as mocked:
1936+
manager = Mock()
1937+
manager.attach_mock(mocked, 'attach_obj')
1938+
obj = Something()
1939+
obj.meth(1, 2, 3, d=4)
1940+
manager.assert_has_calls([call.attach_obj(),
1941+
call.attach_obj().meth(1, 2, 3, d=4)])
1942+
obj.meth.assert_has_calls([call(1, 2, 3, d=4)])
1943+
mocked.assert_has_calls([call(), call().meth(1, 2, 3, d=4)])
1944+
1945+
19171946
def test_attribute_deletion(self):
19181947
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
19191948
NonCallableMock()):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use signature from inner mock for autospecced methods attached with
2+
:func:`unittest.mock.attach_mock`. Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)