[Python-checkins] gh-91803: Mock - fix error when using autospec methods with seal (#92213)
cjw296
webhook-mailer at python.org
Mon Nov 7 02:25:19 EST 2022
https://github.com/python/cpython/commit/c6325b1c9fe60f72bb3fa4b8570a699e9e97af53
commit: c6325b1c9fe60f72bb3fa4b8570a699e9e97af53
branch: main
author: andrei kulakov <andrei.avk at gmail.com>
committer: cjw296 <chris at withers.org>
date: 2022-11-07T07:24:46Z
summary:
gh-91803: Mock - fix error when using autospec methods with seal (#92213)
Fixes https://github.com/python/cpython/issues/91803.
Co-authored-by: Karthikeyan Singaravelan <tir.karthi at gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst
M Lib/test/test_unittest/testmock/testsealable.py
M Lib/unittest/mock.py
diff --git a/Lib/test/test_unittest/testmock/testsealable.py b/Lib/test/test_unittest/testmock/testsealable.py
index daba2b49b46f..e0c38293cffd 100644
--- a/Lib/test/test_unittest/testmock/testsealable.py
+++ b/Lib/test/test_unittest/testmock/testsealable.py
@@ -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')
@@ -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')
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b8f4e57f0b49..096b1a571473 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -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,
diff --git a/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst b/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst
new file mode 100644
index 000000000000..14829e8fe777
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst
@@ -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.
More information about the Python-checkins
mailing list