/
api_draft_04.py
696 lines (644 loc) · 26.1 KB
/
api_draft_04.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
"""
The API version draft 04.
"""
import re
from typing import Literal, Union, cast
from jsonschema_gentypes import (
BuiltinType,
CombinedType,
NamedType,
NativeType,
Type,
TypeAlias,
TypedDictType,
TypeEnum,
TypeProxy,
configuration,
get_description,
get_name,
jsonschema_draft_04,
jsonschema_draft_06,
jsonschema_draft_2019_09_applicator,
jsonschema_draft_2019_09_meta_data,
jsonschema_draft_2020_12_applicator,
jsonschema_draft_2020_12_core,
jsonschema_draft_2020_12_validation,
)
from jsonschema_gentypes.api import API
class APIv4(API):
"""
JSON Schema draft 4.
"""
def enum(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_validation.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate an enum.
"""
schema.setdefault("used", set()).add("enum") # type: ignore[typeddict-item]
schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
schema,
)
return TypeEnum(
self.get_name(schema_meta_data, proposed_name),
cast(list[Union[int, float, bool, str, None]], schema["enum"]),
get_description(schema_meta_data),
)
def boolean(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate a ``bool`` annotation for a boolean object.
"""
del schema, proposed_name
return BuiltinType("bool")
def object(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate an annotation for an object, usually a TypedDict.
"""
schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
schema,
)
schema_validation = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_validation.JSONSchemaItemD2020,
],
schema,
)
std_dict = None
name = self.get_name(schema_meta_data, proposed_name)
schema.setdefault("used", set()).add("additionalProperties") # type: ignore[typeddict-item]
additional_properties = cast(
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaD2020],
schema.get("additionalProperties"),
)
if (
additional_properties is True
and self.additional_properties == configuration.ADDITIONALPROPERTIES_ALWAYS
):
std_dict = CombinedType(NativeType("Dict"), [BuiltinType("str"), NativeType("Any")])
elif isinstance(additional_properties, dict):
sub_type = self.get_type(additional_properties, f"{proposed_name} additionalProperties")
std_dict = CombinedType(NativeType("Dict"), [BuiltinType("str"), sub_type])
else:
pattern_properties = cast(
dict[
str,
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020,
],
],
schema.get("patternProperties"),
)
if pattern_properties and len(pattern_properties) == 1:
schema.setdefault("used", set()).add("patternProperties") # type: ignore[typeddict-item]
pattern_prop = list(pattern_properties.values())[0]
sub_type = self.get_type(pattern_prop, f"{proposed_name} Type")
std_dict = CombinedType(NativeType("Dict"), [BuiltinType("str"), sub_type])
schema.setdefault("used", set()).add("properties") # type: ignore[typeddict-item]
properties = cast(
dict[
str,
Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
],
schema.get("properties"),
)
proposed_name = schema_meta_data.get("title", proposed_name)
if properties:
required = set(schema_validation.get("required", []))
schema.setdefault("used", set()).add("required") # type: ignore[typeddict-item]
struct = {
prop: self.get_type(sub_schema, proposed_name + " " + prop, auto_alias=False)
for prop, sub_schema in properties.items()
}
type_: Type = TypedDictType(
name if std_dict is None else name + "Typed",
struct,
get_description(schema_meta_data) if std_dict is None else [],
required=required,
)
comments = []
if std_dict is not None:
type_ = CombinedType(NativeType("Union"), [std_dict, type_])
comments += [
"",
"WARNING: Normally the types should be a mix of each other instead of Union.",
"See: https://github.com/camptocamp/jsonschema-gentypes/issues/7",
]
type_.set_comments(comments)
return type_
if std_dict is not None:
return std_dict
return CombinedType(NativeType("Dict"), [BuiltinType("str"), NativeType("Any")])
def array(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2019_09_applicator.JSONSchemaItemD2019
],
proposed_name: str,
) -> Type:
"""
Generate a ``List[]`` annotation with the allowed types.
"""
items = schema.get("items")
if items is True:
schema.setdefault("used", set()).add("items") # type: ignore[typeddict-item]
return CombinedType(NativeType("List"), [NativeType("Any")])
elif items is False:
result = NativeType("None")
result.set_comments(["`items: false` is not supported"])
return result
elif isinstance(items, list):
schema.setdefault("used", set()).add("items") # type: ignore[typeddict-item]
schema.setdefault("used", set()).add("additionalItems") # type: ignore[typeddict-item]
additional_items = schema.get("additionalItems")
if additional_items:
items = [*items, additional_items]
inner_types = [
self.get_type(
cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020,
],
item,
),
f"{proposed_name} {nb}",
)
for nb, item in enumerate(items)
]
type_: Type = CombinedType(NativeType("Tuple"), inner_types)
if {schema.get("minItems"), schema.get("maxItems")} - {None, len(items)}:
type_.set_comments(
[
"WARNING: 'items': If list, must have minItems == maxItems.",
"See: https://json-schema.org/understanding-json-schema/"
"reference/array.html#tuple-validation",
]
)
return type_
elif items is not None:
schema.setdefault("used", set()).add("items") # type: ignore[typeddict-item]
return CombinedType(
NativeType("List"),
[
self.get_type(
cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020,
],
items,
),
proposed_name + " item",
)
],
)
else:
schema.setdefault("used", set()).add("items") # type: ignore[typeddict-item]
return CombinedType(NativeType("List"), [NativeType("Any")])
def any_of(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
sub_schemas: Union[
list[jsonschema_draft_04.JSONSchemaD4], list[jsonschema_draft_2020_12_applicator.JSONSchemaD2020]
],
proposed_name: str,
sub_name: str,
recursion: int = 0,
) -> tuple[
Type,
list[Type],
list[
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020]
],
]:
"""
Generate a ``Union`` annotation with the allowed types.
"""
if recursion > 10:
raise ValueError("Recursion limit reached")
additional_types: list[Type] = []
inner_types: list[Type] = []
inner_types_schema: list[
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020]
] = []
for index, sub_schema in enumerate(sub_schemas):
assert not isinstance(sub_schema, bool)
sub_schema = self.combined_sub_type(schema, sub_schema)
force_sub_type = "title" in sub_schema
if "allOf" in sub_schema:
type_, named_types, combined_schema = self.all_of(
sub_schema,
sub_schema["allOf"],
proposed_name + " " + sub_name,
"allof",
recursion=recursion + 1,
)
additional_types += named_types
if force_sub_type:
combined_schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
combined_schema,
)
if not isinstance(type_, NamedType):
type_ = TypeAlias(
self.get_name(combined_schema_meta_data, proposed_name + " " + sub_name),
type_,
[],
)
additional_types.append(type_)
inner_types.append(type_)
inner_types_schema.append(combined_schema)
elif (
"anyOf" in sub_schema
and self.significative_sub_type(sub_schema["anyOf"])
or "oneOf" in sub_schema
and self.significative_sub_type(sub_schema["oneOf"])
):
kind: Literal["anyOf", "oneOf"] = "anyOf" if "anyOf" in sub_schema else "oneOf"
sub_schema = self.combined_sub_type(schema, sub_schema)
type_, named_types, combined_schemas = self.any_of(
sub_schema,
sub_schema[kind],
proposed_name + " " + sub_name,
kind,
recursion=recursion + 1,
)
additional_types += named_types
if force_sub_type:
combined_schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
combined_schemas,
)
if not isinstance(type_, NamedType):
type_ = TypeAlias(
self.get_name(combined_schema_meta_data, proposed_name + " " + sub_name),
type_,
[],
)
additional_types.append(type_)
inner_types.append(type_)
inner_types_schema += combined_schemas
else:
sub_schema = self.combined_sub_type(schema, sub_schema)
inner_types_schema.append(sub_schema)
sub_type = self.get_type(sub_schema, f"{proposed_name} {sub_name}{index}")
if force_sub_type:
additional_types.append(sub_type)
inner_types.append(sub_type)
return CombinedType(NativeType("Union"), inner_types), additional_types, inner_types_schema
def clean_schema(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
) -> Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020]:
"""Remove the properties that could not be combined with an other type."""
return { # type: ignore[return-value]
k: v
for k, v in schema.items()
if k not in ["title", "description", "example", "allOf", "anyOf", "oneOf"]
}
def combined_sub_type(
self,
base_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
sub_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
) -> Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020]:
"""Create a sub schema with the elements from the base schema."""
combined_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
] = {}
combined_schema.update(self.clean_schema(base_schema)) # type: ignore[typeddict-item]
combined_schema.update(sub_schema) # type: ignore[typeddict-item]
return combined_schema
def combined_base_type_all_of(
self,
base_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
sub_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaD2020
],
) -> Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020]:
"""Create a sub schema with the elements from the base schema."""
assert not isinstance(sub_schema, bool)
base_schema_validation = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_validation.JSONSchemaItemD2020,
],
base_schema,
)
sub_schema_validation = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_validation.JSONSchemaItemD2020,
],
sub_schema,
)
combined_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
] = {}
combined_schema_validation = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2020_12_validation.JSONSchemaItemD2020,
],
combined_schema,
)
combined_schema.update(base_schema) # type: ignore[typeddict-item]
combined_schema.update(self.clean_schema(sub_schema)) # type: ignore[typeddict-item]
if "properties" in base_schema and "properties" in sub_schema:
base_schema.setdefault("used", set()).add("properties") # type: ignore[typeddict-item]
sub_schema.setdefault("used", set()).add("properties") # type: ignore[typeddict-item]
combined_schema["properties"] = {
**base_schema["properties"], # type: ignore[dict-item]
**sub_schema["properties"], # type: ignore[dict-item]
}
if "required" in base_schema and "required" in sub_schema_validation:
combined_schema_validation["required"] = list(
{
*base_schema_validation["required"],
*sub_schema_validation["required"],
}
)
if "type" in base_schema and "type" in sub_schema_validation:
base_type = (
base_schema_validation["type"]
if isinstance(base_schema_validation["type"], list)
else [base_schema_validation["type"]]
)
sub_type = (
sub_schema_validation["type"]
if isinstance(sub_schema_validation["type"], list)
else [sub_schema_validation["type"]]
)
combined_schema_validation["type"] = [t for t in base_type if t in sub_type]
return combined_schema
def all_of(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
sub_schemas: Union[
list[jsonschema_draft_04.JSONSchemaD4], list[jsonschema_draft_2020_12_applicator.JSONSchemaD2020]
],
proposed_name: str,
sub_name: str,
recursion: int = 0,
) -> tuple[
Type,
list[Type],
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020],
]:
"""
Combine all the definitions.
"""
if recursion > 10:
raise ValueError("Recursion limit reached")
additional_types: list[Type] = []
all_schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
] = {}
all_schema.update(schema) # type: ignore[typeddict-item]
for prop in ["allOf", "anyOf", "oneOf"]:
if prop in all_schema:
del all_schema[prop] # type: ignore[misc]
for index, new_schema in enumerate(sub_schemas):
assert not isinstance(new_schema, bool)
new_schema = self.resolve_ref(new_schema)
force_sub_type = "title" in new_schema
if "allOf" in new_schema and self.significative_sub_type(new_schema["allOf"]):
type_, named_types, combined_schema = self.all_of(
self.combined_sub_type(schema, new_schema),
new_schema["allOf"],
f"{proposed_name} {sub_name}{index}",
"allof",
recursion=recursion + 1,
)
additional_types += named_types
if force_sub_type:
combined_schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
combined_schema,
)
if not isinstance(type_, NamedType):
type_ = TypeAlias(
self.get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"),
type_,
[],
)
additional_types.append(type_)
all_schema = self.combined_base_type_all_of(all_schema, combined_schema)
elif "anyOf" in new_schema or "oneOf" in new_schema:
kind_name = "anyOf" if "anyOf" in new_schema else "oneOf"
type_, named_types, combined_schemas = self.any_of(
self.combined_sub_type(schema, new_schema),
new_schema[kind_name], # type: ignore[literal-required]
f"{proposed_name} {sub_name}{index}",
kind_name,
recursion=recursion + 1,
)
additional_types += named_types
if force_sub_type:
combined_schema_meta_data = cast(
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
],
combined_schemas,
)
if not isinstance(type_, NamedType):
type_ = TypeAlias(
self.get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"),
type_,
[],
)
additional_types.append(type_)
for combined_schema in combined_schemas:
all_schema = self.combined_base_type_all_of(all_schema, combined_schema)
else:
combined_schema = self.combined_sub_type(schema, new_schema)
if force_sub_type:
additional_types.append(
self.get_type(combined_schema, f"{proposed_name} {sub_name}{index}"),
)
all_schema = self.combined_base_type_all_of(all_schema, combined_schema)
type_ = self.get_type(
cast(
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaD2020],
all_schema,
),
proposed_name,
)
return type_, additional_types, all_schema
def ref_to_proposed_name(self, ref: str) -> str:
"""
Create a proposed name from a ref.
Change the case from camel case to standard case, this is for layer inverse conversion then don't lost the case.
"""
ref_proposed_name = ref
if ref.startswith("#/definitions/"):
ref_proposed_name = ref[len("#/definitions/") :]
elif ref.startswith("#/"):
ref_proposed_name = ref[len("#/") :]
if "/" in ref_proposed_name:
ref_proposed_name = ref_proposed_name.replace("/", " ")
else:
if re.search("[a-z]", ref_proposed_name):
ref_proposed_name = re.sub("([a-z0-9])([A-Z])", r"\1 \2", ref_proposed_name).lower()
return ref_proposed_name
def ref(
self,
schema: Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_core.JSONSchemaItemD2020],
proposed_name: str,
) -> Type:
"""
Handle a `$ref`.
"""
# ref is not correctly declared in draft 4.
schema_casted = cast(
Union[jsonschema_draft_06.JSONSchemaItemD6, jsonschema_draft_2020_12_core.JSONSchemaItemD2020],
schema,
)
ref = schema_casted["$ref"]
schema.setdefault("used", set()).add("$ref") # type: ignore[typeddict-item]
if ref == "#": # Self ref.
# Per @ilevkivskyi:
#
# > You should never use ForwardRef manually
# > Also it is deprecated and will be removed soon
# > Support for recursive types is limited to proper classes
# > currently
#
# forward_ref = ForwardRef(UnboundType(self.outer_name))
# self.forward_refs.append(forward_ref)
# return forward_ref
assert self.root is not None
return self.root
if ref in self.ref_type:
return self.ref_type[ref]
resolved = self.resolver.lookup(ref)
proxy = TypeProxy()
self.ref_type[ref] = proxy
type_ = self.get_type(
cast(
Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaD2020],
resolved,
),
self.ref_to_proposed_name(ref),
)
proxy.set_type(type_)
self.ref_type[ref] = type_
return type_
def string(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate a ``str`` annotation.
"""
del schema, proposed_name
return BuiltinType("str")
def number(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate a ``Union[int, float]`` annotation.
"""
del schema, proposed_name
return CombinedType(NativeType("Union"), [BuiltinType("int"), BuiltinType("float")])
def integer(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate an ``int`` annotation.
"""
del schema, proposed_name
return BuiltinType("int")
def null(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaItemD2020
],
proposed_name: str,
) -> Type:
"""
Generate an ``None`` annotation.
"""
del schema, proposed_name
return BuiltinType("None")
def default(
self,
schema: Union[
jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019
],
proposed_name: str,
) -> Type:
"""
Treat the default keyword.
See: https://json-schema.org/understanding-json-schema/reference/generic.html
"""
type_: Type = NativeType("Any")
for test_type, type_name in [
(str, "str"),
(int, "int"),
(float, "float"),
(bool, "bool"),
]:
if isinstance(schema["default"], test_type):
type_ = BuiltinType(type_name)
return type_