Upload type_utils.py with huggingface_hub
Browse files18 unmodified lines19202122232425262728292223242526272829303132333435363738394041424344454647486 unmodified lines555657495058596087 unmodified lines14814915014414514614714814915015115215315415115215315415515615715815916016116216316416516616716827 unmodified lines196197198188189190191192193194195196199200201202203204205206207208209210211212213240 unmodified lines45445545644344444544644744844945045145245345445545645745845946046146245745845946046146246346446546646746846947047147247347447547647747847948048148248318 unmodified lines bool: 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 .. highlight:: python .. code-block:: python
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 type == typing.Any: return True
if 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 False
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 None
if type == typing.Any: return True return isinstance(object, type)
87 unmodified lines Return None for unsupported types.
Examples: ```python from typing_utils import get_origin
get_origin(Literal[42]) is Literal get_origin(int) is None get_origin(ClassVar[int]) is ClassVar get_origin(Generic) is Generic get_origin(Generic[T]) is Generic get_origin(Union[T, int]) is Union get_origin(List[Tuple[T, T]][int]) == list ``` Here are some code examples using `get_origin` from the `typing_utils` module:
.. code-block:: python
from typing_utils import get_origin
# Examples of get_origin usage get_origin(Literal[42]) is Literal # True get_origin(int) is None # True get_origin(ClassVar[int]) is ClassVar # True get_origin(Generic) is Generic # True get_origin(Generic[T]) is Generic # True get_origin(Union[T, int]) is Union # True get_origin(List[Tuple[T, T]][int]) == list # True
""" if hasattr(typing, "get_origin"): # python 3.8+ _getter = typing.get_origin27 unmodified lines For unions, basic simplifications used by Union constructor are performed.
Examples: ```python from typing_utils import get_args
get_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) ``` Here are some code examples using `get_args` from the `typing_utils` module:
.. code-block:: python
from typing_utils import get_args
# Examples of get_args usage get_args(Dict[str, int]) == (str, int) # True get_args(int) == () # True get_args(Union[int, Union[T, int], str][int]) == (int, str) # True get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) # True get_args(Callable[[], T][int]) == ([], int) # True """ if hasattr(typing, "get_args"): # python 3.8+ _getter = typing.get_args240 unmodified lines Also works for nested types including ForwardRefs.
Examples: ```python from typing_utils import issubtype
issubtype(typing.List, typing.Any) == True issubtype(list, list) == True issubtype(list, typing.List) == True issubtype(list, typing.Sequence) == True issubtype(typing.List[int], list) == True issubtype(typing.List[typing.List], list) == True issubtype(list, typing.List[int]) == False issubtype(list, typing.Union[typing.Tuple, typing.Set]) == False issubtype(typing.List[typing.List], typing.List[typing.Sequence]) == True JSON = typing.Union[ int, float, bool, str, None, typing.Sequence["JSON"], typing.Mapping[str, "JSON"] ] issubtype(str, JSON, forward_refs={'JSON': JSON}) == True issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) == True issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) == False ``` Here are some code examples using `issubtype` from the `typing_utils` module:
.. code-block:: python
from typing_utils import issubtype
# Examples of issubtype checks issubtype(typing.List, typing.Any) # True issubtype(list, list) # True issubtype(list, typing.List) # True issubtype(list, typing.Sequence) # True issubtype(typing.List[int], list) # True issubtype(typing.List[typing.List], list) # True issubtype(list, typing.List[int]) # False issubtype(list, typing.Union[typing.Tuple, typing.Set]) # False issubtype(typing.List[typing.List], typing.List[typing.Sequence]) # True
# Example with custom JSON type JSON = typing.Union[ int, float, bool, str, None, typing.Sequence["JSON"], typing.Mapping[str, "JSON"] ] issubtype(str, JSON, forward_refs={'JSON': JSON}) # True issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) # True issubtype(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