text
stringlengths
0
828
for typpp in sub_list:
for t in get_all_subclasses(typpp, recursive=True, _memo=_memo):
# unfortunately we have to check 't not in sub_list' because with generics strange things happen
# also is_subtype returns false when the parent is a generic
if t not in sub_list and is_subtype(t, typ, bound_typevars={}):
result.append(t)
return result"
1044,"def eval_forward_ref(typ: _ForwardRef):
""""""
Climbs the current stack until the given Forward reference has been resolved, or raises an InvalidForwardRefError
:param typ: the forward reference to resolve
:return:
""""""
for frame in stack():
m = getmodule(frame[0])
m_name = m.__name__ if m is not None else '<unknown>'
if m_name.startswith('parsyfiles.tests') or not m_name.startswith('parsyfiles'):
try:
# print(""File {}:{}"".format(frame.filename, frame.lineno))
return typ._eval_type(frame[0].f_globals, frame[0].f_locals)
except NameError:
pass
raise InvalidForwardRefError(typ)"
1045,"def is_valid_pep484_type_hint(typ_hint, allow_forward_refs: bool = False):
""""""
Returns True if the provided type is a valid PEP484 type hint, False otherwise.
Note: string type hints (forward references) are not supported by default, since callers of this function in
parsyfiles lib actually require them to be resolved already.
:param typ_hint:
:param allow_forward_refs:
:return:
""""""
# most common case first, to be faster
try:
if isinstance(typ_hint, type):
return True
except:
pass
# optionally, check forward reference
try:
if allow_forward_refs and is_forward_ref(typ_hint):
return True
except:
pass
# finally check unions and typevars
try:
return is_union_type(typ_hint) or is_typevar(typ_hint)
except:
return False"
1046,"def is_pep484_nonable(typ):
""""""
Checks if a given type is nonable, meaning that it explicitly or implicitly declares a Union with NoneType.
Nested TypeVars and Unions are supported.
:param typ:
:return:
""""""
# TODO rely on typing_inspect if there is an answer to https://github.com/ilevkivskyi/typing_inspect/issues/14
if typ is type(None):
return True
elif is_typevar(typ) or is_union_type(typ):
return any(is_pep484_nonable(tt) for tt in get_alternate_types_resolving_forwardref_union_and_typevar(typ))
else:
return False"
1047,"def _extract_collection_base_type(collection_object_type, exception_if_none: bool = True,
resolve_fwd_refs: bool = True) -> Tuple[Type, Optional[Type]]:
""""""
Utility method to extract the base item type from a collection/iterable item type.
Throws
* a TypeError if the collection_object_type a Dict with non-string keys.
* an AttributeError if the collection_object_type is actually not a collection
* a TypeInformationRequiredError if somehow the inner type can't be found from the collection type (either if dict,
list, set, tuple were used instead of their typing module equivalents (Dict, List, Set, Tuple), or if the latter
were specified without inner content types (as in Dict instead of Dict[str, Foo])
:param collection_object_type:
:return: a tuple containing the collection's content type (which may itself be a Tuple in case of a Tuple) and the
collection's content key type for dicts (or None)
""""""
contents_item_type = None
contents_key_type = None
check_var(collection_object_type, var_types=type, var_name='collection_object_type')
is_tuple = False
if is_tuple_type(collection_object_type): # Tuple is a special construct, is_generic_type does not work
is_tuple = True
# --old: hack into typing module
# if hasattr(collection_object_type, '__args__') and collection_object_type.__args__ is not None:
# contents_item_type = collection_object_type.__args__
# --new : using typing_inspect
# __args = get_last_args(collection_object_type)