summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorinfohash <46137868+infohash@users.noreply.github.com>2024-03-08 19:14:32 (GMT)
committerGitHub <noreply@github.com>2024-03-08 19:14:32 (GMT)
commit735fc2cbbcf875c359021b5b2af7f4c29f4cf66d (patch)
tree1dabd0f3ea328367d4f0864f9b7c2e6ee86ecd2c /Lib/unittest/mock.py
parent7db871e4fa48eef20ea22414b226998f83facfd6 (diff)
downloadcpython-735fc2cbbcf875c359021b5b2af7f4c29f4cf66d.zip
cpython-735fc2cbbcf875c359021b5b2af7f4c29f4cf66d.tar.gz
cpython-735fc2cbbcf875c359021b5b2af7f4c29f4cf66d.tar.bz2
gh-75988: Fix issues with autospec ignoring wrapped object (#115223)
* set default return value of functional types as _mock_return_value * added test of wrapping child attributes * added backward compatibility with explicit return * added docs on the order of precedence * added test to check default return_value
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 93f4d97..1799e9b 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -573,7 +573,7 @@ class NonCallableMock(Base):
if self._mock_delegate is not None:
ret = self._mock_delegate.return_value
- if ret is DEFAULT:
+ if ret is DEFAULT and self._mock_wraps is None:
ret = self._get_child_mock(
_new_parent=self, _new_name='()'
)
@@ -1234,6 +1234,9 @@ class CallableMixin(Base):
if self._mock_return_value is not DEFAULT:
return self.return_value
+ if self._mock_delegate and self._mock_delegate.return_value is not DEFAULT:
+ return self.return_value
+
if self._mock_wraps is not None:
return self._mock_wraps(*args, **kwargs)
@@ -2785,9 +2788,12 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if _parent is not None and not instance:
_parent._mock_children[_name] = mock
+ wrapped = kwargs.get('wraps')
+
if is_type and not instance and 'return_value' not in kwargs:
mock.return_value = create_autospec(spec, spec_set, instance=True,
- _name='()', _parent=mock)
+ _name='()', _parent=mock,
+ wraps=wrapped)
for entry in dir(spec):
if _is_magic(entry):
@@ -2809,6 +2815,9 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
continue
kwargs = {'spec': original}
+ # Wrap child attributes also.
+ if wrapped and hasattr(wrapped, entry):
+ kwargs.update(wraps=original)
if spec_set:
kwargs = {'spec_set': original}