Skip to content

Commit

Permalink
bpo-39966: Revert "bpo-25597: Ensure wraps' return value is used for …
Browse files Browse the repository at this point in the history
…magic methods in MagicMock" (GH-19734)

* Revert "bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock (#16029)"

This reverts commit 72b1004657e60c900e4cd031b2635b587f4b280e.

Backports: 521c8d6806adf0305c158d280ec00cca48e8ab22
Signed-off-by: Chris Withers <[email protected]>
  • Loading branch information
tirkarthi authored and cjw296 committed Dec 10, 2020
1 parent 902eea1 commit c627515
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.d/2020-04-27-14-48-43.bpo-39966.N5yXUe.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Revert bpo-25597. :class:`unittest.mock.MagicMock` with wraps' set uses
default return values for magic methods.
6 changes: 0 additions & 6 deletions mock/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2025,12 +2025,6 @@ def __aiter__():


def _set_return_value(mock, method, name):
# If _mock_wraps is present then attach it so that wrapped object
# is used for return value is used when called.
if mock._mock_wraps is not None:
method._mock_wraps = getattr(mock._mock_wraps, name)
return

fixed = _return_values.get(name, DEFAULT)
if fixed is not DEFAULT:
method.return_value = fixed
Expand Down
16 changes: 10 additions & 6 deletions mock/tests/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,16 @@ def method(self): pass


def test_magic_method_wraps_dict(self):
# bpo-25597: MagicMock with wrap doesn't call wrapped object's
# method for magic methods with default values.
data = {'foo': 'bar'}

wrapped_dict = MagicMock(wraps=data)
self.assertEqual(wrapped_dict.get('foo'), 'bar')
self.assertEqual(wrapped_dict['foo'], 'bar')
self.assertTrue('foo' in wrapped_dict)
# Accessing key gives a MagicMock
self.assertIsInstance(wrapped_dict['foo'], MagicMock)
# __contains__ method has a default value of False
self.assertFalse('foo' in wrapped_dict)

# return_value is non-sentinel and takes precedence over wrapped value.
wrapped_dict.get.return_value = 'return_value'
Expand All @@ -732,14 +736,13 @@ def test_magic_method_wraps_dict(self):
self.assertEqual(wrapped_dict.get('foo'), 'bar')

self.assertEqual(wrapped_dict.get('baz'), None)
with self.assertRaises(KeyError):
wrapped_dict['baz']
self.assertIsInstance(wrapped_dict['baz'], MagicMock)
self.assertFalse('bar' in wrapped_dict)

data['baz'] = 'spam'
self.assertEqual(wrapped_dict.get('baz'), 'spam')
self.assertEqual(wrapped_dict['baz'], 'spam')
self.assertTrue('baz' in wrapped_dict)
self.assertIsInstance(wrapped_dict['baz'], MagicMock)
self.assertFalse('bar' in wrapped_dict)

del data['baz']
self.assertEqual(wrapped_dict.get('baz'), None)
Expand All @@ -759,6 +762,7 @@ def __custom_method__(self):
klass = MagicMock(wraps=Foo)
obj = klass()
self.assertEqual(obj.__getitem__(2), 2)
self.assertEqual(obj[2], 2)
self.assertEqual(obj.__custom_method__(), "foo")


Expand Down

0 comments on commit c627515

Please sign in to comment.