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
评论列表
文章目录