bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock (#16029)
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index beed717..1acafc5 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2033,6 +2033,12 @@
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