Skip to content

Commit

Permalink
gh-91803: Mock - fix error when using autospec methods with seal (#92213
Browse files Browse the repository at this point in the history
)

Fixes #91803.

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 7, 2022
1 parent 728e42f commit c6325b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Lib/test/test_unittest/testmock/testsealable.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def ban(self):
self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock)
self.assertIsInstance(foo.Baz.ban, mock.MagicMock)

# see gh-91803
self.assertIsInstance(foo.bar2(), mock.MagicMock)

self.assertEqual(foo.bar1(), 'a')
foo.bar1.return_value = 'new_a'
self.assertEqual(foo.bar1(), 'new_a')
Expand All @@ -212,7 +215,7 @@ def ban(self):
with self.assertRaises(AttributeError):
foo.bar = 1
with self.assertRaises(AttributeError):
foo.bar2()
foo.bar2().x

foo.bar2.return_value = 'bar2'
self.assertEqual(foo.bar2(), 'bar2')
Expand Down
1 change: 1 addition & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2745,6 +2745,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
_new_parent=parent,
**kwargs)
mock._mock_children[entry] = new
new.return_value = child_klass()
_check_signature(original, new, skipfirst=skipfirst)

# so functions created with _set_signature become instance attributes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an error when using a method of objects mocked with
:func:`unittest.mock.create_autospec` after it was sealed with
:func:`unittest.mock.seal` function.

0 comments on commit c6325b1

Please sign in to comment.