mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
Use relative imports in mock and its tests to help backporting (GH-18197)
* asyncio.run only available in 3.8+ * iscoroutinefunction has important bungfixes in 3.8 * IsolatedAsyncioTestCase only available in 3.8+
This commit is contained in:
parent
997443c14c
commit
c7dd3c7d87
3 changed files with 82 additions and 79 deletions
|
|
@ -30,6 +30,7 @@
|
|||
import pprint
|
||||
import sys
|
||||
import builtins
|
||||
from asyncio import iscoroutinefunction
|
||||
from types import CodeType, ModuleType, MethodType
|
||||
from unittest.util import safe_repr
|
||||
from functools import wraps, partial
|
||||
|
|
@ -48,12 +49,12 @@ def _is_async_obj(obj):
|
|||
return False
|
||||
if hasattr(obj, '__func__'):
|
||||
obj = getattr(obj, '__func__')
|
||||
return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj)
|
||||
return iscoroutinefunction(obj) or inspect.isawaitable(obj)
|
||||
|
||||
|
||||
def _is_async_func(func):
|
||||
if getattr(func, '__code__', None):
|
||||
return asyncio.iscoroutinefunction(func)
|
||||
return iscoroutinefunction(func)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
|
@ -488,7 +489,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
|
|||
_spec_asyncs = []
|
||||
|
||||
for attr in dir(spec):
|
||||
if asyncio.iscoroutinefunction(getattr(spec, attr, None)):
|
||||
if iscoroutinefunction(getattr(spec, attr, None)):
|
||||
_spec_asyncs.append(attr)
|
||||
|
||||
if spec is not None and not _is_list(spec):
|
||||
|
|
@ -2152,7 +2153,7 @@ class AsyncMockMixin(Base):
|
|||
|
||||
def __init__(self, /, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# asyncio.iscoroutinefunction() checks _is_coroutine property to say if an
|
||||
# iscoroutinefunction() checks _is_coroutine property to say if an
|
||||
# object is a coroutine. Without this check it looks to see if it is a
|
||||
# function/method, which in this case it is not (since it is an
|
||||
# AsyncMock).
|
||||
|
|
@ -2188,7 +2189,7 @@ async def _execute_mock_call(self, /, *args, **kwargs):
|
|||
raise StopAsyncIteration
|
||||
if _is_exception(result):
|
||||
raise result
|
||||
elif asyncio.iscoroutinefunction(effect):
|
||||
elif iscoroutinefunction(effect):
|
||||
result = await effect(*args, **kwargs)
|
||||
else:
|
||||
result = effect(*args, **kwargs)
|
||||
|
|
@ -2200,7 +2201,7 @@ async def _execute_mock_call(self, /, *args, **kwargs):
|
|||
return self.return_value
|
||||
|
||||
if self._mock_wraps is not None:
|
||||
if asyncio.iscoroutinefunction(self._mock_wraps):
|
||||
if iscoroutinefunction(self._mock_wraps):
|
||||
return await self._mock_wraps(*args, **kwargs)
|
||||
return self._mock_wraps(*args, **kwargs)
|
||||
|
||||
|
|
@ -2337,7 +2338,7 @@ class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock):
|
|||
recognized as an async function, and the result of a call is an awaitable:
|
||||
|
||||
>>> mock = AsyncMock()
|
||||
>>> asyncio.iscoroutinefunction(mock)
|
||||
>>> iscoroutinefunction(mock)
|
||||
True
|
||||
>>> inspect.isawaitable(mock())
|
||||
True
|
||||
|
|
@ -2710,7 +2711,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
|
|||
|
||||
skipfirst = _must_skip(spec, entry, is_type)
|
||||
kwargs['_eat_self'] = skipfirst
|
||||
if asyncio.iscoroutinefunction(original):
|
||||
if iscoroutinefunction(original):
|
||||
child_klass = AsyncMock
|
||||
else:
|
||||
child_klass = MagicMock
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue