-
Notifications
You must be signed in to change notification settings - Fork 0
/
typedefs.py
48 lines (37 loc) · 1.36 KB
/
typedefs.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
import typing
from pydantic_core import core_schema, SchemaSerializer
Json: typing.TypeAlias = (
int | str | bool | float | None | list["Json"] | dict[str, "Json"]
)
HTTP_METHODS = typing.Literal["GET", "POST", "PUT", "PATCH", "DELETE"]
class NotSet:
"""Use this class instead of None for propertites and fields that were not set"""
_instance = None
def __new__(cls) -> "NotSet":
if cls._instance == None:
cls._instance = super(NotSet, cls).__new__(cls)
return cls._instance
def __bool__(self):
return False
def __repr__(self) -> str:
return "NotSet"
@classmethod
def _validate(cls, value: "NotSet"):
return not value
@classmethod
def __get_pydantic_core_schema__(cls, source_type, handler):
_ = source_type
schema = core_schema.no_info_after_validator_function(
cls._validate,
handler(set),
serialization=core_schema.plain_serializer_function_ser_schema(
set,
info_arg=False,
return_schema=core_schema.set_schema(),
),
)
# https://github.com/pydantic/pydantic/issues/7779#issuecomment-1775629521
cls.__pydantic_serializer__ = SchemaSerializer(
schema
) # <-- this is necessary for pydantic-core to serialize
return schema