Skip to content

Commit c6325b1

Browse files
akulakovtirkarthiiritkatriel
authored
gh-91803: Mock - fix error when using autospec methods with seal (#92213)
Fixes #91803. Co-authored-by: Karthikeyan Singaravelan <[email protected]> Co-authored-by: Irit Katriel <[email protected]>
1 parent 728e42f commit c6325b1

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test_unittest/testmock/testsealable.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ def ban(self):
200200
self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock)
201201
self.assertIsInstance(foo.Baz.ban, mock.MagicMock)
202202

203+
# see gh-91803
204+
self.assertIsInstance(foo.bar2(), mock.MagicMock)
205+
203206
self.assertEqual(foo.bar1(), 'a')
204207
foo.bar1.return_value = 'new_a'
205208
self.assertEqual(foo.bar1(), 'new_a')
@@ -212,7 +215,7 @@ def ban(self):
212215
with self.assertRaises(AttributeError):
213216
foo.bar = 1
214217
with self.assertRaises(AttributeError):
215-
foo.bar2()
218+
foo.bar2().x
216219

217220
foo.bar2.return_value = 'bar2'
218221
self.assertEqual(foo.bar2(), 'bar2')

Lib/unittest/mock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
27452745
_new_parent=parent,
27462746
**kwargs)
27472747
mock._mock_children[entry] = new
2748+
new.return_value = child_klass()
27482749
_check_signature(original, new, skipfirst=skipfirst)
27492750

27502751
# so functions created with _set_signature become instance attributes,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an error when using a method of objects mocked with
2+
:func:`unittest.mock.create_autospec` after it was sealed with
3+
:func:`unittest.mock.seal` function.

0 commit comments

Comments
 (0)