typed.py 文件源码

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

项目:tsukkomi 作者: spoqa 项目源码 文件源码
def check_type(value: typing.Any, hint: typing.Optional[type]) -> bool:
    """Check given ``value``'s type.

    :param value: given argument
    :param hint: expected type of given ``value``.
                 as like :mod:`typing` interprets, :const:`None` is interpreted
                 as :class:`types.NoneType`
    :type hint: :class:`typing.Optional`[:class:`type`]

    """
    if hint is None:
        hint = NoneType
    actual_type = type(value)
    if hint is NoneType:
        correct = value is None
    elif hint is typing.Any:
        correct = True
    elif hint is typing.Pattern or hint is typing.Match:
        correct = isinstance(value, hint.impl_type)
    elif isinstance(hint, typing.TypeVar):
        # TODO: Check generic
        correct = True
    elif issubclass(hint, typing.Callable):
        actual_type, correct = check_callable(value, hint)
    elif issubclass(hint, typing.Tuple):
        actual_type, correct = check_tuple(value, hint)
    elif issubclass(hint, typing.Union):
        actual_type, correct = check_union(value, hint)
    else:
        correct = isinstance(value, hint)
    return actual_type, correct
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号