Upload type_utils.py with huggingface_hub
Browse files2 unmodified lines34567899101110111213141513 unmodified lines293031313232333435365 unmodified lines42434444454647484996 unmodified lines14614714814714915015115235 unmodified lines18818919018919119219319419319419519519619719810 unmodified lines2092102112122122132142142152162178 unmodified lines22622722822922923023123123223323431 unmodified lines26626726826926927027127127227327411 unmodified lines28628728828929029129229328929029129229 unmodified lines3223233243293303313253263333343353363373383273283293303413423433443453463313323333341 unmodified line3363373383543553563393403413428 unmodified lines3513523533713543553568 unmodified lines3653663673863873883683693903913923703713723953963973733743753761 unmodified line37837938040540640738138238338415 unmodified lines4004014024294034044054064074082 unmodified linesimport itertoolsimport typing
def isoftype(object, type): """ Checks if an object is of a certain typing type, including nested types. This function supports simple types (like `int`, `str`), typing types (like `List[int]`, `Tuple[str, int]`, `Dict[str, int]`), and nested typing
This function supports simple types (like `int`, `str`), typing types (like `List[int]`, `Tuple[str, int]`, `Dict[str, int]`), and nested typing types (like `List[List[int]]`, `Tuple[List[str], int]`, `Dict[str, List[int]]`).
Args:13 unmodified lines >>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]]) True """ if hasattr(type, '__origin__'):
if hasattr(type, "__origin__"): origin = type.__origin__ type_args = typing.get_args(type)
5 unmodified lines return all(isoftype(element, type_arg) for element, type_arg in zip(object, type_args)) else: return isinstance(object, type)
# copied from: https://github.com/bojiang/typing_utils/blob/main/typing_utils/__init__.py# liscened under Apache License 2.0
96 unmodified lines get_origin(List[Tuple[T, T]][int]) == list ``` """ if hasattr(typing, 'get_origin'): # python 3.8+ if hasattr(typing, "get_origin"): # python 3.8+ _getter = getattr(typing, "get_origin") ori = _getter(type_) elif hasattr(typing.List, "_special"): # python 3.735 unmodified lines get_args(Callable[[], T][int]) == ([], int) ``` """ if hasattr(typing, 'get_args'): # python 3.8+ if hasattr(typing, "get_args"): # python 3.8+ _getter = getattr(typing, "get_args") res = _getter(type_) elif hasattr(typing.List, "_special"): # python 3.7 if ( isinstance(type_, GenericClass) and not type_._special ): # backport for python 3.8 if isinstance(type_, GenericClass) and not type_._special: # backport for python 3.8 res = type_.__args__ if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis: res = (list(res[:-1]), res[-1])10 unmodified lines
def eval_forward_ref(ref, forward_refs=None): ''' """ eval forward_refs in all cPython versions ''' """ localns = forward_refs or {}
if hasattr(typing, "_eval_type"): # python3.8 & python 3.98 unmodified lines
class NormalizedType(typing.NamedTuple): ''' """ Normalized type, made it possible to compare, hash between types. ''' """
origin: Type args: typing.Union[tuple, frozenset] = tuple()31 unmodified lines
def normalize(type_: Type) -> NormalizedType: ''' """ convert types to NormalizedType instances. ''' """ args = get_args(type_) origin = get_origin(type_) if not origin:11 unmodified lines if left is right: return True
if ( left is not None and left in STATIC_SUBTYPE_MAPPING and right == STATIC_SUBTYPE_MAPPING[left] ): if left is not None and left in STATIC_SUBTYPE_MAPPING and right == STATIC_SUBTYPE_MAPPING[left]: return True
if hasattr(left, "mro"):29 unmodified lines return True
# Union[list, int] <> Union[typing.Sequence, int] return all( any(_is_normal_subtype(e, r, forward_refs) for r in right) for e in excluded ) return all(any(_is_normal_subtype(e, r, forward_refs) for r in right) for e in excluded)
if isinstance(left, collections.abc.Sequence) and not isinstance( left, NormalizedType ): if not isinstance(right, collections.abc.Sequence) or isinstance( right, NormalizedType ): if isinstance(left, collections.abc.Sequence) and not isinstance(left, NormalizedType): if not isinstance(right, collections.abc.Sequence) or isinstance(right, NormalizedType): return False
if ( left and left[-1].origin is not Ellipsis and right and right[-1].origin is Ellipsis ): if left and left[-1].origin is not Ellipsis and right and right[-1].origin is Ellipsis: # Tuple[type, type] <> Tuple[type, ...] return all(_is_origin_subtype_args(l, right[0], forward_refs) for l in left)
1 unmodified line return False
return all( l is not None and r is not None and _is_origin_subtype_args(l, r, forward_refs) l is not None and r is not None and _is_origin_subtype_args(l, r, forward_refs) for l, r in itertools.zip_longest(left, right) )
8 unmodified lines right: NormalizedType, forward_refs: typing.Optional[typing.Mapping[str, type]],) -> typing.Optional[bool]:
if isinstance(left.origin, ForwardRef): left = normalize(eval_forward_ref(left.origin, forward_refs=forward_refs))
8 unmodified lines if right.origin is typing.Union and left.origin is typing.Union: return _is_origin_subtype_args(left.args, right.args, forward_refs) if right.origin is typing.Union: return optional_any( _is_normal_subtype(left, a, forward_refs) for a in right.args ) return optional_any(_is_normal_subtype(left, a, forward_refs) for a in right.args) if left.origin is typing.Union: return optional_all( _is_normal_subtype(a, right, forward_refs) for a in left.args ) return optional_all(_is_normal_subtype(a, right, forward_refs) for a in left.args)
# TypeVar if isinstance(left.origin, typing.TypeVar) and isinstance( right.origin, typing.TypeVar ): if isinstance(left.origin, typing.TypeVar) and isinstance(right.origin, typing.TypeVar): if left.origin is right.origin: return True
1 unmodified line right_bound = getattr(right.origin, "__bound__", None) if right_bound is None or left_bound is None: return unknown return _is_normal_subtype( normalize(left_bound), normalize(right_bound), forward_refs ) return _is_normal_subtype(normalize(left_bound), normalize(right_bound), forward_refs) if isinstance(right.origin, typing.TypeVar): return unknown if isinstance(left.origin, typing.TypeVar):15 unmodified lines
def issubtype( left: Type, right: Type, forward_refs: typing.Optional[dict] = None, left: Type, right: Type, forward_refs: typing.Optional[dict] = None,) -> typing.Optional[bool]: """Check that the left argument is a subtype of the right. For unions, check if the type arguments of the left is a subset of the right.type_utils.py+30-54