stubfile_manager.py 文件源码

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

项目:pytypes 作者: Stewori 项目源码 文件源码
def _match_stub_type(stub_type):
    if not (sys.version_info.major >= 3):
        return stub_type
    # Todo: Only apply if stub-module is involved
    # Todo: Somehow cache results
    if isinstance(stub_type, TupleMeta):
        prms = pytypes.get_Tuple_params(stub_type)
        res = Tuple[(tuple(_match_stub_type(t) for t in prms))]
    elif pytypes.is_Union(stub_type):
        try:
            # Python 3.6
            res = Union[tuple(_match_stub_type(t) for t in stub_type.__args__)]
        except AttributeError:
            res = Union[tuple(_match_stub_type(t) for t in stub_type.__union_params__)]
    elif isinstance(stub_type, GenericMeta):
        if stub_type.__args__ is None:
            res = stub_type
        elif isinstance(stub_type, CallableMeta):
            if hasattr(stub_type, '__result__'):
                res = stub_type.__origin__[tuple(_match_stub_type(t) for t in stub_type.__args__)]
                res.__result__ = _match_stub_type(stub_type.__result__)
            else:
                res = stub_type.__origin__[tuple([
                        [_match_stub_type(t) for t in stub_type.__args__[:-1]],
                        _match_stub_type(stub_type.__args__[-1]) ]) ]
        else:
            res = stub_type.__origin__[tuple(_match_stub_type(t) for t in stub_type.__args__)]
    elif isclass(stub_type):
        res = stub_type._match_type if hasattr(stub_type, '_match_type') else stub_type
    else:
        res = stub_type
    return res
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号