]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-100287: Fix unittest.mock.seal with AsyncMock (#100496)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Sat, 24 Dec 2022 19:39:39 +0000 (13:39 -0600)
committerGitHub <noreply@github.com>
Sat, 24 Dec 2022 19:39:39 +0000 (19:39 +0000)
Lib/test/test_unittest/testmock/testasync.py
Lib/unittest/mock.py
Misc/NEWS.d/next/Library/2022-12-24-08-42-05.gh-issue-100287.n0oEuG.rst [new file with mode: 0644]

index 52a3b71be1ef8d7e71c1ca016a41604ee56b3c62..471162dc50501653f1008950a52e8ca8fe31611c 100644 (file)
@@ -11,7 +11,7 @@ support.requires_working_socket(module=True)
 from asyncio import run, iscoroutinefunction
 from unittest import IsolatedAsyncioTestCase
 from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock,
-                           create_autospec, sentinel, _CallList)
+                           create_autospec, sentinel, _CallList, seal)
 
 
 def tearDownModule():
@@ -300,6 +300,14 @@ class AsyncSpecTest(unittest.TestCase):
         self.assertIsInstance(mock.async_method, AsyncMock)
         self.assertIsInstance(mock.normal_method, Mock)
 
+    def test_spec_normal_methods_on_class_with_mock_seal(self):
+        mock = Mock(AsyncClass)
+        seal(mock)
+        with self.assertRaises(AttributeError):
+            mock.normal_method
+        with self.assertRaises(AttributeError):
+            mock.async_method
+
     def test_spec_async_attributes_instance(self):
         async_instance = AsyncClass()
         async_instance.async_func_attr = async_func
@@ -1089,3 +1097,7 @@ class AsyncMockAssert(unittest.TestCase):
                         'Actual: [call(1)]'))) as cm:
             self.mock.assert_has_awaits([call(), call(1, 2)])
         self.assertIsInstance(cm.exception.__cause__, TypeError)
+
+
+if __name__ == '__main__':
+    unittest.main()
index 583ab74a82553131cae5f21843742849333c7fb0..994947cad518f9908a403b2794621048f30fb951 100644 (file)
@@ -1019,15 +1019,15 @@ class NonCallableMock(Base):
 
         For non-callable mocks the callable variant will be used (rather than
         any custom subclass)."""
-        _new_name = kw.get("_new_name")
-        if _new_name in self.__dict__['_spec_asyncs']:
-            return AsyncMock(**kw)
-
         if self._mock_sealed:
             attribute = f".{kw['name']}" if "name" in kw else "()"
             mock_name = self._extract_mock_name() + attribute
             raise AttributeError(mock_name)
 
+        _new_name = kw.get("_new_name")
+        if _new_name in self.__dict__['_spec_asyncs']:
+            return AsyncMock(**kw)
+
         _type = type(self)
         if issubclass(_type, MagicMock) and _new_name in _async_method_magics:
             # Any asynchronous magic becomes an AsyncMock
diff --git a/Misc/NEWS.d/next/Library/2022-12-24-08-42-05.gh-issue-100287.n0oEuG.rst b/Misc/NEWS.d/next/Library/2022-12-24-08-42-05.gh-issue-100287.n0oEuG.rst
new file mode 100644 (file)
index 0000000..b353f08
--- /dev/null
@@ -0,0 +1 @@
+Fix the interaction of :func:`unittest.mock.seal` with :class:`unittest.mock.AsyncMock`.