File tree 9 files changed +32
-26
lines changed
9 files changed +32
-26
lines changed Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -22,7 +22,10 @@ def convert_object_schema_to_class_def(
22
22
)
23
23
24
24
for k , v in schema .properties .items ():
25
- if v .type == "boolean" :
25
+ if isinstance (v , json_schema .types .Ref ):
26
+ ref_name = v .ref .split ("#" )[- 1 ]
27
+ type_value = ast .Name (id = ref_name )
28
+ elif v .type == "boolean" :
26
29
type_value = Name .bool
27
30
elif v .type == "integer" :
28
31
type_value = Name .int
Original file line number Diff line number Diff line change 6
6
BooleanSchema ,
7
7
IntegerSchema ,
8
8
ObjectSchema ,
9
+ Ref ,
9
10
StringSchema ,
10
11
)
11
12
from .class_def import convert_object_schema_to_class_def
@@ -20,19 +21,22 @@ def test_all_types(self) -> None:
20
21
schema = ObjectSchema (
21
22
id = "#Person" ,
22
23
properties = {
23
- "first_name" : StringSchema (),
24
- "last_name" : StringSchema (),
25
- "age" : IntegerSchema (),
26
- "is_active" : BooleanSchema (),
24
+ "first_name" : StringSchema (type = "string" ),
25
+ "last_name" : StringSchema (type = "string" ),
26
+ "age" : IntegerSchema (type = "integer" ),
27
+ "is_active" : BooleanSchema (type = "boolean" ),
28
+ "car" : Ref (ref = "Car" ),
27
29
},
28
- required = ["first_name" ]
30
+ required = ["first_name" ],
31
+ type = "object" ,
29
32
)
30
33
31
34
assert self ._get_class_str (schema ) == textwrap .dedent ("""\
32
35
class Person(TypedDict):
33
36
first_name: str
34
37
last_name: NotRequired[str]
35
38
age: NotRequired[int]
36
- is_active: NotRequired[bool]"""
39
+ is_active: NotRequired[bool]
40
+ car: NotRequired[Car]"""
37
41
)
38
42
Original file line number Diff line number Diff line change @@ -8,6 +8,5 @@ def _load_json_schema(path: str) -> ObjectSchema:
8
8
9
9
def load_schemas (path : str ) -> list [Schema ]:
10
10
full_schema = _load_json_schema (path )
11
- # breakpoint()
12
11
13
12
return [schema for schema in full_schema .properties .values ()]
Original file line number Diff line number Diff line change @@ -14,24 +14,31 @@ class _BaseSchema(pydantic.BaseModel):
14
14
15
15
class BooleanSchema (_BaseSchema ):
16
16
id : None = None
17
- type : Literal ["boolean" ] = "boolean"
17
+ type : Literal ["boolean" ]
18
18
19
19
class IntegerSchema (_BaseSchema ):
20
20
id : None = None
21
- type : Literal ["integer" ] = "integer"
21
+ type : Literal ["integer" ]
22
22
23
23
24
24
class ObjectSchema (_BaseSchema ):
25
25
properties : dict [str , Schema ]
26
26
required : list [str ] = []
27
- type : Literal ["object" ] = "object"
27
+ type : Literal ["object" ]
28
28
29
29
30
30
class StringSchema (_BaseSchema ):
31
31
id : None = None
32
- type : Literal ["string" ] = "string"
32
+ type : Literal ["string" ]
33
33
34
34
35
- Schema = BooleanSchema | IntegerSchema | ObjectSchema | StringSchema
35
+ class Ref (pydantic .BaseModel ):
36
+ ref : str = pydantic .Field (alias = '$ref' )
37
+
38
+ class Config :
39
+ allow_population_by_field_name = True
40
+
41
+
42
+ Schema = Ref | BooleanSchema | IntegerSchema | ObjectSchema | StringSchema
36
43
37
44
ObjectSchema .update_forward_refs ()
Original file line number Diff line number Diff line change
1
+ [mypy]
2
+ plugins = pydantic.mypy
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ class Person(TypedDict):
5
5
first_name : str
6
6
last_name : NotRequired [str ]
7
7
age : NotRequired [int ]
8
+ car : NotRequired [Car ]
8
9
9
10
class Car (TypedDict ):
10
11
color : str
Original file line number Diff line number Diff line change 18
18
"description" : " Age in years which must be equal to or greater than zero." ,
19
19
"type" : " integer" ,
20
20
"minimum" : 0
21
+ },
22
+ "car" : {
23
+ "$ref" : " #Car"
21
24
}
22
25
},
23
26
"required" : [" first_name" ]
You can’t perform that action at this time.
0 commit comments