gh-94924: support inspect.iscoroutinefunction in create_autospec(async_def) (#94962)

* support inspect.iscoroutinefunction in create_autospec(async_def)

* test create_autospec with inspect.iscoroutine and inspect.iscoroutinefunction

* test when create_autospec functions check their signature
This commit is contained in:
Thomas Grainger 2023-06-09 14:29:09 +01:00 committed by GitHub
parent 0f885ffa94
commit 9bf8d825a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 2 deletions

View file

@ -204,6 +204,33 @@ def _set_signature(mock, original, instance=False):
_setup_func(funcopy, mock, sig)
return funcopy
def _set_async_signature(mock, original, instance=False, is_async_mock=False):
# creates an async function with signature (*args, **kwargs) that delegates to a
# mock. It still does signature checking by calling a lambda with the same
# signature as the original.
skipfirst = isinstance(original, type)
result = _get_signature_object(original, instance, skipfirst)
if result is None:
return mock
func, sig = result
def checksig(*args, **kwargs):
sig.bind(*args, **kwargs)
_copy_func_details(func, checksig)
name = original.__name__
if not name.isidentifier():
name = 'funcopy'
context = {'_checksig_': checksig, 'mock': mock}
src = """async def %s(*args, **kwargs):
_checksig_(*args, **kwargs)
return await mock(*args, **kwargs)""" % name
exec (src, context)
funcopy = context[name]
_setup_func(funcopy, mock, sig)
_setup_async_mock(funcopy)
return funcopy
def _setup_func(funcopy, mock, sig):
funcopy.mock = mock
@ -2745,9 +2772,10 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if isinstance(spec, FunctionTypes):
# should only happen at the top level because we don't
# recurse for functions
mock = _set_signature(mock, spec)
if is_async_func:
_setup_async_mock(mock)
mock = _set_async_signature(mock, spec)
else:
mock = _set_signature(mock, spec)
else:
_check_signature(spec, mock, is_type, instance)