Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
args_len = len(allowed.args)
if is_method:
args_len -= 1
minimum = args_len - (allowed.defaults and len(allowed.defaults) or 0)
maximum = None
if allowed.varargs is None and allowed.keywords is None:
maximum = args_len
total_positional = len(
kargs + tuple(a for a in kwargs if a in allowed.args))
named_optionals = [a for a in kwargs
if allowed.defaults
if a in allowed.args[len(allowed.args) - len(allowed.defaults):]]
if allowed.defaults and total_positional == minimum and named_optionals:
minimum += len(named_optionals)
if total_positional < minimum:
raise MethodSignatureError(
'%s requires at least %s arguments, expectation provided %s' %
(self.name, minimum, total_positional))
if maximum is not None and total_positional > maximum:
raise MethodSignatureError(
'%s requires at most %s arguments, expectation provided %s' %
(self.name, maximum, total_positional))
if args_len == len(kargs) and any(a for a in kwargs if a in allowed.args):
raise MethodSignatureError(
'%s already given as positional arguments to %s' %
([a for a in kwargs if a in allowed.args], self.name))
if (not allowed.keywords and
any(a for a in kwargs if a not in allowed.args + allowed.kwonlyargs)):
raise MethodSignatureError(
'%s is not a valid keyword argument to %s' %
([a for a in kwargs
if a not in (allowed.args + allowed.kwonlyargs)][0], self.name))
if return_value.raises:
if _isclass(return_value.raises):
raise return_value.raises(
*return_value.value['kargs'], **return_value.value['kwargs'])
else:
raise return_value.raises
else:
return return_value.value
else:
# make sure to clean up expectations to ensure none of them
# interfere with the runner's error reporing mechanism
# e.g. open()
for _, expectations in FlexmockContainer.flexmock_objects.items():
for expectation in expectations:
_getattr(expectation, 'reset')()
raise MethodSignatureError(_format_args(name, arguments))
original = _getattr(expectation, 'original')
_mock = _getattr(expectation, '_mock')
if _isclass(_mock):
if type(original) in SPECIAL_METHODS:
original = _getattr(expectation, 'original_function')
return_values = original(*kargs, **kwargs)
else:
return_values = original(runtime_self, *kargs, **kwargs)
else:
return_values = original(*kargs, **kwargs)
except:
return _handle_exception_matching(expectation)
expected_values = _getattr(expectation, 'return_values')
if (expected_values and
not match_return_values(expected_values[0].value, return_values)):
raise (MethodSignatureError('expected to return %s, returned %s' %
(expected_values[0].value, return_values)))
return return_values