-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subclass_tagged_union.py
211 lines (175 loc) · 5.5 KB
/
subclass_tagged_union.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from collections import defaultdict
from collections.abc import AsyncIterable, Callable, Iterator
from dataclasses import dataclass, field
from types import new_class
from typing import Annotated, Any, TypeVar, get_type_hints
import graphql
from apischema import deserializer, schema, serializer, type_name
from apischema.conversions import Conversion
from apischema.graphql import graphql_schema
from apischema.metadata import conversion
from apischema.objects import object_deserialization
from apischema.tagged_unions import Tagged, TaggedUnion, get_tagged
from apischema.utils import to_pascal_case
_alternative_constructors: dict[type, list[Callable]] = defaultdict(list)
Func = TypeVar("Func", bound=Callable)
def alternative_constructor(func: Func) -> Func:
_alternative_constructors[get_type_hints(func)["return"]].append(func)
return func
def rec_subclasses(cls: type) -> Iterator[type]:
"""Recursive implementation of type.__subclasses__"""
for sub_cls in cls.__subclasses__():
yield sub_cls
yield from rec_subclasses(sub_cls)
Cls = TypeVar("Cls", bound=type)
def as_tagged_union(cls: Cls) -> Cls:
def serialization() -> Conversion:
annotations = {sub.__name__: Tagged[sub] for sub in rec_subclasses(cls)}
namespace = {"__annotations__": annotations}
tagged_union = new_class(
cls.__name__, (TaggedUnion,), exec_body=lambda ns: ns.update(namespace)
)
return Conversion(
lambda obj: tagged_union(**{obj.__class__.__name__: obj}),
source=cls,
target=tagged_union,
# Conversion must not be inherited because it would lead to
# infinite recursion otherwise
inherited=False,
)
def deserialization() -> Conversion:
annotations: dict[str, Any] = {}
namespace: dict[str, Any] = {"__annotations__": annotations}
for sub in rec_subclasses(cls):
annotations[sub.__name__] = Tagged[sub]
# Add tagged fields for all its alternative constructors
for constructor in _alternative_constructors.get(sub, ()):
# Build the alias of the field
alias = to_pascal_case(constructor.__name__)
# object_deserialization uses get_type_hints, but the constructor
# return type is stringified and the class not defined yet,
# so it must be assigned manually
constructor.__annotations__["return"] = sub
# Use object_deserialization to wrap constructor as deserializer
deserialization = object_deserialization(constructor, type_name(alias))
# Add constructor tagged field with its conversion
annotations[alias] = Tagged[sub]
namespace[alias] = Tagged(conversion(deserialization=deserialization))
# Create the deserialization tagged union class
tagged_union = new_class(
cls.__name__, (TaggedUnion,), exec_body=lambda ns: ns.update(namespace)
)
return Conversion(
lambda obj: get_tagged(obj)[1], source=tagged_union, target=cls
)
deserializer(lazy=deserialization, target=cls)
serializer(lazy=serialization, source=cls)
return cls
@as_tagged_union
class Drawing:
def points(self) -> AsyncIterable[float]:
raise NotImplementedError
@dataclass
class Line(Drawing):
start: float
stop: float
step: float = field(default=1, metadata=schema(exc_min=0))
async def points(self) -> AsyncIterable[float]:
point = self.start
while point <= self.stop:
yield point
point += self.step
@alternative_constructor
def sized_line(
start: float, stop: float, size: Annotated[float, schema(min=1)]
) -> "Line":
return Line(start=start, stop=stop, step=(stop - start) / (size - 1))
@dataclass
class Concat(Drawing):
left: Drawing
right: Drawing
async def points(self) -> AsyncIterable[float]:
async for point in self.left.points():
yield point
async for point in self.right.points():
yield point
def echo(drawing: Drawing = None) -> Drawing | None:
return drawing
drawing_schema = graphql_schema(query=[echo])
assert (
graphql.utilities.print_schema(drawing_schema)
== """\
type Query {
echo(drawing: DrawingInput): Drawing
}
type Drawing {
Line: Line
Concat: Concat
}
type Line {
start: Float!
stop: Float!
step: Float!
}
type Concat {
left: Drawing!
right: Drawing!
}
input DrawingInput {
Line: LineInput
SizedLine: SizedLineInput
Concat: ConcatInput
}
input LineInput {
start: Float!
stop: Float!
step: Float! = 1
}
input SizedLineInput {
start: Float!
stop: Float!
size: Float!
}
input ConcatInput {
left: DrawingInput!
right: DrawingInput!
}"""
)
query = """\
{
echo(drawing: {
Concat: {
left: {
SizedLine: {
start: 0, stop: 12, size: 3
},
},
right: {
Line: {
start: 12, stop: 13
},
}
}
}) {
Concat {
left {
Line {
start stop step
}
}
right {
Line {
start stop step
}
}
}
}
}"""
assert graphql.graphql_sync(drawing_schema, query).data == {
"echo": {
"Concat": {
"left": {"Line": {"start": 0.0, "stop": 12.0, "step": 6.0}},
"right": {"Line": {"start": 12.0, "stop": 13.0, "step": 1.0}},
}
}
}