Jack Wong <[email protected]> added the comment:
Can we reopen this bug? Karthikeyan's PR works for Dmitry's toy example, but it
does not work in the usual case where patch() and attach_mock() are used. I
encountered this bug on Python 3.7.3, which includes the PR.
Non-toy example:
import unittest.mock as mock
def foo():
pass
parent = mock.Mock()
with mock.patch('__main__.foo', autospec=True) as mock_foo:
parent.attach_mock(mock_foo, 'child')
parent.child()
print(parent.mock_calls)
Actual output:
[]
Expected output:
[call.child()]
The reason why Karthikeyan's PR works on the toy example is that that mock's
name is not set. In the usual case, the function mock's name will be set so
this "if" block in _check_and_set_parent will return immediately.
if ((value._mock_name or value._mock_new_name) or
(value._mock_parent is not None) or
(value._mock_new_parent is not None)):
return False
I think a possible fix is to move the inner mock extraction out to the
attach_mock function as that function contains code to clear the mock's parent
and name attributes. Downside is that that would make it fail on Dmitry's toy
example.
----------
nosy: +iforapsy
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue21478>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com