|
|
|
@ -106,7 +106,7 @@ def _check_signature(func, mock, skipfirst, instance=False): |
|
|
|
if sig is None: |
|
|
|
return |
|
|
|
func, sig = sig |
|
|
|
def checksig(_mock_self, *args, **kwargs): |
|
|
|
def checksig(self, /, *args, **kwargs): |
|
|
|
sig.bind(*args, **kwargs) |
|
|
|
_copy_func_details(func, checksig) |
|
|
|
type(mock)._mock_check_sig = checksig |
|
|
|
@ -243,7 +243,7 @@ def _setup_async_mock(mock): |
|
|
|
# Mock is not configured yet so the attributes are set |
|
|
|
# to a function and then the corresponding mock helper function |
|
|
|
# is called when the helper is accessed similar to _setup_func. |
|
|
|
def wrapper(attr, *args, **kwargs): |
|
|
|
def wrapper(attr, /, *args, **kwargs): |
|
|
|
return getattr(mock.mock, attr)(*args, **kwargs) |
|
|
|
|
|
|
|
for attribute in ('assert_awaited', |
|
|
|
@ -387,7 +387,7 @@ class _MockIter(object): |
|
|
|
class Base(object): |
|
|
|
_mock_return_value = DEFAULT |
|
|
|
_mock_side_effect = None |
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
def __init__(self, /, *args, **kwargs): |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
@ -395,7 +395,7 @@ class Base(object): |
|
|
|
class NonCallableMock(Base): |
|
|
|
"""A non-callable version of `Mock`""" |
|
|
|
|
|
|
|
def __new__(cls, *args, **kw): |
|
|
|
def __new__(cls, /, *args, **kw): |
|
|
|
# every instance has its own class |
|
|
|
# so we can create magic methods on the |
|
|
|
# class without stomping on other mocks |
|
|
|
@ -602,7 +602,7 @@ class NonCallableMock(Base): |
|
|
|
ret.reset_mock(visited) |
|
|
|
|
|
|
|
|
|
|
|
def configure_mock(self, **kwargs): |
|
|
|
def configure_mock(self, /, **kwargs): |
|
|
|
"""Set attributes on the mock through keyword arguments. |
|
|
|
|
|
|
|
Attributes plus return values and side effects can be set on child |
|
|
|
@ -820,10 +820,9 @@ class NonCallableMock(Base): |
|
|
|
else: |
|
|
|
return _call |
|
|
|
|
|
|
|
def assert_not_called(_mock_self): |
|
|
|
def assert_not_called(self): |
|
|
|
"""assert that the mock was never called. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if self.call_count != 0: |
|
|
|
msg = ("Expected '%s' to not have been called. Called %s times.%s" |
|
|
|
% (self._mock_name or 'mock', |
|
|
|
@ -831,19 +830,17 @@ class NonCallableMock(Base): |
|
|
|
self._calls_repr())) |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def assert_called(_mock_self): |
|
|
|
def assert_called(self): |
|
|
|
"""assert that the mock was called at least once |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if self.call_count == 0: |
|
|
|
msg = ("Expected '%s' to have been called." % |
|
|
|
self._mock_name or 'mock') |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def assert_called_once(_mock_self): |
|
|
|
def assert_called_once(self): |
|
|
|
"""assert that the mock was called only once. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if not self.call_count == 1: |
|
|
|
msg = ("Expected '%s' to have been called once. Called %s times.%s" |
|
|
|
% (self._mock_name or 'mock', |
|
|
|
@ -851,12 +848,11 @@ class NonCallableMock(Base): |
|
|
|
self._calls_repr())) |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def assert_called_with(_mock_self, *args, **kwargs): |
|
|
|
def assert_called_with(self, /, *args, **kwargs): |
|
|
|
"""assert that the mock was called with the specified arguments. |
|
|
|
|
|
|
|
Raises an AssertionError if the args and keyword args passed in are |
|
|
|
different to the last call to the mock.""" |
|
|
|
self = _mock_self |
|
|
|
if self.call_args is None: |
|
|
|
expected = self._format_mock_call_signature(args, kwargs) |
|
|
|
actual = 'not called.' |
|
|
|
@ -874,10 +870,9 @@ class NonCallableMock(Base): |
|
|
|
raise AssertionError(_error_message()) from cause |
|
|
|
|
|
|
|
|
|
|
|
def assert_called_once_with(_mock_self, *args, **kwargs): |
|
|
|
def assert_called_once_with(self, /, *args, **kwargs): |
|
|
|
"""assert that the mock was called exactly once and that that call was |
|
|
|
with the specified arguments.""" |
|
|
|
self = _mock_self |
|
|
|
if not self.call_count == 1: |
|
|
|
msg = ("Expected '%s' to be called once. Called %s times.%s" |
|
|
|
% (self._mock_name or 'mock', |
|
|
|
@ -924,7 +919,7 @@ class NonCallableMock(Base): |
|
|
|
) from cause |
|
|
|
|
|
|
|
|
|
|
|
def assert_any_call(self, *args, **kwargs): |
|
|
|
def assert_any_call(self, /, *args, **kwargs): |
|
|
|
"""assert the mock has been called with the specified arguments. |
|
|
|
|
|
|
|
The assert passes if the mock has *ever* been called, unlike |
|
|
|
@ -940,7 +935,7 @@ class NonCallableMock(Base): |
|
|
|
) from cause |
|
|
|
|
|
|
|
|
|
|
|
def _get_child_mock(self, **kw): |
|
|
|
def _get_child_mock(self, /, **kw): |
|
|
|
"""Create the child mocks for attributes and return value. |
|
|
|
By default child mocks will be the same type as the parent. |
|
|
|
Subclasses of Mock may want to override this to customize the way |
|
|
|
@ -1016,20 +1011,19 @@ class CallableMixin(Base): |
|
|
|
self.side_effect = side_effect |
|
|
|
|
|
|
|
|
|
|
|
def _mock_check_sig(self, *args, **kwargs): |
|
|
|
def _mock_check_sig(self, /, *args, **kwargs): |
|
|
|
# stub method that can be replaced with one with a specific signature |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def __call__(_mock_self, *args, **kwargs): |
|
|
|
def __call__(self, /, *args, **kwargs): |
|
|
|
# can't use self in-case a function / method we are mocking uses self |
|
|
|
# in the signature |
|
|
|
_mock_self._mock_check_sig(*args, **kwargs) |
|
|
|
return _mock_self._mock_call(*args, **kwargs) |
|
|
|
self._mock_check_sig(*args, **kwargs) |
|
|
|
return self._mock_call(*args, **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
def _mock_call(_mock_self, *args, **kwargs): |
|
|
|
self = _mock_self |
|
|
|
def _mock_call(self, /, *args, **kwargs): |
|
|
|
self.called = True |
|
|
|
self.call_count += 1 |
|
|
|
|
|
|
|
@ -1840,7 +1834,7 @@ _non_defaults = { |
|
|
|
|
|
|
|
def _get_method(name, func): |
|
|
|
"Turns a callable object (like a mock) into a real function" |
|
|
|
def method(self, *args, **kw): |
|
|
|
def method(self, /, *args, **kw): |
|
|
|
return func(self, *args, **kw) |
|
|
|
method.__name__ = name |
|
|
|
return method |
|
|
|
@ -1954,7 +1948,7 @@ def _set_return_value(mock, method, name): |
|
|
|
|
|
|
|
|
|
|
|
class MagicMixin(object): |
|
|
|
def __init__(self, *args, **kw): |
|
|
|
def __init__(self, /, *args, **kw): |
|
|
|
self._mock_set_magics() # make magic work for kwargs in init |
|
|
|
_safe_super(MagicMixin, self).__init__(*args, **kw) |
|
|
|
self._mock_set_magics() # fix magic broken by upper level init |
|
|
|
@ -1996,7 +1990,7 @@ class NonCallableMagicMock(MagicMixin, NonCallableMock): |
|
|
|
|
|
|
|
|
|
|
|
class AsyncMagicMixin: |
|
|
|
def __init__(self, *args, **kw): |
|
|
|
def __init__(self, /, *args, **kw): |
|
|
|
self._mock_set_async_magics() # make magic work for kwargs in init |
|
|
|
_safe_super(AsyncMagicMixin, self).__init__(*args, **kw) |
|
|
|
self._mock_set_async_magics() # fix magic broken by upper level init |
|
|
|
@ -2067,7 +2061,7 @@ class AsyncMockMixin(Base): |
|
|
|
await_args = _delegating_property('await_args') |
|
|
|
await_args_list = _delegating_property('await_args_list') |
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
def __init__(self, /, *args, **kwargs): |
|
|
|
super().__init__(*args, **kwargs) |
|
|
|
# asyncio.iscoroutinefunction() checks _is_coroutine property to say if an |
|
|
|
# object is a coroutine. Without this check it looks to see if it is a |
|
|
|
@ -2084,8 +2078,7 @@ class AsyncMockMixin(Base): |
|
|
|
code_mock.co_flags = inspect.CO_COROUTINE |
|
|
|
self.__dict__['__code__'] = code_mock |
|
|
|
|
|
|
|
async def _mock_call(_mock_self, *args, **kwargs): |
|
|
|
self = _mock_self |
|
|
|
async def _mock_call(self, /, *args, **kwargs): |
|
|
|
try: |
|
|
|
result = super()._mock_call(*args, **kwargs) |
|
|
|
except (BaseException, StopIteration) as e: |
|
|
|
@ -2110,30 +2103,27 @@ class AsyncMockMixin(Base): |
|
|
|
|
|
|
|
return await proxy() |
|
|
|
|
|
|
|
def assert_awaited(_mock_self): |
|
|
|
def assert_awaited(self): |
|
|
|
""" |
|
|
|
Assert that the mock was awaited at least once. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if self.await_count == 0: |
|
|
|
msg = f"Expected {self._mock_name or 'mock'} to have been awaited." |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def assert_awaited_once(_mock_self): |
|
|
|
def assert_awaited_once(self): |
|
|
|
""" |
|
|
|
Assert that the mock was awaited exactly once. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if not self.await_count == 1: |
|
|
|
msg = (f"Expected {self._mock_name or 'mock'} to have been awaited once." |
|
|
|
f" Awaited {self.await_count} times.") |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def assert_awaited_with(_mock_self, *args, **kwargs): |
|
|
|
def assert_awaited_with(self, /, *args, **kwargs): |
|
|
|
""" |
|
|
|
Assert that the last await was with the specified arguments. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if self.await_args is None: |
|
|
|
expected = self._format_mock_call_signature(args, kwargs) |
|
|
|
raise AssertionError(f'Expected await: {expected}\nNot awaited') |
|
|
|
@ -2148,23 +2138,21 @@ class AsyncMockMixin(Base): |
|
|
|
cause = expected if isinstance(expected, Exception) else None |
|
|
|
raise AssertionError(_error_message()) from cause |
|
|
|
|
|
|
|
def assert_awaited_once_with(_mock_self, *args, **kwargs): |
|
|
|
def assert_awaited_once_with(self, /, *args, **kwargs): |
|
|
|
""" |
|
|
|
Assert that the mock was awaited exactly once and with the specified |
|
|
|
arguments. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if not self.await_count == 1: |
|
|
|
msg = (f"Expected {self._mock_name or 'mock'} to have been awaited once." |
|
|
|
f" Awaited {self.await_count} times.") |
|
|
|
raise AssertionError(msg) |
|
|
|
return self.assert_awaited_with(*args, **kwargs) |
|
|
|
|
|
|
|
def assert_any_await(_mock_self, *args, **kwargs): |
|
|
|
def assert_any_await(self, /, *args, **kwargs): |
|
|
|
""" |
|
|
|
Assert the mock has ever been awaited with the specified arguments. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
expected = self._call_matcher((args, kwargs)) |
|
|
|
actual = [self._call_matcher(c) for c in self.await_args_list] |
|
|
|
if expected not in actual: |
|
|
|
@ -2174,7 +2162,7 @@ class AsyncMockMixin(Base): |
|
|
|
'%s await not found' % expected_string |
|
|
|
) from cause |
|
|
|
|
|
|
|
def assert_has_awaits(_mock_self, calls, any_order=False): |
|
|
|
def assert_has_awaits(self, calls, any_order=False): |
|
|
|
""" |
|
|
|
Assert the mock has been awaited with the specified calls. |
|
|
|
The :attr:`await_args_list` list is checked for the awaits. |
|
|
|
@ -2186,7 +2174,6 @@ class AsyncMockMixin(Base): |
|
|
|
If `any_order` is True then the awaits can be in any order, but |
|
|
|
they must all appear in :attr:`await_args_list`. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
expected = [self._call_matcher(c) for c in calls] |
|
|
|
cause = expected if isinstance(expected, Exception) else None |
|
|
|
all_awaits = _CallList(self._call_matcher(c) for c in self.await_args_list) |
|
|
|
@ -2211,17 +2198,16 @@ class AsyncMockMixin(Base): |
|
|
|
'%r not all found in await list' % (tuple(not_found),) |
|
|
|
) from cause |
|
|
|
|
|
|
|
def assert_not_awaited(_mock_self): |
|
|
|
def assert_not_awaited(self): |
|
|
|
""" |
|
|
|
Assert that the mock was never awaited. |
|
|
|
""" |
|
|
|
self = _mock_self |
|
|
|
if self.await_count != 0: |
|
|
|
msg = (f"Expected {self._mock_name or 'mock'} to not have been awaited." |
|
|
|
f" Awaited {self.await_count} times.") |
|
|
|
raise AssertionError(msg) |
|
|
|
|
|
|
|
def reset_mock(self, *args, **kwargs): |
|
|
|
def reset_mock(self, /, *args, **kwargs): |
|
|
|
""" |
|
|
|
See :func:`.Mock.reset_mock()` |
|
|
|
""" |
|
|
|
@ -2424,7 +2410,7 @@ class _Call(tuple): |
|
|
|
__ne__ = object.__ne__ |
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs): |
|
|
|
def __call__(self, /, *args, **kwargs): |
|
|
|
if self._mock_name is None: |
|
|
|
return _Call(('', args, kwargs), name='()') |
|
|
|
|
|
|
|
@ -2439,10 +2425,10 @@ class _Call(tuple): |
|
|
|
return _Call(name=name, parent=self, from_kall=False) |
|
|
|
|
|
|
|
|
|
|
|
def count(self, *args, **kwargs): |
|
|
|
def count(self, /, *args, **kwargs): |
|
|
|
return self.__getattr__('count')(*args, **kwargs) |
|
|
|
|
|
|
|
def index(self, *args, **kwargs): |
|
|
|
def index(self, /, *args, **kwargs): |
|
|
|
return self.__getattr__('index')(*args, **kwargs) |
|
|
|
|
|
|
|
def _get_call_arguments(self): |
|
|
|
@ -2778,7 +2764,7 @@ class PropertyMock(Mock): |
|
|
|
Fetching a `PropertyMock` instance from an object calls the mock, with |
|
|
|
no args. Setting it calls the mock with the value being set. |
|
|
|
""" |
|
|
|
def _get_child_mock(self, **kwargs): |
|
|
|
def _get_child_mock(self, /, **kwargs): |
|
|
|
return MagicMock(**kwargs) |
|
|
|
|
|
|
|
def __get__(self, obj, obj_type): |
|
|
|
|