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
Comments
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__
works just fine, instead |
mock itself fails |
Tests for this bug + proposed fix. |
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") |
Added more logging to the patch. |
New changeset 4d1236b180be by Łukasz Langa in branch 'default': |
New changeset 8e5592a43d65 by Łukasz Langa in branch '3.4': New changeset dd8f48ff9480 by Łukasz Langa in branch 'default': |
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. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: