7
7
import decimal
8
8
import enum
9
9
10
- from pydantic import BaseModel , fields
10
+ from pydantic import BaseModel
11
+ from pydantic .fields import Field as PydanticField
12
+
13
+ try :
14
+ # Pydantic pre-1.0
15
+ from pydantic .fields import Shape
16
+
17
+ SHAPE_SINGLETON = (Shape .SINGLETON ,)
18
+ SHAPE_SEQUENTIAL = (
19
+ Shape .LIST ,
20
+ Shape .TUPLE ,
21
+ Shape .TUPLE_ELLIPS ,
22
+ Shape .SEQUENCE ,
23
+ Shape .SET ,
24
+ )
25
+ SHAPE_MAPPING = (Shape .MAPPING ,)
26
+ except ImportError :
27
+ # Pydantic 1.0+
28
+ from pydantic import fields
29
+
30
+ SHAPE_SINGLETON = (fields .SHAPE_SINGLETON ,)
31
+ SHAPE_SEQUENTIAL = (
32
+ fields .SHAPE_LIST ,
33
+ fields .SHAPE_TUPLE ,
34
+ fields .SHAPE_TUPLE_ELLIPSIS ,
35
+ fields .SHAPE_SEQUENCE ,
36
+ fields .SHAPE_SET ,
37
+ )
38
+ SHAPE_MAPPING = (fields .SHAPE_MAPPING ,)
39
+
11
40
12
41
from graphene import Field , Boolean , Enum , Float , Int , List , String , UUID , Union
13
42
from graphene .types .base import BaseType
@@ -45,7 +74,7 @@ def _get_field(root, _info):
45
74
46
75
47
76
def convert_pydantic_field (
48
- field : fields . Field ,
77
+ field : PydanticField ,
49
78
registry : Registry ,
50
79
parent_type : T .Type = None ,
51
80
model : T .Type [BaseModel ] = None ,
@@ -75,7 +104,7 @@ def convert_pydantic_field(
75
104
76
105
def convert_pydantic_type (
77
106
type_ : T .Type ,
78
- field : fields . Field ,
107
+ field : PydanticField ,
79
108
registry : Registry = None ,
80
109
parent_type : T .Type = None ,
81
110
model : T .Type [BaseModel ] = None ,
@@ -88,24 +117,18 @@ def convert_pydantic_type(
88
117
graphene_type = find_graphene_type (
89
118
type_ , field , registry , parent_type = parent_type , model = model
90
119
)
91
- if field .shape == fields . Shape . SINGLETON :
120
+ if field .shape in SHAPE_SINGLETON :
92
121
return graphene_type
93
- elif field .shape in (
94
- fields .Shape .LIST ,
95
- fields .Shape .TUPLE ,
96
- fields .Shape .TUPLE_ELLIPS ,
97
- fields .Shape .SEQUENCE ,
98
- fields .Shape .SET ,
99
- ):
122
+ elif field .shape in SHAPE_SEQUENTIAL :
100
123
# TODO: _should_ Sets remain here?
101
124
return List (graphene_type )
102
- elif field .shape == fields . Shape . MAPPING :
125
+ elif field .shape in SHAPE_MAPPING :
103
126
raise ConversionError (f"Don't know how to handle mappings in Graphene." )
104
127
105
128
106
129
def find_graphene_type (
107
130
type_ : T .Type ,
108
- field : fields . Field ,
131
+ field : PydanticField ,
109
132
registry : Registry = None ,
110
133
parent_type : T .Type = None ,
111
134
model : T .Type [BaseModel ] = None ,
@@ -178,7 +201,7 @@ def find_graphene_type(
178
201
179
202
def convert_generic_python_type (
180
203
type_ : T .Type ,
181
- field : fields . Field ,
204
+ field : PydanticField ,
182
205
registry : Registry = None ,
183
206
parent_type : T .Type = None ,
184
207
model : T .Type [BaseModel ] = None ,
@@ -231,7 +254,7 @@ def convert_generic_python_type(
231
254
232
255
def convert_union_type (
233
256
type_ : T .Type ,
234
- field : fields . Field ,
257
+ field : PydanticField ,
235
258
registry : Registry = None ,
236
259
parent_type : T .Type = None ,
237
260
model : T .Type [BaseModel ] = None ,
0 commit comments