Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vegarsti mannequin opened this issue Nov 27, 2019 · 5 comments
Closed

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

vegarsti mannequin opened this issue Nov 27, 2019 · 5 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@vegarsti
Copy link
Mannequin

vegarsti mannequin commented Nov 27, 2019

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']

    @vegarsti
    Copy link
    Mannequin Author

    vegarsti mannequin commented Nov 27, 2019

    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)

    @vegarsti vegarsti mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 27, 2019
    @vegarsti vegarsti mannequin 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 Nov 27, 2019
    @vegarsti
    Copy link
    Mannequin Author

    vegarsti mannequin commented Nov 27, 2019

    I said MagicMock, but I meant Mock.

    @vegarsti vegarsti mannequin removed the 3.7 (EOL) end of life label Nov 27, 2019
    @tirkarthi
    Copy link
    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.

    @tirkarthi tirkarthi added the 3.9 only security fixes label Nov 28, 2019
    @vegarsti
    Copy link
    Mannequin Author

    vegarsti mannequin commented Nov 28, 2019

    Oh, right! Thanks!

    @cjw296
    Copy link
    Contributor

    cjw296 commented Jan 25, 2020

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

    @cjw296 cjw296 closed this as completed Jan 25, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants