Upload type_utils.py with huggingface_hub
Browse files- type_utils.py +40 -0
type_utils.py
CHANGED
@@ -3,6 +3,46 @@ import io
|
|
3 |
import itertools
|
4 |
import typing
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def isoftype(object, type):
|
8 |
"""Checks if an object is of a certain typing type, including nested types.
|
|
|
3 |
import itertools
|
4 |
import typing
|
5 |
|
6 |
+
from .utils import safe_eval
|
7 |
+
|
8 |
+
|
9 |
+
def parse_type_string(type_string: str) -> typing.Any:
|
10 |
+
"""Parses a string representing a Python type hint and evaluates it to return the corresponding type object.
|
11 |
+
|
12 |
+
This function uses a safe evaluation context
|
13 |
+
to mitigate the risks of executing arbitrary code.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
type_string (str): A string representation of a Python type hint. Examples include
|
17 |
+
'List[int]', 'Dict[str, Any]', 'Optional[List[str]]', etc.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
typing.Any: The Python type object corresponding to the given type string.
|
21 |
+
|
22 |
+
Raises:
|
23 |
+
ValueError: If the type string contains elements not allowed in the safe context
|
24 |
+
or tokens list.
|
25 |
+
|
26 |
+
The function uses a predefined safe context with common types from the `typing` module
|
27 |
+
and basic Python data types. It also defines a list of safe tokens that are allowed
|
28 |
+
in the type string.
|
29 |
+
"""
|
30 |
+
safe_context = {
|
31 |
+
"Any": typing.Any,
|
32 |
+
"List": typing.List,
|
33 |
+
"Dict": typing.Dict,
|
34 |
+
"Tuple": typing.Tuple,
|
35 |
+
"Union": typing.Union,
|
36 |
+
"int": int,
|
37 |
+
"str": str,
|
38 |
+
"float": float,
|
39 |
+
"bool": bool,
|
40 |
+
"Optional": typing.Optional,
|
41 |
+
}
|
42 |
+
|
43 |
+
safe_tokens = ["[", "]", ",", " "]
|
44 |
+
return safe_eval(type_string, safe_context, safe_tokens)
|
45 |
+
|
46 |
|
47 |
def isoftype(object, type):
|
48 |
"""Checks if an object is of a certain typing type, including nested types.
|