typed.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:tsukkomi 作者: spoqa 项目源码 文件源码
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
                )
            )
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号