Skip to content

Commit e4b43eb

Browse files
authoredDec 24, 2022
gh-100287: Fix unittest.mock.seal with AsyncMock (#100496)
1 parent 46e6a28 commit e4b43eb

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed
 

‎Lib/test/test_unittest/testmock/testasync.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from asyncio import run, iscoroutinefunction
1212
from unittest import IsolatedAsyncioTestCase
1313
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock,
14-
create_autospec, sentinel, _CallList)
14+
create_autospec, sentinel, _CallList, seal)
1515

1616

1717
def tearDownModule():
@@ -300,6 +300,14 @@ def test_spec_normal_methods_on_class_with_mock(self):
300300
self.assertIsInstance(mock.async_method, AsyncMock)
301301
self.assertIsInstance(mock.normal_method, Mock)
302302

303+
def test_spec_normal_methods_on_class_with_mock_seal(self):
304+
mock = Mock(AsyncClass)
305+
seal(mock)
306+
with self.assertRaises(AttributeError):
307+
mock.normal_method
308+
with self.assertRaises(AttributeError):
309+
mock.async_method
310+
303311
def test_spec_async_attributes_instance(self):
304312
async_instance = AsyncClass()
305313
async_instance.async_func_attr = async_func
@@ -1089,3 +1097,7 @@ async def f(x=None): pass
10891097
'Actual: [call(1)]'))) as cm:
10901098
self.mock.assert_has_awaits([call(), call(1, 2)])
10911099
self.assertIsInstance(cm.exception.__cause__, TypeError)
1100+
1101+
1102+
if __name__ == '__main__':
1103+
unittest.main()

‎Lib/unittest/mock.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1019,15 +1019,15 @@ def _get_child_mock(self, /, **kw):
10191019
10201020
For non-callable mocks the callable variant will be used (rather than
10211021
any custom subclass)."""
1022-
_new_name = kw.get("_new_name")
1023-
if _new_name in self.__dict__['_spec_asyncs']:
1024-
return AsyncMock(**kw)
1025-
10261022
if self._mock_sealed:
10271023
attribute = f".{kw['name']}" if "name" in kw else "()"
10281024
mock_name = self._extract_mock_name() + attribute
10291025
raise AttributeError(mock_name)
10301026

1027+
_new_name = kw.get("_new_name")
1028+
if _new_name in self.__dict__['_spec_asyncs']:
1029+
return AsyncMock(**kw)
1030+
10311031
_type = type(self)
10321032
if issubclass(_type, MagicMock) and _new_name in _async_method_magics:
10331033
# Any asynchronous magic becomes an AsyncMock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the interaction of :func:`unittest.mock.seal` with :class:`unittest.mock.AsyncMock`.

0 commit comments

Comments
 (0)
Please sign in to comment.