This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
conftest.py
118 lines (77 loc) · 2.25 KB
/
conftest.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, NewType, Optional, Tuple, Union
try:
from fastuuid import UUID
except ImportError:
from uuid import UUID
from dataclasses_jsonschema import FieldEncoder, JsonSchemaMixin
Postcode = NewType("Postcode", str)
class PostcodeField(FieldEncoder):
@property
def json_schema(self):
return {"type": "string", "minLength": 5, "maxLength": 8}
JsonSchemaMixin.register_field_encoders({Postcode: PostcodeField()})
class SubSchemas(JsonSchemaMixin):
pass
class Weekday(Enum):
MON = "Monday"
TUE = "Tuesday"
WED = "Wednesday"
THU = "Thursday"
FRI = "Friday"
@dataclass(eq=True, frozen=True)
class Point(SubSchemas):
x: float = field(metadata=dict(description="Point x coordinate"))
y: float = field(metadata=dict(description="Point y coordinate"))
@classmethod
def field_mapping(cls) -> Dict[str, str]:
return {"x": "z"}
NewPoint = NewType("NewPoint", Point)
@dataclass
class Foo(SubSchemas):
"""A foo that foos"""
a: datetime
b: Optional[List[Point]]
c: Dict[str, int]
d: Weekday = field(init=False)
f: Tuple[str, int]
g: Tuple[str, ...]
e: Optional[Postcode] = None
h: Optional[NewPoint] = None
@dataclass(eq=True)
class Bar(JsonSchemaMixin):
"""Type with union field"""
a: Union[str, Weekday, Point]
@dataclass
class Baz(JsonSchemaMixin):
"""Type with nested default value"""
a: Point = field(default=Point(0.0, 0.0))
@dataclass
class Recursive(JsonSchemaMixin):
"""A recursive data-structure"""
a: str
b: Optional["Recursive"] = None
@dataclass
class OpaqueData(JsonSchemaMixin):
"""Structure with unknown types"""
a: List[Any]
b: Dict[str, Any]
@dataclass
class Product(JsonSchemaMixin):
name: str
cost: float = 20.0
@dataclass
class ShoppingCart(JsonSchemaMixin):
items: List[Product]
@property
def cost(self):
return sum([item.cost for item in self.items])
@dataclass
class ProductList(JsonSchemaMixin):
products: Dict[UUID, Product]
@dataclass
class Zoo(JsonSchemaMixin):
"""A zoo"""
animal_types: Optional[Dict[str, str]] = field(default_factory=dict)