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

MagicMock initializer fails for magic methods #67499

Closed
berdario mannequin opened this issue Jan 24, 2015 · 8 comments
Closed

MagicMock initializer fails for magic methods #67499

berdario mannequin opened this issue Jan 24, 2015 · 8 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@berdario
Copy link
Mannequin

berdario mannequin commented Jan 24, 2015

BPO 23310
Nosy @rbtcollins, @voidspace, @ambv, @ChillarAnand
Files
  • test_mock.py: sample test
  • fix_magic_in_init.diff: Tests for this bug + proposed fix.
  • fix_magic_in_init_v2.diff
  • 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/ambv'
    closed_at = <Date 2015-04-14.07:19:28.092>
    created_at = <Date 2015-01-24.17:20:39.542>
    labels = ['type-bug', 'library']
    title = 'MagicMock initializer fails for magic methods'
    updated_at = <Date 2015-04-14.07:24:37.325>
    user = 'https://bugs.python.org/berdario'

    bugs.python.org fields:

    activity = <Date 2015-04-14.07:24:37.325>
    actor = 'lukasz.langa'
    assignee = 'lukasz.langa'
    closed = True
    closed_date = <Date 2015-04-14.07:19:28.092>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2015-01-24.17:20:39.542>
    creator = 'berdario'
    dependencies = []
    files = ['38654', '38919', '38922']
    hgrepos = []
    issue_num = 23310
    keywords = ['patch']
    message_count = 8.0
    messages = ['234623', '239013', '240623', '240626', '240631', '240844', '240853', '240854']
    nosy_count = 7.0
    nosy_names = ['rbcollins', 'michael.foord', 'lukasz.langa', 'python-dev', 'berdario', 'chillaranand', 'kjachim']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23310'
    versions = ['Python 3.4', 'Python 3.5']

    @berdario
    Copy link
    Mannequin Author

    berdario mannequin commented Jan 24, 2015

    I guess this should be expected... too much magic :P

    >>> from unittest.mock import MagicMock
    >>> MagicMock(**{'__hash__.return_value': "FIXME"})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/nix/store/qlvbf3n3y34idxcgwwhsi9pq26v28q99-python3-3.4.2/lib/python3.4/unittest/mock.py", line 1772, in __init__
        _safe_super(MagicMixin, self).__init__(*args, **kw)
      File "/nix/store/qlvbf3n3y34idxcgwwhsi9pq26v28q99-python3-3.4.2/lib/python3.4/unittest/mock.py", line 881, in __init__
        _spec_state, _new_name, _new_parent, **kwargs
      File "/nix/store/qlvbf3n3y34idxcgwwhsi9pq26v28q99-python3-3.4.2/lib/python3.4/unittest/mock.py", line 410, in __init__
        self.configure_mock(**kwargs)
      File "/nix/store/qlvbf3n3y34idxcgwwhsi9pq26v28q99-python3-3.4.2/lib/python3.4/unittest/mock.py", line 560, in configure_mock
        setattr(obj, final, val)
    AttributeError: 'method-wrapper' object has no attribute 'return_value'

    The same happens with e.g. __str__

    >> m.configure_mock(**{'__hash__.return_value': 1})

    works just fine, instead

    @berdario berdario mannequin added the stdlib Python modules in the Lib dir label Jan 24, 2015
    @rbtcollins rbtcollins added type-crash A hard crash of the interpreter, possibly with a core dump type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 16, 2015
    @ChillarAnand
    Copy link
    Mannequin

    ChillarAnand mannequin commented Mar 23, 2015

    mock itself fails

    @ChillarAnand ChillarAnand mannequin changed the title MagicMock constructor configuration fails for magic methods Mock constructor configuration fails for magic methods Mar 23, 2015
    @kjachim
    Copy link
    Mannequin

    kjachim mannequin commented Apr 13, 2015

    Tests for this bug + proposed fix.

    @kjachim
    Copy link
    Mannequin

    kjachim mannequin commented Apr 13, 2015

    For Mock both the following tests fail, I would say it is an expected behavior.

    def test_setting_magic_method_for_mock(self):
        m = Mock()
        m.configure_mock(**{'__str__.return_value': "14"})
        self.assertEqual(str(m), "14")
            
    def test_setting_magic_method_in_mock_initialization(self):
        m = Mock(**{'__str__.return_value': "12"})
        self.assertEqual(str(m), "12")

    @kjachim
    Copy link
    Mannequin

    kjachim mannequin commented Apr 13, 2015

    Added more logging to the patch.

    @ambv ambv self-assigned this Apr 13, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 14, 2015

    New changeset 4d1236b180be by Łukasz Langa in branch 'default':
    Issue bpo-23310: Fix MagicMock's initializer to work with __methods__.
    https://hg.python.org/cpython/rev/4d1236b180be

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 14, 2015

    New changeset 8e5592a43d65 by Łukasz Langa in branch '3.4':
    Issue bpo-23310: Fix MagicMock's initializer to work with __methods__.
    https://hg.python.org/cpython/rev/8e5592a43d65

    New changeset dd8f48ff9480 by Łukasz Langa in branch 'default':
    Merge 3.4 (bpo-23310)
    https://hg.python.org/cpython/rev/dd8f48ff9480

    @ambv
    Copy link
    Contributor

    ambv commented Apr 14, 2015

    Awesome! Thank you for your patch, Kasia. For the record, I left the non-magic Mock behavior untouched since Kasia rightfully points out that in this case m.configure_mock() also does not work.

    @ambv ambv closed this as completed Apr 14, 2015
    @ambv ambv changed the title Mock constructor configuration fails for magic methods MagicMock initializer fails for magic methods Apr 14, 2015
    @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