This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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 <>
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():

    parent = mock.Mock()

    with mock.patch('', autospec=True) as mock_foo:
        parent.attach_mock(mock_foo, 'child')

Actual output:

Expected output:

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.
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: <>
2019-07-10 01:50:13iforapsylinkissue21478 messages
2019-07-10 01:50:12iforapsycreate