Skip to content

Commit 3a00950

Browse files
committed
make sure patch.dict() can be applied on async functions
Code is from gh#python/cpython@88b101f, it was released upstream in 3.10.9. Fixes: gh#python#98086 Patch: 99366-patch.dict-can-decorate-async.patch
1 parent 9e8f7c7 commit 3a00950

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Lib/unittest/mock.py

+18
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,12 @@ def __init__(self, in_dict, values=(), clear=False, **kwargs):
15951595
def __call__(self, f):
15961596
if isinstance(f, type):
15971597
return self.decorate_class(f)
1598+
if inspect.iscoroutinefunction(f):
1599+
return self.decorate_async_callable(f)
1600+
return self.decorate_callable(f)
1601+
1602+
1603+
def decorate_callable(self, f):
15981604
@wraps(f)
15991605
def _inner(*args, **kw):
16001606
self._patch_dict()
@@ -1606,6 +1612,18 @@ def _inner(*args, **kw):
16061612
return _inner
16071613

16081614

1615+
def decorate_async_callable(self, f):
1616+
@wraps(f)
1617+
async def _inner(*args, **kw):
1618+
self._patch_dict()
1619+
try:
1620+
return await f(*args, **kw)
1621+
finally:
1622+
self._unpatch_dict()
1623+
1624+
return _inner
1625+
1626+
16091627
def decorate_class(self, klass):
16101628
for attr in dir(klass):
16111629
attr_value = getattr(klass, attr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make sure ``patch.dict()`` can be applied on async functions.

0 commit comments

Comments
 (0)