]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91803: Mock - fix error when using autospec methods with seal (#92213)
authorandrei kulakov <andrei.avk@gmail.com>
Mon, 7 Nov 2022 07:24:46 +0000 (02:24 -0500)
committerGitHub <noreply@github.com>
Mon, 7 Nov 2022 07:24:46 +0000 (07:24 +0000)
Fixes https://github.com/python/cpython/issues/91803.

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/test/test_unittest/testmock/testsealable.py
Lib/unittest/mock.py
Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst [new file with mode: 0644]

index daba2b49b46f6351d3032582935beea3ab615939..e0c38293cffd7ac51eb6957d37e17d26f0fa637e 100644 (file)
@@ -200,6 +200,9 @@ class TestSealable(unittest.TestCase):
                 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 @@ class TestSealable(unittest.TestCase):
                 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')
index b8f4e57f0b49273a10aa99b1901164bb676d1c37..096b1a571473629a3b65e5753ba1762f8014661c 100644 (file)
@@ -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 (file)
index 0000000..14829e8
--- /dev/null
@@ -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.