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

mock.MagicMock does not mock __truediv__ #65167

Closed
JohannesBaiter mannequin opened this issue Mar 18, 2014 · 10 comments
Closed

mock.MagicMock does not mock __truediv__ #65167

JohannesBaiter mannequin opened this issue Mar 18, 2014 · 10 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JohannesBaiter
Copy link
Mannequin

JohannesBaiter mannequin commented Mar 18, 2014

BPO 20968
Nosy @rhettinger, @voidspace
Files
  • mock_truediv.diff: Make unittest.mock.MagicMock mock truediv and rtruediv
  • mock_truediv_numerics.diff: Add 'truediv' to global 'numerics' instead of 'magic_methods'
  • mock_truediv_with_tests.diff: Add tests
  • 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 = 'https://github.com/voidspace'
    closed_at = <Date 2014-04-14.15:44:42.196>
    created_at = <Date 2014-03-18.14:13:34.429>
    labels = ['type-bug', 'library']
    title = 'mock.MagicMock does not mock __truediv__'
    updated_at = <Date 2014-04-14.15:44:42.195>
    user = 'https://bugs.python.org/JohannesBaiter'

    bugs.python.org fields:

    activity = <Date 2014-04-14.15:44:42.195>
    actor = 'michael.foord'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2014-04-14.15:44:42.196>
    closer = 'michael.foord'
    components = ['Library (Lib)']
    creation = <Date 2014-03-18.14:13:34.429>
    creator = 'Johannes.Baiter'
    dependencies = []
    files = ['34490', '34491', '34515']
    hgrepos = []
    issue_num = 20968
    keywords = ['patch']
    message_count = 10.0
    messages = ['213964', '213968', '213973', '214077', '214083', '214084', '215228', '215357', '216094', '216101']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'michael.foord', 'python-dev', 'Johannes.Baiter']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20968'
    versions = ['Python 3.4']

    @JohannesBaiter
    Copy link
    Mannequin Author

    JohannesBaiter mannequin commented Mar 18, 2014

    It seems that when creating a MagicMock the magic '__truediv__' method is not replaced with a mock:

    >>> import mock
    >>> foo = mock.MagicMock()
    >>> foo / 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'MagicMock' and 'int'

    The same thing works perfectly fine when using the third party module in Python 2.7, since the 2.x '__div__' seems to be mocked:

    >>> import mock
    >>> foo = mock.MagicMock()
    >>> foo/2
    <MagicMock name='mock.__div__()' id='139760595027088'>

    To clarify the context, I am trying to mock a 'pathlib.Path' object in my unittest, which overloads the division operator, i.e. implements '__truediv__'.

    @JohannesBaiter JohannesBaiter mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 18, 2014
    @JohannesBaiter
    Copy link
    Mannequin Author

    JohannesBaiter mannequin commented Mar 18, 2014

    Attached is a patch that fixes the issue for me.

    @JohannesBaiter
    Copy link
    Mannequin Author

    JohannesBaiter mannequin commented Mar 18, 2014

    I just noticed that I put the magic method names in the wrong place in the patch.
    Attached is a fix that adds 'truediv' to the global 'numberics' variable, this way '__rtruediv__' and '__itruediv__' will be correctly mocked as well.

    @voidspace
    Copy link
    Contributor

    A test would be nice please (good catch on the bug).

    @JohannesBaiter
    Copy link
    Mannequin Author

    JohannesBaiter mannequin commented Mar 19, 2014

    From looking at 'test_numerics', only 'add' is currently tested.
    Probably since the mechanism for all of the numeric magic methods is the same (i.e. create __<op>, __i<op>, __r<op>__).

    Should I add a test for __truediv__ and its inplace and right variants nonetheless?

    @voidspace
    Copy link
    Contributor

    Well, as this is a regression fix we definitley need a test. Ideally we would test all the operations - I didn't realise that only add was tested! However for this specific issue, just testing division is fine, and yes testing in place and right hand as well would be good.

    @JohannesBaiter
    Copy link
    Mannequin Author

    JohannesBaiter mannequin commented Mar 31, 2014

    Sorry for commenting so late, I submitted a version of the patch with unit tests roughly two weeks ago, I just forgot to mention it in a comment. Hereby fixed :-)

    @rhettinger
    Copy link
    Contributor

    The patch looks clean and correct. It passes the test suite.

    I recommend going ahead and applying the patch.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 14, 2014

    New changeset 445ef3b58109 by Michael Foord in branch '3.4':
    bpo-20968. unittest.mock.MagicMock now supports division
    http://hg.python.org/cpython/rev/445ef3b58109

    @voidspace
    Copy link
    Contributor

    Thanks!

    @voidspace voidspace self-assigned this Apr 14, 2014
    @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
    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