typeable.py 文件源码

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

项目:speccer 作者: bensimner 项目源码 文件源码
def from_type(t):
    '''Converts a type `t` to a Typeable
    '''
    if isinstance(t, Typeable):
        return t

    if isinstance(t, list):
        if len(t) != 1:
            if len(t) < 1:
                reason = 'Missing type parameter'
            else:
                reason = 'Too many type parameters, only homogenous lists allowed'
            msg = 'Can only use literal list alias with a single type, `{}` is invalid: {}'
            raise ValueError(msg.format(repr(t), reason))
        t0 = from_type(t[0]).typ
        return from_type(typing.List[t0])
    elif isinstance(t, set):
        if len(t) != 1:
            if len(t) < 1:
                reason = 'Missing type parameter'
            else:
                reason = 'Too many type parameters, only homogenous sets allowed'
            msg = 'Can only use literal set alias with a single type, `{}` is invalid: {}'
            raise ValueError(msg.format(repr(t), reason))
        t0 = from_type(next(iter(t))).typ
        return from_type(typing.Set[t0])
    elif isinstance(t, tuple):
        args = tuple([from_type(a).typ for a in t])
        return from_type(typing.Tuple[args])

    return _from_typing36(t)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号