text
stringlengths
0
828
:return: type.__name__ if type is not a subclass of typing.{List,Dict,Set,Tuple}, otherwise
type__name__[list of inner_types.__name__]
""""""
try:
# DO NOT resolve forward references otherwise this can lead to infinite recursion
contents_item_type, contents_key_type = _extract_collection_base_type(object_type, resolve_fwd_refs=False)
if isinstance(contents_item_type, tuple):
return object_type.__name__ + '[' \
+ ', '.join([get_pretty_type_str(item_type) for item_type in contents_item_type]) + ']'
else:
if contents_key_type is not None:
return object_type.__name__ + '[' + get_pretty_type_str(contents_key_type) + ', ' \
+ get_pretty_type_str(contents_item_type) + ']'
elif contents_item_type is not None:
return object_type.__name__ + '[' + get_pretty_type_str(contents_item_type) + ']'
except Exception as e:
pass
if is_union_type(object_type):
return 'Union[' + ', '.join([get_pretty_type_str(item_type)
for item_type in get_args(object_type, evaluate=True)]) + ']'
elif is_typevar(object_type):
# typevars usually do not display their namespace so str() is compact. And it displays the cov/contrav symbol
return str(object_type)
else:
try:
return object_type.__name__
except:
return str(object_type)"
1042,"def is_collection(object_type, strict: bool = False) -> bool:
""""""
Utility method to check if a type is a subclass of typing.{List,Dict,Set,Tuple}
or of list, dict, set, tuple.
If strict is set to True, the method will return True only if the class IS directly one of the base collection
classes
:param object_type:
:param strict: if set to True, this method will look for a strict match.
:return:
""""""
if object_type is None or object_type is Any or is_union_type(object_type) or is_typevar(object_type):
return False
elif strict:
return object_type == dict \
or object_type == list \
or object_type == tuple \
or object_type == set \
or get_base_generic_type(object_type) == Dict \
or get_base_generic_type(object_type) == List \
or get_base_generic_type(object_type) == Set \
or get_base_generic_type(object_type) == Tuple
else:
return issubclass(object_type, Dict) \
or issubclass(object_type, List) \
or issubclass(object_type, Set) \
or issubclass(object_type, Tuple) \
or issubclass(object_type, dict) \
or issubclass(object_type, list) \
or issubclass(object_type, tuple) \
or issubclass(object_type, set)"
1043,"def get_all_subclasses(typ, recursive: bool = True, _memo = None) -> Sequence[Type[Any]]:
""""""
Returns all subclasses, and supports generic types. It is recursive by default
See discussion at https://github.com/Stewori/pytypes/issues/31
:param typ:
:param recursive: a boolean indicating whether recursion is needed
:param _memo: internal variable used in recursion to avoid exploring subclasses that were already explored
:return:
""""""
_memo = _memo or set()
# if we have collected the subclasses for this already, return
if typ in _memo:
return []
# else remember that we have collected them, and collect them
_memo.add(typ)
if is_generic_type(typ):
# We now use get_origin() to also find all the concrete subclasses in case the desired type is a generic
sub_list = get_origin(typ).__subclasses__()
else:
sub_list = typ.__subclasses__()
# recurse
result = []
for t in sub_list:
# only keep the origins in the list
to = get_origin(t) or t
try:
if to is not typ and to not in result and is_subtype(to, typ, bound_typevars={}):
result.append(to)
except:
# catching an error with is_subtype(Dict, Dict[str, int], bound_typevars={})
pass
# recurse
if recursive: