Upload type_utils.py with huggingface_hub
Browse files18 unmodified lines1920212223242526272829303132333435363738396 unmodified lines464748495051525387 unmodified lines14114214314414514614714814915015115215315415515615727 unmodified lines185186187188189190191192193194195196197198199240 unmodified lines44044144244344444544644744844945045145245345445545645745845946046146246346418 unmodified linesbool: True if the object is of the specified type, False otherwise.Examples:>>> isoftype(1, int)True>>> isoftype([1, 2, 3], typing.List[int])True>>> isoftype([1, 2, 3], typing.List[str])False>>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]])True"""if hasattr(type, "__origin__"):origin = type.__origin__type_args = typing.get_args(type)if origin is list or origin is set:return all(isoftype(element, type_args[0]) for element in object)if origin is dict:return all(isoftype(key, type_args[0]) and isoftype(value, type_args[1])6 unmodified lines)return Noneif type == typing.Any:return Truereturn isinstance(object, type)87 unmodified linesReturn None for unsupported types.Examples:```pythonfrom typing_utils import get_originget_origin(Literal[42]) is Literalget_origin(int) is Noneget_origin(ClassVar[int]) is ClassVarget_origin(Generic) is Genericget_origin(Generic[T]) is Genericget_origin(Union[T, int]) is Unionget_origin(List[Tuple[T, T]][int]) == list```"""if hasattr(typing, "get_origin"): # python 3.8+_getter = typing.get_origin27 unmodified linesFor unions, basic simplifications used by Union constructor are performed.Examples:```pythonfrom typing_utils import get_argsget_args(Dict[str, int]) == (str, int)get_args(int) == ()get_args(Union[int, Union[T, int], str][int]) == (int, str)get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])get_args(Callable[[], T][int]) == ([], int)```"""if hasattr(typing, "get_args"): # python 3.8+_getter = typing.get_args240 unmodified linesAlso works for nested types including ForwardRefs.Examples:```pythonfrom typing_utils import issubtypeissubtype(typing.List, typing.Any) == Trueissubtype(list, list) == Trueissubtype(list, typing.List) == Trueissubtype(list, typing.Sequence) == Trueissubtype(typing.List[int], list) == Trueissubtype(typing.List[typing.List], list) == Trueissubtype(list, typing.List[int]) == Falseissubtype(list, typing.Union[typing.Tuple, typing.Set]) == Falseissubtype(typing.List[typing.List], typing.List[typing.Sequence]) == TrueJSON = typing.Union[int, float, bool, str, None, typing.Sequence["JSON"],typing.Mapping[str, "JSON"]]issubtype(str, JSON, forward_refs={'JSON': JSON}) == Trueissubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) == Trueissubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) == False```"""return _is_normal_subtype(normalize(left), normalize(right), forward_refs)18 unmodified lines1920212223242526272829303132333435363738394041424344454647486 unmodified lines55565758596087 unmodified lines14814915015115215315415515615715815916016116216316416516616716827 unmodified lines196197198199200201202203204205206207208209210211212213240 unmodified lines45445545645745845946046146246346446546646746846947047147247347447547647747847948048148248318 unmodified linesbool: True if the object is of the specified type, False otherwise.Examples:.. highlight:: python.. code-block:: pythonisoftype(1, int) # Trueisoftype([1, 2, 3], typing.List[int]) # Trueisoftype([1, 2, 3], typing.List[str]) # Falseisoftype([[1, 2], [3, 4]], typing.List[typing.List[int]]) # True"""if type == typing.Any:return Trueif hasattr(type, "__origin__"):origin = type.__origin__type_args = typing.get_args(type)if origin is typing.Union:return any(isoftype(object, sub_type) for sub_type in type_args)if not isinstance(object, origin):return Falseif origin is list or origin is set:return all(isoftype(element, type_args[0]) for element in object)if origin is dict:return all(isoftype(key, type_args[0]) and isoftype(value, type_args[1])6 unmodified lines)return Nonereturn isinstance(object, type)87 unmodified linesReturn None for unsupported types.Examples:Here are some code examples using `get_origin` from the `typing_utils` module:.. code-block:: pythonfrom typing_utils import get_origin# Examples of get_origin usageget_origin(Literal[42]) is Literal # Trueget_origin(int) is None # Trueget_origin(ClassVar[int]) is ClassVar # Trueget_origin(Generic) is Generic # Trueget_origin(Generic[T]) is Generic # Trueget_origin(Union[T, int]) is Union # Trueget_origin(List[Tuple[T, T]][int]) == list # True"""if hasattr(typing, "get_origin"): # python 3.8+_getter = typing.get_origin27 unmodified linesFor unions, basic simplifications used by Union constructor are performed.Examples:Here are some code examples using `get_args` from the `typing_utils` module:.. code-block:: pythonfrom typing_utils import get_args# Examples of get_args usageget_args(Dict[str, int]) == (str, int) # Trueget_args(int) == () # Trueget_args(Union[int, Union[T, int], str][int]) == (int, str) # Trueget_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) # Trueget_args(Callable[[], T][int]) == ([], int) # True"""if hasattr(typing, "get_args"): # python 3.8+_getter = typing.get_args240 unmodified linesAlso works for nested types including ForwardRefs.Examples:Here are some code examples using `issubtype` from the `typing_utils` module:.. code-block:: pythonfrom typing_utils import issubtype# Examples of issubtype checksissubtype(typing.List, typing.Any) # Trueissubtype(list, list) # Trueissubtype(list, typing.List) # Trueissubtype(list, typing.Sequence) # Trueissubtype(typing.List[int], list) # Trueissubtype(typing.List[typing.List], list) # Trueissubtype(list, typing.List[int]) # Falseissubtype(list, typing.Union[typing.Tuple, typing.Set]) # Falseissubtype(typing.List[typing.List], typing.List[typing.Sequence]) # True# Example with custom JSON typeJSON = typing.Union[int, float, bool, str, None, typing.Sequence["JSON"],typing.Mapping[str, "JSON"]]issubtype(str, JSON, forward_refs={'JSON': JSON}) # Trueissubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) # Trueissubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) # False"""return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
type_utils.py+69-50