Upload type_utils.py with huggingface_hub
Browse files- type_utils.py +30 -54
type_utils.py
CHANGED
@@ -3,12 +3,13 @@ import io
|
|
3 |
import itertools
|
4 |
import typing
|
5 |
|
|
|
6 |
def isoftype(object, type):
|
7 |
"""
|
8 |
Checks if an object is of a certain typing type, including nested types.
|
9 |
-
|
10 |
-
This function supports simple types (like `int`, `str`), typing types
|
11 |
-
(like `List[int]`, `Tuple[str, int]`, `Dict[str, int]`), and nested typing
|
12 |
types (like `List[List[int]]`, `Tuple[List[str], int]`, `Dict[str, List[int]]`).
|
13 |
|
14 |
Args:
|
@@ -28,8 +29,8 @@ def isoftype(object, type):
|
|
28 |
>>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]])
|
29 |
True
|
30 |
"""
|
31 |
-
|
32 |
-
if hasattr(type,
|
33 |
origin = type.__origin__
|
34 |
type_args = typing.get_args(type)
|
35 |
|
@@ -41,7 +42,8 @@ def isoftype(object, type):
|
|
41 |
return all(isoftype(element, type_arg) for element, type_arg in zip(object, type_args))
|
42 |
else:
|
43 |
return isinstance(object, type)
|
44 |
-
|
|
|
45 |
# copied from: https://github.com/bojiang/typing_utils/blob/main/typing_utils/__init__.py
|
46 |
# liscened under Apache License 2.0
|
47 |
|
@@ -144,7 +146,7 @@ def get_origin(type_):
|
|
144 |
get_origin(List[Tuple[T, T]][int]) == list
|
145 |
```
|
146 |
"""
|
147 |
-
if hasattr(typing,
|
148 |
_getter = getattr(typing, "get_origin")
|
149 |
ori = _getter(type_)
|
150 |
elif hasattr(typing.List, "_special"): # python 3.7
|
@@ -186,13 +188,11 @@ def get_args(type_) -> typing.Tuple:
|
|
186 |
get_args(Callable[[], T][int]) == ([], int)
|
187 |
```
|
188 |
"""
|
189 |
-
if hasattr(typing,
|
190 |
_getter = getattr(typing, "get_args")
|
191 |
res = _getter(type_)
|
192 |
elif hasattr(typing.List, "_special"): # python 3.7
|
193 |
-
if (
|
194 |
-
isinstance(type_, GenericClass) and not type_._special
|
195 |
-
): # backport for python 3.8
|
196 |
res = type_.__args__
|
197 |
if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis:
|
198 |
res = (list(res[:-1]), res[-1])
|
@@ -209,9 +209,9 @@ def get_args(type_) -> typing.Tuple:
|
|
209 |
|
210 |
|
211 |
def eval_forward_ref(ref, forward_refs=None):
|
212 |
-
|
213 |
eval forward_refs in all cPython versions
|
214 |
-
|
215 |
localns = forward_refs or {}
|
216 |
|
217 |
if hasattr(typing, "_eval_type"): # python3.8 & python 3.9
|
@@ -226,9 +226,9 @@ def eval_forward_ref(ref, forward_refs=None):
|
|
226 |
|
227 |
|
228 |
class NormalizedType(typing.NamedTuple):
|
229 |
-
|
230 |
Normalized type, made it possible to compare, hash between types.
|
231 |
-
|
232 |
|
233 |
origin: Type
|
234 |
args: typing.Union[tuple, frozenset] = tuple()
|
@@ -266,9 +266,9 @@ def _normalize_args(tps: TypeArgs):
|
|
266 |
|
267 |
|
268 |
def normalize(type_: Type) -> NormalizedType:
|
269 |
-
|
270 |
convert types to NormalizedType instances.
|
271 |
-
|
272 |
args = get_args(type_)
|
273 |
origin = get_origin(type_)
|
274 |
if not origin:
|
@@ -286,11 +286,7 @@ def _is_origin_subtype(left: OriginType, right: OriginType) -> bool:
|
|
286 |
if left is right:
|
287 |
return True
|
288 |
|
289 |
-
if
|
290 |
-
left is not None
|
291 |
-
and left in STATIC_SUBTYPE_MAPPING
|
292 |
-
and right == STATIC_SUBTYPE_MAPPING[left]
|
293 |
-
):
|
294 |
return True
|
295 |
|
296 |
if hasattr(left, "mro"):
|
@@ -326,24 +322,13 @@ def _is_origin_subtype_args(
|
|
326 |
return True
|
327 |
|
328 |
# Union[list, int] <> Union[typing.Sequence, int]
|
329 |
-
return all(
|
330 |
-
any(_is_normal_subtype(e, r, forward_refs) for r in right) for e in excluded
|
331 |
-
)
|
332 |
|
333 |
-
if isinstance(left, collections.abc.Sequence) and not isinstance(
|
334 |
-
|
335 |
-
):
|
336 |
-
if not isinstance(right, collections.abc.Sequence) or isinstance(
|
337 |
-
right, NormalizedType
|
338 |
-
):
|
339 |
return False
|
340 |
|
341 |
-
if
|
342 |
-
left
|
343 |
-
and left[-1].origin is not Ellipsis
|
344 |
-
and right
|
345 |
-
and right[-1].origin is Ellipsis
|
346 |
-
):
|
347 |
# Tuple[type, type] <> Tuple[type, ...]
|
348 |
return all(_is_origin_subtype_args(l, right[0], forward_refs) for l in left)
|
349 |
|
@@ -351,9 +336,7 @@ def _is_origin_subtype_args(
|
|
351 |
return False
|
352 |
|
353 |
return all(
|
354 |
-
l is not None
|
355 |
-
and r is not None
|
356 |
-
and _is_origin_subtype_args(l, r, forward_refs)
|
357 |
for l, r in itertools.zip_longest(left, right)
|
358 |
)
|
359 |
|
@@ -368,7 +351,6 @@ def _is_normal_subtype(
|
|
368 |
right: NormalizedType,
|
369 |
forward_refs: typing.Optional[typing.Mapping[str, type]],
|
370 |
) -> typing.Optional[bool]:
|
371 |
-
|
372 |
if isinstance(left.origin, ForwardRef):
|
373 |
left = normalize(eval_forward_ref(left.origin, forward_refs=forward_refs))
|
374 |
|
@@ -383,18 +365,12 @@ def _is_normal_subtype(
|
|
383 |
if right.origin is typing.Union and left.origin is typing.Union:
|
384 |
return _is_origin_subtype_args(left.args, right.args, forward_refs)
|
385 |
if right.origin is typing.Union:
|
386 |
-
return optional_any(
|
387 |
-
_is_normal_subtype(left, a, forward_refs) for a in right.args
|
388 |
-
)
|
389 |
if left.origin is typing.Union:
|
390 |
-
return optional_all(
|
391 |
-
_is_normal_subtype(a, right, forward_refs) for a in left.args
|
392 |
-
)
|
393 |
|
394 |
# TypeVar
|
395 |
-
if isinstance(left.origin, typing.TypeVar) and isinstance(
|
396 |
-
right.origin, typing.TypeVar
|
397 |
-
):
|
398 |
if left.origin is right.origin:
|
399 |
return True
|
400 |
|
@@ -402,9 +378,7 @@ def _is_normal_subtype(
|
|
402 |
right_bound = getattr(right.origin, "__bound__", None)
|
403 |
if right_bound is None or left_bound is None:
|
404 |
return unknown
|
405 |
-
return _is_normal_subtype(
|
406 |
-
normalize(left_bound), normalize(right_bound), forward_refs
|
407 |
-
)
|
408 |
if isinstance(right.origin, typing.TypeVar):
|
409 |
return unknown
|
410 |
if isinstance(left.origin, typing.TypeVar):
|
@@ -426,7 +400,9 @@ def _is_normal_subtype(
|
|
426 |
|
427 |
|
428 |
def issubtype(
|
429 |
-
left: Type,
|
|
|
|
|
430 |
) -> typing.Optional[bool]:
|
431 |
"""Check that the left argument is a subtype of the right.
|
432 |
For unions, check if the type arguments of the left is a subset of the right.
|
|
|
3 |
import itertools
|
4 |
import typing
|
5 |
|
6 |
+
|
7 |
def isoftype(object, type):
|
8 |
"""
|
9 |
Checks if an object is of a certain typing type, including nested types.
|
10 |
+
|
11 |
+
This function supports simple types (like `int`, `str`), typing types
|
12 |
+
(like `List[int]`, `Tuple[str, int]`, `Dict[str, int]`), and nested typing
|
13 |
types (like `List[List[int]]`, `Tuple[List[str], int]`, `Dict[str, List[int]]`).
|
14 |
|
15 |
Args:
|
|
|
29 |
>>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]])
|
30 |
True
|
31 |
"""
|
32 |
+
|
33 |
+
if hasattr(type, "__origin__"):
|
34 |
origin = type.__origin__
|
35 |
type_args = typing.get_args(type)
|
36 |
|
|
|
42 |
return all(isoftype(element, type_arg) for element, type_arg in zip(object, type_args))
|
43 |
else:
|
44 |
return isinstance(object, type)
|
45 |
+
|
46 |
+
|
47 |
# copied from: https://github.com/bojiang/typing_utils/blob/main/typing_utils/__init__.py
|
48 |
# liscened under Apache License 2.0
|
49 |
|
|
|
146 |
get_origin(List[Tuple[T, T]][int]) == list
|
147 |
```
|
148 |
"""
|
149 |
+
if hasattr(typing, "get_origin"): # python 3.8+
|
150 |
_getter = getattr(typing, "get_origin")
|
151 |
ori = _getter(type_)
|
152 |
elif hasattr(typing.List, "_special"): # python 3.7
|
|
|
188 |
get_args(Callable[[], T][int]) == ([], int)
|
189 |
```
|
190 |
"""
|
191 |
+
if hasattr(typing, "get_args"): # python 3.8+
|
192 |
_getter = getattr(typing, "get_args")
|
193 |
res = _getter(type_)
|
194 |
elif hasattr(typing.List, "_special"): # python 3.7
|
195 |
+
if isinstance(type_, GenericClass) and not type_._special: # backport for python 3.8
|
|
|
|
|
196 |
res = type_.__args__
|
197 |
if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis:
|
198 |
res = (list(res[:-1]), res[-1])
|
|
|
209 |
|
210 |
|
211 |
def eval_forward_ref(ref, forward_refs=None):
|
212 |
+
"""
|
213 |
eval forward_refs in all cPython versions
|
214 |
+
"""
|
215 |
localns = forward_refs or {}
|
216 |
|
217 |
if hasattr(typing, "_eval_type"): # python3.8 & python 3.9
|
|
|
226 |
|
227 |
|
228 |
class NormalizedType(typing.NamedTuple):
|
229 |
+
"""
|
230 |
Normalized type, made it possible to compare, hash between types.
|
231 |
+
"""
|
232 |
|
233 |
origin: Type
|
234 |
args: typing.Union[tuple, frozenset] = tuple()
|
|
|
266 |
|
267 |
|
268 |
def normalize(type_: Type) -> NormalizedType:
|
269 |
+
"""
|
270 |
convert types to NormalizedType instances.
|
271 |
+
"""
|
272 |
args = get_args(type_)
|
273 |
origin = get_origin(type_)
|
274 |
if not origin:
|
|
|
286 |
if left is right:
|
287 |
return True
|
288 |
|
289 |
+
if left is not None and left in STATIC_SUBTYPE_MAPPING and right == STATIC_SUBTYPE_MAPPING[left]:
|
|
|
|
|
|
|
|
|
290 |
return True
|
291 |
|
292 |
if hasattr(left, "mro"):
|
|
|
322 |
return True
|
323 |
|
324 |
# Union[list, int] <> Union[typing.Sequence, int]
|
325 |
+
return all(any(_is_normal_subtype(e, r, forward_refs) for r in right) for e in excluded)
|
|
|
|
|
326 |
|
327 |
+
if isinstance(left, collections.abc.Sequence) and not isinstance(left, NormalizedType):
|
328 |
+
if not isinstance(right, collections.abc.Sequence) or isinstance(right, NormalizedType):
|
|
|
|
|
|
|
|
|
329 |
return False
|
330 |
|
331 |
+
if left and left[-1].origin is not Ellipsis and right and right[-1].origin is Ellipsis:
|
|
|
|
|
|
|
|
|
|
|
332 |
# Tuple[type, type] <> Tuple[type, ...]
|
333 |
return all(_is_origin_subtype_args(l, right[0], forward_refs) for l in left)
|
334 |
|
|
|
336 |
return False
|
337 |
|
338 |
return all(
|
339 |
+
l is not None and r is not None and _is_origin_subtype_args(l, r, forward_refs)
|
|
|
|
|
340 |
for l, r in itertools.zip_longest(left, right)
|
341 |
)
|
342 |
|
|
|
351 |
right: NormalizedType,
|
352 |
forward_refs: typing.Optional[typing.Mapping[str, type]],
|
353 |
) -> typing.Optional[bool]:
|
|
|
354 |
if isinstance(left.origin, ForwardRef):
|
355 |
left = normalize(eval_forward_ref(left.origin, forward_refs=forward_refs))
|
356 |
|
|
|
365 |
if right.origin is typing.Union and left.origin is typing.Union:
|
366 |
return _is_origin_subtype_args(left.args, right.args, forward_refs)
|
367 |
if right.origin is typing.Union:
|
368 |
+
return optional_any(_is_normal_subtype(left, a, forward_refs) for a in right.args)
|
|
|
|
|
369 |
if left.origin is typing.Union:
|
370 |
+
return optional_all(_is_normal_subtype(a, right, forward_refs) for a in left.args)
|
|
|
|
|
371 |
|
372 |
# TypeVar
|
373 |
+
if isinstance(left.origin, typing.TypeVar) and isinstance(right.origin, typing.TypeVar):
|
|
|
|
|
374 |
if left.origin is right.origin:
|
375 |
return True
|
376 |
|
|
|
378 |
right_bound = getattr(right.origin, "__bound__", None)
|
379 |
if right_bound is None or left_bound is None:
|
380 |
return unknown
|
381 |
+
return _is_normal_subtype(normalize(left_bound), normalize(right_bound), forward_refs)
|
|
|
|
|
382 |
if isinstance(right.origin, typing.TypeVar):
|
383 |
return unknown
|
384 |
if isinstance(left.origin, typing.TypeVar):
|
|
|
400 |
|
401 |
|
402 |
def issubtype(
|
403 |
+
left: Type,
|
404 |
+
right: Type,
|
405 |
+
forward_refs: typing.Optional[dict] = None,
|
406 |
) -> typing.Optional[bool]:
|
407 |
"""Check that the left argument is a subtype of the right.
|
408 |
For unions, check if the type arguments of the left is a subset of the right.
|