Author iforapsy
Recipients Claudiu.Popa, and, cjw296, iforapsy, kushal.das, mariocj89, michael.foord, xtreak
Date 2019-07-10.01:50:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562723413.26.0.780344129112.issue21478@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-07-10 01:50:13iforapsysetrecipients: + iforapsy, cjw296, michael.foord, Claudiu.Popa, kushal.das, and, mariocj89, xtreak
2019-07-10 01:50:13iforapsysetmessageid: <1562723413.26.0.780344129112.issue21478@roundup.psfhosted.org>
2019-07-10 01:50:13iforapsylinkissue21478 messages
2019-07-10 01:50:12iforapsycreate