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