Skip to content

unittest.mock.Mock.reset_mocks does not pass all arguments to its children #83113

Description

@vegarsti
mannequin
BPO 38932
Nosy @cjw296, @voidspace, @lisroach, @mariocj89, @tirkarthi, @vegarsti
PRs
  • bpo-38932: Make Mock.reset_mock() pass return_value and side_effect values to reset_mock on child mock objects #17409
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-01-25.15:46:39.649>
    created_at = <Date 2019-11-27.19:59:26.401>
    labels = ['type-bug', 'library', '3.9']
    title = 'unittest.mock.Mock.reset_mocks does not pass all arguments to its children'
    updated_at = <Date 2020-01-25.15:46:39.648>
    user = 'https://github.com/vegarsti'

    bugs.python.org fields:

    activity = <Date 2020-01-25.15:46:39.648>
    actor = 'cjw296'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-01-25.15:46:39.649>
    closer = 'cjw296'
    components = ['Library (Lib)']
    creation = <Date 2019-11-27.19:59:26.401>
    creator = 'vegarsti'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38932
    keywords = ['patch']
    message_count = 5.0
    messages = ['357581', '357582', '357594', '357606', '360693']
    nosy_count = 6.0
    nosy_names = ['cjw296', 'michael.foord', 'lisroach', 'mariocj89', 'xtreak', 'vegarsti']
    pr_nums = ['17409']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38932'
    versions = ['Python 3.9']

    Linked PRs

    Activity

    1. vegarsti commented on Nov 27, 2019

      vegarstimannequin
      MannequinAuthor

      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
      m.reset_mock(return_value=True)
      print(m.a())
      

      Pertinent line in Github

      child.reset_mock(visited)

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Nov 27, 2019
    3. changed the title [-]MagicMock.reset_mocks does not pass all arguments to its children[/-] [+]unittest.mock.Mock.reset_mocks does not pass all arguments to its children[/+] on Nov 27, 2019
    4. vegarsti commented on Nov 27, 2019

      vegarstimannequin
      MannequinAuthor

      I said MagicMock, but I meant Mock.

    5. tirkarthi commented on Nov 28, 2019

      @tirkarthi
      Member

      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.

    6. vegarsti commented on Nov 28, 2019

      vegarstimannequin
      MannequinAuthor

      Oh, right! Thanks!

    7. cjw296 commented on Jan 25, 2020

      @cjw296
      Contributor

      New changeset aef7dc8 by Chris Withers (Vegard Stikbakke) in branch 'master':
      bpo-38932: Mock fully resets child objects on reset_mock(). (GH-17409)
      aef7dc8

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions