diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-11-11 08:04:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 08:04:30 (GMT) |
commit | 67b4d2772c5124b908f8ed9b13166a79bbeb88d2 (patch) | |
tree | 5f1615e79bf0b5d47c5b73fd34aa9a4d97a7f6e4 /Lib/unittest | |
parent | 97c493dd3543c7c3bb5319587c162f46271d4c5d (diff) | |
download | cpython-67b4d2772c5124b908f8ed9b13166a79bbeb88d2.zip cpython-67b4d2772c5124b908f8ed9b13166a79bbeb88d2.tar.gz cpython-67b4d2772c5124b908f8ed9b13166a79bbeb88d2.tar.bz2 |
gh-98086: Now ``patch.dict`` can decorate async functions (#98095)
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 096b1a5..a273753 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1809,6 +1809,12 @@ class _patch_dict(object): def __call__(self, f): if isinstance(f, type): return self.decorate_class(f) + if inspect.iscoroutinefunction(f): + return self.decorate_async_callable(f) + return self.decorate_callable(f) + + + def decorate_callable(self, f): @wraps(f) def _inner(*args, **kw): self._patch_dict() @@ -1820,6 +1826,18 @@ class _patch_dict(object): return _inner + def decorate_async_callable(self, f): + @wraps(f) + async def _inner(*args, **kw): + self._patch_dict() + try: + return await f(*args, **kw) + finally: + self._unpatch_dict() + + return _inner + + def decorate_class(self, klass): for attr in dir(klass): attr_value = getattr(klass, attr) |