Upload type_utils.py with huggingface_hub
Browse files- type_utils.py +69 -50
type_utils.py
CHANGED
|
@@ -19,21 +19,30 @@ def isoftype(object, type):
|
|
| 19 |
bool: True if the object is of the specified type, False otherwise.
|
| 20 |
|
| 21 |
Examples:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
True
|
| 26 |
-
|
| 27 |
-
False
|
| 28 |
-
|
| 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])
|
|
@@ -46,8 +55,6 @@ def isoftype(object, type):
|
|
| 46 |
)
|
| 47 |
return None
|
| 48 |
|
| 49 |
-
if type == typing.Any:
|
| 50 |
-
return True
|
| 51 |
return isinstance(object, type)
|
| 52 |
|
| 53 |
|
|
@@ -141,17 +148,21 @@ def get_origin(type_):
|
|
| 141 |
Return None for unsupported types.
|
| 142 |
|
| 143 |
Examples:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
"""
|
| 156 |
if hasattr(typing, "get_origin"): # python 3.8+
|
| 157 |
_getter = typing.get_origin
|
|
@@ -185,15 +196,18 @@ def get_args(type_) -> typing.Tuple:
|
|
| 185 |
For unions, basic simplifications used by Union constructor are performed.
|
| 186 |
|
| 187 |
Examples:
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
"""
|
| 198 |
if hasattr(typing, "get_args"): # python 3.8+
|
| 199 |
_getter = typing.get_args
|
|
@@ -440,25 +454,30 @@ def issubtype(
|
|
| 440 |
Also works for nested types including ForwardRefs.
|
| 441 |
|
| 442 |
Examples:
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
typing.
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
"""
|
| 464 |
return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
|
|
|
|
| 19 |
bool: True if the object is of the specified type, False otherwise.
|
| 20 |
|
| 21 |
Examples:
|
| 22 |
+
.. highlight:: python
|
| 23 |
+
.. code-block:: python
|
| 24 |
+
|
| 25 |
+
isoftype(1, int) # True
|
| 26 |
+
isoftype([1, 2, 3], typing.List[int]) # True
|
| 27 |
+
isoftype([1, 2, 3], typing.List[str]) # False
|
| 28 |
+
isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]]) # True
|
|
|
|
| 29 |
"""
|
| 30 |
+
if type == typing.Any:
|
| 31 |
+
return True
|
| 32 |
+
|
| 33 |
if hasattr(type, "__origin__"):
|
| 34 |
origin = type.__origin__
|
| 35 |
type_args = typing.get_args(type)
|
| 36 |
|
| 37 |
+
if origin is typing.Union:
|
| 38 |
+
return any(isoftype(object, sub_type) for sub_type in type_args)
|
| 39 |
+
|
| 40 |
+
if not isinstance(object, origin):
|
| 41 |
+
return False
|
| 42 |
+
|
| 43 |
if origin is list or origin is set:
|
| 44 |
return all(isoftype(element, type_args[0]) for element in object)
|
| 45 |
+
|
| 46 |
if origin is dict:
|
| 47 |
return all(
|
| 48 |
isoftype(key, type_args[0]) and isoftype(value, type_args[1])
|
|
|
|
| 55 |
)
|
| 56 |
return None
|
| 57 |
|
|
|
|
|
|
|
| 58 |
return isinstance(object, type)
|
| 59 |
|
| 60 |
|
|
|
|
| 148 |
Return None for unsupported types.
|
| 149 |
|
| 150 |
Examples:
|
| 151 |
+
Here are some code examples using `get_origin` from the `typing_utils` module:
|
| 152 |
+
|
| 153 |
+
.. code-block:: python
|
| 154 |
+
|
| 155 |
+
from typing_utils import get_origin
|
| 156 |
+
|
| 157 |
+
# Examples of get_origin usage
|
| 158 |
+
get_origin(Literal[42]) is Literal # True
|
| 159 |
+
get_origin(int) is None # True
|
| 160 |
+
get_origin(ClassVar[int]) is ClassVar # True
|
| 161 |
+
get_origin(Generic) is Generic # True
|
| 162 |
+
get_origin(Generic[T]) is Generic # True
|
| 163 |
+
get_origin(Union[T, int]) is Union # True
|
| 164 |
+
get_origin(List[Tuple[T, T]][int]) == list # True
|
| 165 |
+
|
| 166 |
"""
|
| 167 |
if hasattr(typing, "get_origin"): # python 3.8+
|
| 168 |
_getter = typing.get_origin
|
|
|
|
| 196 |
For unions, basic simplifications used by Union constructor are performed.
|
| 197 |
|
| 198 |
Examples:
|
| 199 |
+
Here are some code examples using `get_args` from the `typing_utils` module:
|
| 200 |
+
|
| 201 |
+
.. code-block:: python
|
| 202 |
+
|
| 203 |
+
from typing_utils import get_args
|
| 204 |
+
|
| 205 |
+
# Examples of get_args usage
|
| 206 |
+
get_args(Dict[str, int]) == (str, int) # True
|
| 207 |
+
get_args(int) == () # True
|
| 208 |
+
get_args(Union[int, Union[T, int], str][int]) == (int, str) # True
|
| 209 |
+
get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) # True
|
| 210 |
+
get_args(Callable[[], T][int]) == ([], int) # True
|
| 211 |
"""
|
| 212 |
if hasattr(typing, "get_args"): # python 3.8+
|
| 213 |
_getter = typing.get_args
|
|
|
|
| 454 |
Also works for nested types including ForwardRefs.
|
| 455 |
|
| 456 |
Examples:
|
| 457 |
+
Here are some code examples using `issubtype` from the `typing_utils` module:
|
| 458 |
+
|
| 459 |
+
.. code-block:: python
|
| 460 |
+
|
| 461 |
+
from typing_utils import issubtype
|
| 462 |
+
|
| 463 |
+
# Examples of issubtype checks
|
| 464 |
+
issubtype(typing.List, typing.Any) # True
|
| 465 |
+
issubtype(list, list) # True
|
| 466 |
+
issubtype(list, typing.List) # True
|
| 467 |
+
issubtype(list, typing.Sequence) # True
|
| 468 |
+
issubtype(typing.List[int], list) # True
|
| 469 |
+
issubtype(typing.List[typing.List], list) # True
|
| 470 |
+
issubtype(list, typing.List[int]) # False
|
| 471 |
+
issubtype(list, typing.Union[typing.Tuple, typing.Set]) # False
|
| 472 |
+
issubtype(typing.List[typing.List], typing.List[typing.Sequence]) # True
|
| 473 |
+
|
| 474 |
+
# Example with custom JSON type
|
| 475 |
+
JSON = typing.Union[
|
| 476 |
+
int, float, bool, str, None, typing.Sequence["JSON"],
|
| 477 |
+
typing.Mapping[str, "JSON"]
|
| 478 |
+
]
|
| 479 |
+
issubtype(str, JSON, forward_refs={'JSON': JSON}) # True
|
| 480 |
+
issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) # True
|
| 481 |
+
issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) # False
|
| 482 |
"""
|
| 483 |
return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
|