Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def raises_TypeCheckError(self):
with self.assertRaises(TypeCheckError):
self.callable_target(
*self.call_args, **self.call_kwargs
)
inner_expected_type,
*args,
globals=globals,
locals=locals,
**kwargs,
)
with unittest.mock.patch.object(
typeguard, "check_type", new=wrapped_check_type
), unittest.mock.patch.object(
typeguard, "qualified_name", new=wrapped_qualified_name
):
try:
typeguard.check_type(name, value, expected_type)
except TypeError as type_error:
raise TypeCheckError(str(type_error))
def it_fails_with_invalid_types(self):
with self.assertRaises(TypeCheckError):
self.target(message=1234)
def raises_TypeCheckError_for_invalid_types(self):
bad_signature_args = (1234 for arg in self.call_args)
bad_signature_kargs = {
k: 1234 for k, v in self.call_kwargs.items()
}
with self.assertRaises(TypeCheckError):
self.callable_target(
*bad_signature_args, **bad_signature_kargs
)
_validate_argument_type(expected_type, argname, args[idx])
except TypeCheckError as type_error:
type_errors.append(f"{repr(argname)}: {type_error}")
for argname, value in kwargs.items():
try:
expected_type = argspec.annotations.get(argname)
if not expected_type:
continue
_validate_argument_type(expected_type, argname, value)
except TypeCheckError as type_error:
type_errors.append(f"{repr(argname)}: {type_error}")
if type_errors:
raise TypeCheckError(
"Call to "
+ callable_template.__name__
+ " has incompatible argument types:\n "
+ "\n ".join(type_errors)
)
def assert_fails(self, value):
assert_regex = (
r"(?s)type of return must be .+; got .+ instead: .+Defined at .+:\d+"
)
with self.assertRaisesRegex(testslide.lib.TypeCheckError, assert_regex):
testslide.lib._validate_return_type(
self.callable_template, value, self.caller_frame_info
)
def raises_TypeCheckError_when_setting_with_mock_with_invalid_type_template(
self,
):
with self.assertRaises(TypeCheckError):
self.strict_mock.non_callable = unittest.mock.Mock(
spec=int
)
with self.assertRaises(TypeCheckError):
self.strict_mock.non_callable = StrictMock(template=int)