mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-39082: Allow AsyncMock to correctly patch static/class methods (GH-18116)
This commit is contained in:
parent
d0d9fa8c5e
commit
62865f4532
3 changed files with 26 additions and 0 deletions
|
@ -46,6 +46,8 @@ _safe_super = super
|
||||||
def _is_async_obj(obj):
|
def _is_async_obj(obj):
|
||||||
if _is_instance_mock(obj) and not isinstance(obj, AsyncMock):
|
if _is_instance_mock(obj) and not isinstance(obj, AsyncMock):
|
||||||
return False
|
return False
|
||||||
|
if hasattr(obj, '__func__'):
|
||||||
|
obj = getattr(obj, '__func__')
|
||||||
return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj)
|
return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,15 @@ class AsyncClass:
|
||||||
def normal_method(self):
|
def normal_method(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def async_class_method(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def async_static_method():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AwaitableClass:
|
class AwaitableClass:
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
yield
|
yield
|
||||||
|
@ -71,6 +80,20 @@ class AsyncPatchDecoratorTest(unittest.TestCase):
|
||||||
|
|
||||||
test_async()
|
test_async()
|
||||||
|
|
||||||
|
def test_is_AsyncMock_patch_staticmethod(self):
|
||||||
|
@patch.object(AsyncClass, 'async_static_method')
|
||||||
|
def test_async(mock_method):
|
||||||
|
self.assertIsInstance(mock_method, AsyncMock)
|
||||||
|
|
||||||
|
test_async()
|
||||||
|
|
||||||
|
def test_is_AsyncMock_patch_classmethod(self):
|
||||||
|
@patch.object(AsyncClass, 'async_class_method')
|
||||||
|
def test_async(mock_method):
|
||||||
|
self.assertIsInstance(mock_method, AsyncMock)
|
||||||
|
|
||||||
|
test_async()
|
||||||
|
|
||||||
def test_async_def_patch(self):
|
def test_async_def_patch(self):
|
||||||
@patch(f"{__name__}.async_func", return_value=1)
|
@patch(f"{__name__}.async_func", return_value=1)
|
||||||
@patch(f"{__name__}.async_func_args", return_value=2)
|
@patch(f"{__name__}.async_func_args", return_value=2)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Allow AsyncMock to correctly patch static/class methods
|
Loading…
Add table
Add a link
Reference in a new issue