Upload type_utils.py with huggingface_hub
Browse files- type_utils.py +71 -43
type_utils.py
CHANGED
@@ -5,8 +5,7 @@ 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
|
@@ -29,21 +28,27 @@ def isoftype(object, type):
|
|
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 |
|
37 |
if origin is list or origin is set:
|
38 |
return all(isoftype(element, type_args[0]) for element in object)
|
39 |
-
|
40 |
-
return all(
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
if
|
45 |
-
return
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
|
49 |
# copied from: https://github.com/bojiang/typing_utils/blob/main/typing_utils/__init__.py
|
@@ -51,9 +56,9 @@ def isoftype(object, type):
|
|
51 |
|
52 |
|
53 |
if hasattr(typing, "ForwardRef"): # python3.8
|
54 |
-
ForwardRef =
|
55 |
elif hasattr(typing, "_ForwardRef"): # python3.6
|
56 |
-
ForwardRef =
|
57 |
else:
|
58 |
raise NotImplementedError()
|
59 |
|
@@ -131,11 +136,11 @@ def _normalize_aliases(type_: Type) -> Type:
|
|
131 |
|
132 |
def get_origin(type_):
|
133 |
"""Get the unsubscripted version of a type.
|
|
|
134 |
This supports generic types, Callable, Tuple, Union, Literal, Final and ClassVar.
|
135 |
Return None for unsupported types.
|
136 |
|
137 |
Examples:
|
138 |
-
|
139 |
```python
|
140 |
from typing_utils import get_origin
|
141 |
|
@@ -149,7 +154,7 @@ def get_origin(type_):
|
|
149 |
```
|
150 |
"""
|
151 |
if hasattr(typing, "get_origin"): # python 3.8+
|
152 |
-
_getter =
|
153 |
ori = _getter(type_)
|
154 |
elif hasattr(typing.List, "_special"): # python 3.7
|
155 |
if isinstance(type_, GenericClass) and not type_._special:
|
@@ -176,10 +181,10 @@ def get_origin(type_):
|
|
176 |
|
177 |
def get_args(type_) -> typing.Tuple:
|
178 |
"""Get type arguments with all substitutions performed.
|
|
|
179 |
For unions, basic simplifications used by Union constructor are performed.
|
180 |
|
181 |
Examples:
|
182 |
-
|
183 |
```python
|
184 |
from typing_utils import get_args
|
185 |
|
@@ -191,10 +196,12 @@ def get_args(type_) -> typing.Tuple:
|
|
191 |
```
|
192 |
"""
|
193 |
if hasattr(typing, "get_args"): # python 3.8+
|
194 |
-
_getter =
|
195 |
res = _getter(type_)
|
196 |
elif hasattr(typing.List, "_special"): # python 3.7
|
197 |
-
if
|
|
|
|
|
198 |
res = type_.__args__
|
199 |
if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis:
|
200 |
res = (list(res[:-1]), res[-1])
|
@@ -211,29 +218,25 @@ def get_args(type_) -> typing.Tuple:
|
|
211 |
|
212 |
|
213 |
def eval_forward_ref(ref, forward_refs=None):
|
214 |
-
"""
|
215 |
-
eval forward_refs in all cPython versions
|
216 |
-
"""
|
217 |
localns = forward_refs or {}
|
218 |
|
219 |
if hasattr(typing, "_eval_type"): # python3.8 & python 3.9
|
220 |
-
_eval_type =
|
221 |
return _eval_type(ref, globals(), localns)
|
222 |
|
223 |
if hasattr(ref, "_eval_type"): # python3.6
|
224 |
-
_eval_type =
|
225 |
return _eval_type(globals(), localns)
|
226 |
|
227 |
raise NotImplementedError()
|
228 |
|
229 |
|
230 |
class NormalizedType(typing.NamedTuple):
|
231 |
-
"""
|
232 |
-
Normalized type, made it possible to compare, hash between types.
|
233 |
-
"""
|
234 |
|
235 |
origin: Type
|
236 |
-
args: typing.Union[tuple, frozenset] =
|
237 |
|
238 |
def __eq__(self, other):
|
239 |
if isinstance(other, NormalizedType):
|
@@ -268,9 +271,7 @@ def _normalize_args(tps: TypeArgs):
|
|
268 |
|
269 |
|
270 |
def normalize(type_: Type) -> NormalizedType:
|
271 |
-
"""
|
272 |
-
convert types to NormalizedType instances.
|
273 |
-
"""
|
274 |
args = get_args(type_)
|
275 |
origin = get_origin(type_)
|
276 |
if not origin:
|
@@ -288,7 +289,11 @@ def _is_origin_subtype(left: OriginType, right: OriginType) -> bool:
|
|
288 |
if left is right:
|
289 |
return True
|
290 |
|
291 |
-
if
|
|
|
|
|
|
|
|
|
292 |
return True
|
293 |
|
294 |
if hasattr(left, "mro"):
|
@@ -324,22 +329,37 @@ def _is_origin_subtype_args(
|
|
324 |
return True
|
325 |
|
326 |
# Union[list, int] <> Union[typing.Sequence, int]
|
327 |
-
return all(
|
|
|
|
|
328 |
|
329 |
-
if isinstance(left, collections.abc.Sequence) and not isinstance(
|
330 |
-
|
|
|
|
|
|
|
|
|
331 |
return False
|
332 |
|
333 |
-
if
|
|
|
|
|
|
|
|
|
|
|
334 |
# Tuple[type, type] <> Tuple[type, ...]
|
335 |
-
return all(
|
|
|
|
|
336 |
|
337 |
if len(left) != len(right):
|
338 |
return False
|
339 |
|
340 |
return all(
|
341 |
-
|
342 |
-
|
|
|
|
|
343 |
)
|
344 |
|
345 |
assert isinstance(left, NormalizedType)
|
@@ -367,12 +387,18 @@ def _is_normal_subtype(
|
|
367 |
if right.origin is typing.Union and left.origin is typing.Union:
|
368 |
return _is_origin_subtype_args(left.args, right.args, forward_refs)
|
369 |
if right.origin is typing.Union:
|
370 |
-
return optional_any(
|
|
|
|
|
371 |
if left.origin is typing.Union:
|
372 |
-
return optional_all(
|
|
|
|
|
373 |
|
374 |
# TypeVar
|
375 |
-
if isinstance(left.origin, typing.TypeVar) and isinstance(
|
|
|
|
|
376 |
if left.origin is right.origin:
|
377 |
return True
|
378 |
|
@@ -380,7 +406,9 @@ def _is_normal_subtype(
|
|
380 |
right_bound = getattr(right.origin, "__bound__", None)
|
381 |
if right_bound is None or left_bound is None:
|
382 |
return unknown
|
383 |
-
return _is_normal_subtype(
|
|
|
|
|
384 |
if isinstance(right.origin, typing.TypeVar):
|
385 |
return unknown
|
386 |
if isinstance(left.origin, typing.TypeVar):
|
@@ -407,11 +435,11 @@ def issubtype(
|
|
407 |
forward_refs: typing.Optional[dict] = None,
|
408 |
) -> typing.Optional[bool]:
|
409 |
"""Check that the left argument is a subtype of the right.
|
|
|
410 |
For unions, check if the type arguments of the left is a subset of the right.
|
411 |
Also works for nested types including ForwardRefs.
|
412 |
|
413 |
Examples:
|
414 |
-
|
415 |
```python
|
416 |
from typing_utils import issubtype
|
417 |
|
|
|
5 |
|
6 |
|
7 |
def isoftype(object, type):
|
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
|
|
|
28 |
>>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]])
|
29 |
True
|
30 |
"""
|
|
|
31 |
if hasattr(type, "__origin__"):
|
32 |
origin = type.__origin__
|
33 |
type_args = typing.get_args(type)
|
34 |
|
35 |
if origin is list or origin is set:
|
36 |
return all(isoftype(element, type_args[0]) for element in object)
|
37 |
+
if origin is dict:
|
38 |
+
return all(
|
39 |
+
isoftype(key, type_args[0]) and isoftype(value, type_args[1])
|
40 |
+
for key, value in object.items()
|
41 |
+
)
|
42 |
+
if origin is tuple:
|
43 |
+
return all(
|
44 |
+
isoftype(element, type_arg)
|
45 |
+
for element, type_arg in zip(object, type_args)
|
46 |
+
)
|
47 |
+
return None
|
48 |
+
|
49 |
+
if type == typing.Any:
|
50 |
+
return True
|
51 |
+
return isinstance(object, type)
|
52 |
|
53 |
|
54 |
# copied from: https://github.com/bojiang/typing_utils/blob/main/typing_utils/__init__.py
|
|
|
56 |
|
57 |
|
58 |
if hasattr(typing, "ForwardRef"): # python3.8
|
59 |
+
ForwardRef = typing.ForwardRef
|
60 |
elif hasattr(typing, "_ForwardRef"): # python3.6
|
61 |
+
ForwardRef = typing._ForwardRef
|
62 |
else:
|
63 |
raise NotImplementedError()
|
64 |
|
|
|
136 |
|
137 |
def get_origin(type_):
|
138 |
"""Get the unsubscripted version of a type.
|
139 |
+
|
140 |
This supports generic types, Callable, Tuple, Union, Literal, Final and ClassVar.
|
141 |
Return None for unsupported types.
|
142 |
|
143 |
Examples:
|
|
|
144 |
```python
|
145 |
from typing_utils import get_origin
|
146 |
|
|
|
154 |
```
|
155 |
"""
|
156 |
if hasattr(typing, "get_origin"): # python 3.8+
|
157 |
+
_getter = typing.get_origin
|
158 |
ori = _getter(type_)
|
159 |
elif hasattr(typing.List, "_special"): # python 3.7
|
160 |
if isinstance(type_, GenericClass) and not type_._special:
|
|
|
181 |
|
182 |
def get_args(type_) -> typing.Tuple:
|
183 |
"""Get type arguments with all substitutions performed.
|
184 |
+
|
185 |
For unions, basic simplifications used by Union constructor are performed.
|
186 |
|
187 |
Examples:
|
|
|
188 |
```python
|
189 |
from typing_utils import get_args
|
190 |
|
|
|
196 |
```
|
197 |
"""
|
198 |
if hasattr(typing, "get_args"): # python 3.8+
|
199 |
+
_getter = typing.get_args
|
200 |
res = _getter(type_)
|
201 |
elif hasattr(typing.List, "_special"): # python 3.7
|
202 |
+
if (
|
203 |
+
isinstance(type_, GenericClass) and not type_._special
|
204 |
+
): # backport for python 3.8
|
205 |
res = type_.__args__
|
206 |
if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis:
|
207 |
res = (list(res[:-1]), res[-1])
|
|
|
218 |
|
219 |
|
220 |
def eval_forward_ref(ref, forward_refs=None):
|
221 |
+
"""Eval forward_refs in all cPython versions."""
|
|
|
|
|
222 |
localns = forward_refs or {}
|
223 |
|
224 |
if hasattr(typing, "_eval_type"): # python3.8 & python 3.9
|
225 |
+
_eval_type = typing._eval_type
|
226 |
return _eval_type(ref, globals(), localns)
|
227 |
|
228 |
if hasattr(ref, "_eval_type"): # python3.6
|
229 |
+
_eval_type = ref._eval_type
|
230 |
return _eval_type(globals(), localns)
|
231 |
|
232 |
raise NotImplementedError()
|
233 |
|
234 |
|
235 |
class NormalizedType(typing.NamedTuple):
|
236 |
+
"""Normalized type, made it possible to compare, hash between types."""
|
|
|
|
|
237 |
|
238 |
origin: Type
|
239 |
+
args: typing.Union[tuple, frozenset] = ()
|
240 |
|
241 |
def __eq__(self, other):
|
242 |
if isinstance(other, NormalizedType):
|
|
|
271 |
|
272 |
|
273 |
def normalize(type_: Type) -> NormalizedType:
|
274 |
+
"""Convert types to NormalizedType instances."""
|
|
|
|
|
275 |
args = get_args(type_)
|
276 |
origin = get_origin(type_)
|
277 |
if not origin:
|
|
|
289 |
if left is right:
|
290 |
return True
|
291 |
|
292 |
+
if (
|
293 |
+
left is not None
|
294 |
+
and left in STATIC_SUBTYPE_MAPPING
|
295 |
+
and right == STATIC_SUBTYPE_MAPPING[left]
|
296 |
+
):
|
297 |
return True
|
298 |
|
299 |
if hasattr(left, "mro"):
|
|
|
329 |
return True
|
330 |
|
331 |
# Union[list, int] <> Union[typing.Sequence, int]
|
332 |
+
return all(
|
333 |
+
any(_is_normal_subtype(e, r, forward_refs) for r in right) for e in excluded
|
334 |
+
)
|
335 |
|
336 |
+
if isinstance(left, collections.abc.Sequence) and not isinstance(
|
337 |
+
left, NormalizedType
|
338 |
+
):
|
339 |
+
if not isinstance(right, collections.abc.Sequence) or isinstance(
|
340 |
+
right, NormalizedType
|
341 |
+
):
|
342 |
return False
|
343 |
|
344 |
+
if (
|
345 |
+
left
|
346 |
+
and left[-1].origin is not Ellipsis
|
347 |
+
and right
|
348 |
+
and right[-1].origin is Ellipsis
|
349 |
+
):
|
350 |
# Tuple[type, type] <> Tuple[type, ...]
|
351 |
+
return all(
|
352 |
+
_is_origin_subtype_args(lft, right[0], forward_refs) for lft in left
|
353 |
+
)
|
354 |
|
355 |
if len(left) != len(right):
|
356 |
return False
|
357 |
|
358 |
return all(
|
359 |
+
lft is not None
|
360 |
+
and rgt is not None
|
361 |
+
and _is_origin_subtype_args(lft, rgt, forward_refs)
|
362 |
+
for lft, rgt in itertools.zip_longest(left, right)
|
363 |
)
|
364 |
|
365 |
assert isinstance(left, NormalizedType)
|
|
|
387 |
if right.origin is typing.Union and left.origin is typing.Union:
|
388 |
return _is_origin_subtype_args(left.args, right.args, forward_refs)
|
389 |
if right.origin is typing.Union:
|
390 |
+
return optional_any(
|
391 |
+
_is_normal_subtype(left, a, forward_refs) for a in right.args
|
392 |
+
)
|
393 |
if left.origin is typing.Union:
|
394 |
+
return optional_all(
|
395 |
+
_is_normal_subtype(a, right, forward_refs) for a in left.args
|
396 |
+
)
|
397 |
|
398 |
# TypeVar
|
399 |
+
if isinstance(left.origin, typing.TypeVar) and isinstance(
|
400 |
+
right.origin, typing.TypeVar
|
401 |
+
):
|
402 |
if left.origin is right.origin:
|
403 |
return True
|
404 |
|
|
|
406 |
right_bound = getattr(right.origin, "__bound__", None)
|
407 |
if right_bound is None or left_bound is None:
|
408 |
return unknown
|
409 |
+
return _is_normal_subtype(
|
410 |
+
normalize(left_bound), normalize(right_bound), forward_refs
|
411 |
+
)
|
412 |
if isinstance(right.origin, typing.TypeVar):
|
413 |
return unknown
|
414 |
if isinstance(left.origin, typing.TypeVar):
|
|
|
435 |
forward_refs: typing.Optional[dict] = None,
|
436 |
) -> typing.Optional[bool]:
|
437 |
"""Check that the left argument is a subtype of the right.
|
438 |
+
|
439 |
For unions, check if the type arguments of the left is a subset of the right.
|
440 |
Also works for nested types including ForwardRefs.
|
441 |
|
442 |
Examples:
|
|
|
443 |
```python
|
444 |
from typing_utils import issubtype
|
445 |
|