typing_to_base¶
-
privex.helpers.common.typing_to_base(tp, fail=False, return_orig=True, clean_union=True) → Optional[Union[type, object, callable, tuple, Tuple[type]]][source]¶ Attempt to extract one or more native Python base types from a
typingtype, including generics such asList[str], and combined types such asUnion[list, str]>>> typing_to_base(List[str]) list >>> typing_to_base(Union[str, Dict[str, list], int]) (str, dict, int) >>> typing_to_base(Union[str, Dict[str, list], int], clean_union=False) (str, typing.Dict[str, list], int) >>> typing_to_base(str) str >>> typing_to_base(str, fail=True) TypeError: Failed to extract base type for type object: <class 'str'> >>> repr(typing_to_base(str, return_orig=False)) 'None'
- Parameters
tp – The
typingtype object to extract base/native type(s) from.fail (bool) – (Default:
False) If True, then raisesTypeErroriftpdoesn’t appear to be atypingtype.return_orig (bool) – (Default:
True) If True, returnstpas-is if it’s not a typing type. WhenFalse, non-typingtypes will causeNoneto be returned.clean_union (bool) – (Default:
True) If True,typing.Union’s will have each type converted/validated into a normal type usingextract_type()
- Return type_res
Either a
typebase type, atupleof types, atypingtype object, or something else depending on what typetpwas.