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.

classification
Title: MagicMock initializer fails for magic methods
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lukasz.langa Nosy List: berdario, chillaranand, kjachim, lukasz.langa, michael.foord, python-dev, rbcollins
Priority: normal Keywords: patch

Created on 2015-01-24 17:20 by berdario, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_mock.py chillaranand, 2015-03-23 12:37 sample test
fix_magic_in_init.diff kjachim, 2015-04-13 15:26 Tests for this bug + proposed fix. review
fix_magic_in_init_v2.diff kjachim, 2015-04-13 15:55 review
Messages (8)
msg234623 - (view) Author: (berdario) Date: 2015-01-24 17:20
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
msg239013 - (view) Author: Anand Reddy Pandikunta (chillaranand) * Date: 2015-03-23 12:37
mock itself fails
msg240623 - (view) Author: Kasia Jachim (kjachim) Date: 2015-04-13 15:26
Tests for this bug + proposed fix.
msg240626 - (view) Author: Kasia Jachim (kjachim) Date: 2015-04-13 15:39
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")
msg240631 - (view) Author: Kasia Jachim (kjachim) Date: 2015-04-13 15:55
Added more logging to the patch.
msg240844 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-04-14 06:12
New changeset 4d1236b180be by Łukasz Langa in branch 'default':
Issue #23310: Fix MagicMock's initializer to work with __methods__.
https://hg.python.org/cpython/rev/4d1236b180be
msg240853 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-04-14 07:17
New changeset 8e5592a43d65 by Łukasz Langa in branch '3.4':
Issue #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 (#23310)
https://hg.python.org/cpython/rev/dd8f48ff9480
msg240854 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2015-04-14 07:19
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.
History
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67499
2015-04-14 07:24:37lukasz.langasetstage: needs patch -> resolved
2015-04-14 07:19:28lukasz.langasetstatus: open -> closed
title: Mock constructor configuration fails for magic methods -> MagicMock initializer fails for magic methods
messages: + msg240854

keywords: - needs review
resolution: fixed
2015-04-14 07:17:18python-devsetmessages: + msg240853
2015-04-14 06:12:57python-devsetnosy: + python-dev
messages: + msg240844
2015-04-13 16:46:45lukasz.langasetkeywords: + needs review
versions: + Python 3.5
2015-04-13 16:04:42lukasz.langasetassignee: lukasz.langa

nosy: + lukasz.langa
2015-04-13 15:55:03kjachimsetfiles: + fix_magic_in_init_v2.diff

messages: + msg240631
2015-04-13 15:39:41kjachimsetmessages: + msg240626
2015-04-13 15:26:00kjachimsetfiles: + fix_magic_in_init.diff

nosy: + kjachim
messages: + msg240623

keywords: + patch
2015-03-23 12:37:33chillaranandsetfiles: + test_mock.py
title: MagicMock constructor configuration fails for magic methods -> Mock constructor configuration fails for magic methods
nosy: + chillaranand

messages: + msg239013
2015-03-16 03:04:48rbcollinssettype: crash -> behavior
2015-03-16 02:52:13rbcollinssettype: crash
stage: needs patch
2015-03-03 22:43:01ned.deilysetnosy: + rbcollins, michael.foord
2015-01-24 17:20:39berdariocreate