typing.py 文件源码

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

项目:MonkeyType 作者: Instagram 项目源码 文件源码
def get_type(obj):
    """Return the static type that would be used in a type hint"""
    if isinstance(obj, type):
        return Type[obj]
    elif isinstance(obj, _BUILTIN_CALLABLE_TYPES):
        return Callable
    elif isinstance(obj, types.GeneratorType):
        return Iterator[Any]
    typ = type(obj)
    if typ is list:
        elem_type = shrink_types(get_type(e) for e in obj)
        return List[elem_type]
    elif typ is set:
        elem_type = shrink_types(get_type(e) for e in obj)
        return Set[elem_type]
    elif typ is dict:
        key_type = shrink_types(get_type(k) for k in obj.keys())
        val_type = shrink_types(get_type(v) for v in obj.values())
        return Dict[key_type, val_type]
    elif typ is tuple:
        if not obj:
            return Tuple
        return Tuple[tuple(get_type(e) for e in obj)]
    return typ
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号