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.

Title: unittest.mock.Mock.reset_mocks does not pass all arguments to its children
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, lisroach, mariocj89, michael.foord, vegarsti, xtreak
Priority: normal Keywords: patch

Created on 2019-11-27 19:59 by vegarsti, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17409 merged vegarsti, 2019-11-27 20:55
Messages (5)
msg357581 - (view) Author: Vegard Stikbakke (vegarsti) * Date: 2019-11-27 19:59
MagicMock, from unittest.mock, has a method reset_mock, which takes optional arguments return_value and side_effect, both with default values False.

In the body of reset_mock, reset_mock is again called on all the _mock_children of of the MagicMock object. However, here the arguments are not passed. This means that if you have a MagicMock object with children that are also mocked, and methods on these have been directly mocked, then it is not enough to call reset_mock on the parent object. A code example that demonstrates this follows below. Here, we could expect m to have been completely reset. But the final print statement shows that m.a() still returns 1.

from unittest.mock import MagicMock

m = MagicMock(a=MagicMock())
m.a.return_value = 1

Pertinent line in Github
msg357582 - (view) Author: Vegard Stikbakke (vegarsti) * Date: 2019-11-27 20:06
I said MagicMock, but I meant Mock.
msg357594 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-11-28 05:22
Thanks Vegard for the report. The docs say "Child mocks and the return value mock (if any) are reset as well." so it makes sense to me to reset the return_value and side_effect of children. But as mentioned in the PR it's a behaviour change that someone might be dependent on and can go on 3.9 . If we are not going ahead with the change then we can clarify the same in the docs and ask the user to reset mock recursively on their own so that it avoids confusion.
msg357606 - (view) Author: Vegard Stikbakke (vegarsti) * Date: 2019-11-28 06:15
Oh, right! Thanks!
msg360693 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2020-01-25 15:44
New changeset aef7dc89879d099dc704bd8037b8a7686fb72838 by Chris Withers (Vegard Stikbakke) in branch 'master':
bpo-38932: Mock fully resets child objects on reset_mock(). (GH-17409)
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 83113
2020-01-25 15:46:39cjw296setstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-01-25 15:44:49cjw296setmessages: + msg360693
2019-11-28 06:15:44vegarstisetmessages: + msg357606
2019-11-28 05:22:43xtreaksetnosy: + cjw296, michael.foord, lisroach, mariocj89

messages: + msg357594
versions: + Python 3.9
2019-11-28 03:21:14xtreaksetnosy: + xtreak
2019-11-27 20:55:17vegarstisetkeywords: + patch
stage: patch review
pull_requests: + pull_request16889
2019-11-27 20:42:58vegarstisetversions: - Python 3.7
2019-11-27 20:06:00vegarstisetmessages: + msg357582
2019-11-27 20:05:27vegarstisettitle: MagicMock.reset_mocks does not pass all arguments to its children -> unittest.mock.Mock.reset_mocks does not pass all arguments to its children
2019-11-27 19:59:26vegarsticreate