Skip to content
Snippets Groups Projects
Unverified Commit 67b4d277 authored by Nikita Sobolev's avatar Nikita Sobolev Committed by GitHub
Browse files

gh-98086: Now ``patch.dict`` can decorate async functions (#98095)

parent 97c493dd
Branches
Tags
No related merge requests found
......@@ -149,6 +149,23 @@ async def test_async():
run(test_async())
def test_patch_dict_async_def(self):
foo = {'a': 'a'}
@patch.dict(foo, {'a': 'b'})
async def test_async():
self.assertEqual(foo['a'], 'b')
self.assertTrue(iscoroutinefunction(test_async))
run(test_async())
def test_patch_dict_async_def_context(self):
foo = {'a': 'a'}
async def test_async():
with patch.dict(foo, {'a': 'b'}):
self.assertEqual(foo['a'], 'b')
run(test_async())
class AsyncMockTest(unittest.TestCase):
def test_iscoroutinefunction_default(self):
......
......@@ -1809,6 +1809,12 @@ def __init__(self, in_dict, values=(), clear=False, **kwargs):
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 @@ def _inner(*args, **kw):
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)
......
Make sure ``patch.dict()`` can be applied on async functions.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment