def check_arguments(c: typing.Callable,
hints: typing.Mapping[str, typing.Optional[type]],
*args, **kwargs) -> None:
"""Check arguments type, raise :class:`TypeError` if argument type is not
expected type.
:param c: callable object want to check types
:param hints: assumed type of given ``c`` result of
:func:`typing.get_type_hints`
"""
signature = inspect.signature(c)
bound = signature.bind(*args, **kwargs)
for argument_name, value in bound.arguments.items():
try:
type_hint = hints[argument_name]
except KeyError:
continue
actual_type, correct = check_type(value, type_hint)
if not correct:
raise TypeError(
'Incorrect type `{}`, expected `{}` for `{}`'.format(
actual_type, type_hint, argument_name
)
)
评论列表
文章目录