generated from yanyongyu/python-poetry-template
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
76 lines (54 loc) · 1.78 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import inspect
from enum import Enum
from typing_extensions import TypeAlias
from typing import Any, Dict, Union, Literal, TypeVar, final
from pydantic_core import to_jsonable_python
T = TypeVar("T")
@final
class Unset(Enum):
_UNSET = "<UNSET>"
def __repr__(self) -> str:
return "<UNSET>"
def __str__(self) -> str:
return self.__repr__()
def __bool__(self) -> Literal[False]:
return False
def __copy__(self):
return self._UNSET
def __deepcopy__(self, memo: Dict[int, Any]):
return self._UNSET
@classmethod
def __get_validators__(cls):
yield cls._validate
@classmethod
def _validate(cls, value: Any):
if value is not cls._UNSET:
raise ValueError(f"{value!r} is not UNSET")
return value
UNSET = Unset._UNSET
Missing: TypeAlias = Union[Literal[UNSET], T]
def exclude_unset(data: Any) -> Any:
if isinstance(data, dict):
return data.__class__(
(k, exclude_unset(v)) for k, v in data.items() if v is not UNSET
)
elif isinstance(data, list):
return data.__class__(exclude_unset(i) for i in data)
elif data is UNSET:
return None
return data
def is_async(obj: Any) -> bool:
if inspect.isroutine(obj):
return inspect.iscoroutinefunction(obj)
if inspect.isclass(obj):
return False
func_ = getattr(obj, "__call__", None)
return inspect.iscoroutinefunction(func_)
def obj_to_jsonable(obj: Any) -> Any:
if isinstance(obj, dict):
return {k: obj_to_jsonable(v) for k, v in obj.items()}
if isinstance(obj, (list, tuple)):
return [obj_to_jsonable(item) for item in obj]
if obj is None or isinstance(obj, (int, float, str, bool)):
return obj
return to_jsonable_python(obj)