[issue36996] unittest.mock.patch decorator doesn't work with async functions
Andrew Svetlov
report at bugs.python.org
Tue May 21 15:06:31 EDT 2019
Andrew Svetlov <andrew.svetlov at gmail.com> added the comment:
Thank you very much for raising the question.
@patch(...) creates _patch class instance.
For decoration _patch.__call__ is used.
def __call__(self, func):
if isinstance(func, type):
return self.decorate_class(func)
return self.decorate_callable(func)
The code can be modified to
def __call__(self, func):
if isinstance(func, type):
return self.decorate_class(func)
if inspect.iscoroutinefunction(func):
return self.decorate_async_func(func)
return self.decorate_callable(func)
decorate_async_func can do all the same as decorate_callable with the only difference: internal
@wraps(func)
def patched(*args, **keywargs):
should be replaced with
@wraps(func)
async def patched(*args, **keywargs):
and
return func(*args, **keywargs)
replaced with
return await func(*args, **keywargs)
Pretty straightforward.
I did not check the code though.
I'm pretty busy up to 3.8 feature freeze to make the PR for proposed change but I love to review the existing patch.
Do want somebody to be a champion?
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36996>
_______________________________________
More information about the Python-bugs-list
mailing list