Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ad78379
Fix #75988
infohash Feb 9, 2024
53c54e0
Merge branch 'main' into fix-issue-75988
infohash Feb 9, 2024
41e8999
set default return value of functional types as _mock_return_value
infohash Feb 9, 2024
507c285
Merge branch 'main' into fix-issue-75988
infohash Feb 9, 2024
6bf3e13
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 10, 2024
194242f
added test of wrapping child attributes
infohash Feb 10, 2024
8442dcd
Merge branch 'fix-issue-75988' of https://github.com/infohash/cpython…
infohash Feb 10, 2024
21d24f4
Merge branch 'main' into fix-issue-75988
infohash Feb 11, 2024
71beacd
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 19, 2024
65b398f
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 19, 2024
e375a4d
added backward compatibility with explicit return
infohash Feb 19, 2024
ac857f7
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 27, 2024
914b655
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 27, 2024
8e7db2d
Update testmock.py
infohash Feb 27, 2024
0b880a3
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 28, 2024
58b49d6
added docs on the order of precedence
infohash Feb 28, 2024
796db26
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Feb 29, 2024
efd5db6
added test to check default return_value
infohash Feb 29, 2024
48a62f1
Apply suggestions from code review
infohash Mar 8, 2024
514688b
Resolved changes
infohash Mar 8, 2024
49a08dc
Merge remote-tracking branch 'upstream/main' into fix-issue-75988
infohash Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,31 @@ class B(object):
with mock.patch('builtins.open', mock.mock_open()):
mock.mock_open() # should still be valid with open() mocked

def test_create_autospec_wraps_class(self):
"""Autospec a class with wraps & test if the call is passed to the
wrapped object."""
value = 'my_func is called'

class MyClass:
def my_func(self):
return value
class_mock = create_autospec(spec=MyClass, wraps=MyClass)
class_mock.return_value = mock.DEFAULT
self.assertEqual(class_mock().my_func(), value)
# Autospec should also wrap child attributes.
self.assertEqual(class_mock.my_func._mock_wraps, MyClass.my_func)

def test_create_autospec_wraps_function(self):
"""Autospec a function or a method with wraps & test if the call is
passed to the wrapped object."""
value = 'my_func is called'

class MyClass:
def my_func(self):
return value
func_mock = create_autospec(spec=MyClass.my_func, wraps=MyClass.my_func)
self.assertEqual(func_mock(MyClass()), value)

def test_explicit_parent(self):
parent = Mock()
mock1 = Mock(parent=parent, return_value=None)
Expand Down
10 changes: 8 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def reset_mock():
funcopy.method_calls = _CallList()
funcopy.mock_calls = _CallList()

funcopy.return_value = mock.return_value
funcopy.return_value = mock._mock_return_value
funcopy.side_effect = mock.side_effect
funcopy._mock_children = mock._mock_children

Expand Down Expand Up @@ -2785,9 +2785,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):
Expand All @@ -2809,6 +2812,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}

Expand Down