-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2type.py
executable file
·326 lines (276 loc) · 10 KB
/
json2type.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
from __future__ import annotations
import enum
import json
import sys
from collections import Counter
from dataclasses import dataclass, field
from functools import cached_property
from types import NoneType
from typing import Literal, Self, TextIO, assert_never, cast
class TypeEnum(enum.Enum):
STRING = "string"
INT = "int"
FLOAT = "float"
BOOL = "bool"
NONE = "none"
OBJECT = "object"
ARRAY = "array"
ONE_OF = "one_of"
NEVER = "never"
@classmethod
def from_type(cls, t: type) -> Self:
map = {
str: cls.STRING,
int: cls.INT,
float: cls.FLOAT,
bool: cls.BOOL,
NoneType: cls.NONE,
}
if t not in map:
raise TypeError(f"Cannot convert {t=} to {cls=}")
return map[t]
@dataclass(kw_only=True)
class TypeDef:
name: str = field(repr=False)
type: TypeEnum
def as_type_str(self) -> str:
print(repr(self))
raise NotImplementedError()
@dataclass(kw_only=True)
class LiteralTypeDef(TypeDef):
type: Literal[
TypeEnum.STRING, TypeEnum.INT, TypeEnum.FLOAT, TypeEnum.BOOL, TypeEnum.NONE
]
@classmethod
def from_type(cls, t: type, name: str) -> Self:
# Constructing a dict with a list of tuples allows pytight to infer a narrower type
# for the dict keys and values
type_map = dict(
(
(str, TypeEnum.STRING),
(int, TypeEnum.INT),
(float, TypeEnum.FLOAT),
(bool, TypeEnum.BOOL),
(NoneType, TypeEnum.NONE),
)
)
return cls(name=name, type=type_map[t])
def as_type_str(self) -> str:
map = {
TypeEnum.STRING: "str",
TypeEnum.INT: "int",
TypeEnum.FLOAT: "float",
TypeEnum.BOOL: "bool",
TypeEnum.NONE: "None",
}
return map[self.type]
@dataclass(kw_only=True)
class MaybeStringEnumDef(LiteralTypeDef):
type: Literal[TypeEnum.STRING] = field(
init=False, default=TypeEnum.STRING, repr=False
)
values: Counter[str]
def merge(self, other: MaybeStringEnumDef) -> Self:
self.values.update(other.values)
return self
def as_type_str(self) -> str:
# Treat as a Literal if:
# - There are less than 10 distinct values
if len(self.values) < 10:
return f"t.Literal[{', '.join(map(repr, self.values.keys()))}]"
return "str"
@dataclass(kw_only=True)
class ObjectDef(TypeDef):
properties: dict[str, TypeDef]
type: Literal[TypeEnum.OBJECT] = field(
init=False, default=TypeEnum.OBJECT, repr=False
)
not_required_keys: set[str] = field(default_factory=set, repr=False, init=False)
keys_statistic: Counter[str] = field(
default_factory=Counter, repr=False, init=False
)
merge_count: int = field(default=0, repr=False, init=False)
def __post_init__(self) -> None:
self.keys_statistic.update(self.keys)
def merge(self, other: ObjectDef) -> Self:
self.keys_statistic.update(other.keys)
self.merge_count += 1
for other_key, other_value in other.properties.items():
if other_key in self.properties:
self.properties[other_key] = merge_types(
other_value, self.properties[other_key]
)
else:
self.not_required_keys.add(other_key)
self.properties[other_key] = other_value
for key in self.keys:
if key not in other.keys:
self.not_required_keys.add(key)
return self
@cached_property
def keys(self) -> set[str]:
return set(self.properties.keys())
def as_type_str(self) -> str:
if not self.properties:
return "dict[str, t.Any]"
lines = list[str]()
name = self.name.removeprefix("$").replace("/", "_").strip("_").replace("*", "")
if name == "":
name = "Root"
else:
name = "".join(map(str.capitalize, name.split("_")))
name = f"{name}Dict"
lines.append(f"class {name}(t.TypedDict):")
for key, value in self.properties.items():
if key in self.not_required_keys:
lines.append(f" {key}: t.NotRequired[{value.as_type_str()}]")
else:
lines.append(f" {key}: {value.as_type_str()}")
print("\n".join(lines))
print()
return name
@dataclass(kw_only=True)
class ArrayDef(TypeDef):
items: TypeDef
type: Literal[TypeEnum.ARRAY] = field(
init=False, default=TypeEnum.ARRAY, repr=False
)
def __hash__(self) -> int:
return hash((self.name, self.type, self.items))
def as_type_str(self) -> str:
return f"list[{self.items.as_type_str()}]"
@dataclass(kw_only=True)
class OneOfDef(TypeDef):
items: list[ArrayDef | LiteralTypeDef | ObjectDef]
type: TypeEnum = field(init=False, default=TypeEnum.ONE_OF, repr=False)
@property
def needs_union(self) -> bool:
types = [item.type for item in self.items]
return TypeEnum.NONE in types and len(types) == 2
def as_type_str(self) -> str:
items = list[TypeDef]()
for item in self.items:
if isinstance(item, LiteralTypeDef) and item.type == TypeEnum.NONE:
continue
items.append(item)
if len(items) == 1:
return f"t.Optional[{items[0].as_type_str()}]"
else:
return f"Union[{', '.join([item.as_type_str() for item in items])}]"
@dataclass(kw_only=True)
class NeverDef(TypeDef):
name: str = "__never__"
type: Literal[TypeEnum.NEVER] = TypeEnum.NEVER
def as_type_str(self) -> str:
return "t.Never"
def merge_types(a: TypeDef, b: TypeDef) -> TypeDef:
if a.name != b.name and not isinstance(a, NeverDef) and not isinstance(b, NeverDef):
raise TypeError(f"Cannot merge {a.name=} and {b.name=}")
match (a, b):
case [NeverDef(), value] | [value, NeverDef()]:
return value
case [MaybeStringEnumDef(), MaybeStringEnumDef()]:
return a.merge(b)
case [LiteralTypeDef(), LiteralTypeDef()]:
if a.type == b.type:
return LiteralTypeDef(
name=a.name,
type=a.type,
)
elif {a.type, b.type} == {TypeEnum.INT, TypeEnum.FLOAT}:
return LiteralTypeDef(name=a.name, type=TypeEnum.FLOAT)
else:
return OneOfDef(items=[a, b], name=a.name)
case (
[OneOfDef() as one_of, other]
| [
other,
OneOfDef() as one_of,
]
) if not isinstance(other, OneOfDef):
if isinstance(other, MaybeStringEnumDef):
for item in one_of.items:
if isinstance(item, MaybeStringEnumDef):
item.merge(other)
return one_of
elif isinstance(other, LiteralTypeDef):
for item in one_of.items:
if item == other:
return one_of
elif isinstance(other, ObjectDef):
for item in one_of.items:
if isinstance(item, ObjectDef):
item.merge(other)
return one_of
elif isinstance(other, ArrayDef):
for item in one_of.items:
if isinstance(item, ArrayDef):
item.items = merge_types(item.items, other.items)
return one_of
elif isinstance(
other, TypeDef
): # pyright: ignore [reportUnnecessaryIsInstance]
raise NotImplementedError()
else:
assert_never(other)
return OneOfDef(items=[*one_of.items, other], name=one_of.name)
case [ObjectDef(), LiteralTypeDef()] | [LiteralTypeDef(), ObjectDef()]:
return OneOfDef(items=[a, b], name=a.name)
case [ArrayDef(), ArrayDef()]:
return ArrayDef(
name=a.name,
items=merge_types(a.items, b.items),
)
case [ObjectDef(), ObjectDef()]:
return a.merge(b)
case [OneOfDef(), OneOfDef()]:
result = a
for item in b.items:
result = merge_types(result, item)
return result
case _:
raise SystemExit(f'Cannot merge "{a.type}" and "{b.type}"')
def process_obj(obj: object, name: str = "$") -> TypeDef:
match obj:
case str():
return MaybeStringEnumDef(name=name, values=Counter([obj]))
case int() | float() | bool() | None:
return LiteralTypeDef.from_type(type(obj), name)
case dict():
return ObjectDef(
name=name,
properties={
key: process_obj(value, f"{name}/{key}")
for key, value in cast(dict[str, object], obj).items()
},
)
case list():
items = NeverDef()
for value in cast(list[object], obj):
items = merge_types(items, process_obj(value, f"{name}/*"))
return ArrayDef(
name=name,
items=items,
)
case _:
raise NotImplementedError()
def json_line_generator(input: TextIO):
for line in input:
try:
yield json.loads(line)
except json.JSONDecodeError:
print(f"Invalid JSON: {line}", file=sys.stderr)
def build_type_def(obj: TypeDef) -> str:
return obj.as_type_str()
def main() -> int:
final_type = NeverDef()
for i, line in enumerate(json_line_generator(sys.stdin)):
print("Processing line:", i, file=sys.stderr, end="\r")
final_type = merge_types(final_type, process_obj(line))
print("import typing as t")
print()
print(f"RootType = {build_type_def(final_type)}", end="")
return 0
if __name__ == "__main__":
raise SystemExit(main())