33
33
MIN_INT = - 2_147_483_648
34
34
35
35
36
- def serialize_int (value : Any ) -> int :
37
- if isinstance (value , bool ):
38
- return 1 if value else 0
36
+ def serialize_int (output_value : Any ) -> int :
37
+ if isinstance (output_value , bool ):
38
+ return 1 if output_value else 0
39
39
try :
40
- if isinstance (value , int ):
41
- num = value
42
- elif isinstance (value , float ):
43
- num = int (value )
44
- if num != value :
40
+ if isinstance (output_value , int ):
41
+ num = output_value
42
+ elif isinstance (output_value , float ):
43
+ num = int (output_value )
44
+ if num != output_value :
45
45
raise ValueError
46
- elif not value and isinstance (value , str ):
47
- value = ""
46
+ elif not output_value and isinstance (output_value , str ):
47
+ output_value = ""
48
48
raise ValueError
49
49
else :
50
- num = int (value )
51
- float_value = float (value )
50
+ num = int (output_value )
51
+ float_value = float (output_value )
52
52
if num != float_value :
53
53
raise ValueError
54
54
except (OverflowError , ValueError , TypeError ):
55
- raise GraphQLError (f"Int cannot represent non-integer value: { inspect (value )} " )
55
+ raise GraphQLError (
56
+ "Int cannot represent non-integer value: " + inspect (output_value )
57
+ )
56
58
if not MIN_INT <= num <= MAX_INT :
57
59
raise GraphQLError (
58
- f"Int cannot represent non 32-bit signed integer value: { inspect (value )} "
60
+ "Int cannot represent non 32-bit signed integer value: "
61
+ + inspect (output_value )
59
62
)
60
63
return num
61
64
62
65
63
- def coerce_int (value : Any ) -> int :
64
- if not is_integer (value ):
65
- raise GraphQLError (f"Int cannot represent non-integer value: { inspect (value )} " )
66
- if not MIN_INT <= value <= MAX_INT :
66
+ def coerce_int (input_value : Any ) -> int :
67
+ if not is_integer (input_value ):
68
+ raise GraphQLError (
69
+ "Int cannot represent non-integer value: " + inspect (input_value )
70
+ )
71
+ if not MIN_INT <= input_value <= MAX_INT :
67
72
raise GraphQLError (
68
- f"Int cannot represent non 32-bit signed integer value: { inspect (value )} "
73
+ "Int cannot represent non 32-bit signed integer value: "
74
+ + inspect (input_value )
69
75
)
70
- return int (value )
76
+ return int (input_value )
71
77
72
78
73
- def parse_int_literal (ast , _variables = None ):
79
+ def parse_int_literal (value_node , _variables = None ) -> int :
74
80
"""Parse an integer value node in the AST."""
75
- if not isinstance (ast , IntValueNode ):
81
+ if not isinstance (value_node , IntValueNode ):
76
82
raise GraphQLError (
77
- f"Int cannot represent non-integer value: { print_ast (ast )} " , ast
83
+ "Int cannot represent non-integer value: " + print_ast (value_node ),
84
+ value_node ,
78
85
)
79
- num = int (ast .value )
86
+ num = int (value_node .value )
80
87
if not MIN_INT <= num <= MAX_INT :
81
88
raise GraphQLError (
82
- f"Int cannot represent non 32-bit signed integer value: { print_ast (ast )} " ,
83
- ast ,
89
+ "Int cannot represent non 32-bit signed integer value: "
90
+ + print_ast (value_node ),
91
+ value_node ,
84
92
)
85
93
return num
86
94
@@ -96,38 +104,39 @@ def parse_int_literal(ast, _variables=None):
96
104
)
97
105
98
106
99
- def serialize_float (value : Any ) -> float :
100
- if isinstance (value , bool ):
101
- return 1 if value else 0
107
+ def serialize_float (output_value : Any ) -> float :
108
+ if isinstance (output_value , bool ):
109
+ return 1 if output_value else 0
102
110
try :
103
- if not value and isinstance (value , str ):
104
- value = ""
111
+ if not output_value and isinstance (output_value , str ):
112
+ output_value = ""
105
113
raise ValueError
106
- num = value if isinstance (value , float ) else float (value )
114
+ num = output_value if isinstance (output_value , float ) else float (output_value )
107
115
if not isfinite (num ):
108
116
raise ValueError
109
117
except (ValueError , TypeError ):
110
118
raise GraphQLError (
111
- f "Float cannot represent non numeric value: { inspect (value ) } "
119
+ "Float cannot represent non numeric value: " + inspect (output_value )
112
120
)
113
121
return num
114
122
115
123
116
- def coerce_float (value : Any ) -> float :
117
- if not is_finite (value ):
124
+ def coerce_float (input_value : Any ) -> float :
125
+ if not is_finite (input_value ):
118
126
raise GraphQLError (
119
- f "Float cannot represent non numeric value: { inspect (value ) } "
127
+ "Float cannot represent non numeric value: " + inspect (input_value )
120
128
)
121
- return float (value )
129
+ return float (input_value )
122
130
123
131
124
- def parse_float_literal (ast , _variables = None ):
132
+ def parse_float_literal (value_node , _variables = None ) -> float :
125
133
"""Parse a float value node in the AST."""
126
- if not isinstance (ast , (FloatValueNode , IntValueNode )):
134
+ if not isinstance (value_node , (FloatValueNode , IntValueNode )):
127
135
raise GraphQLError (
128
- f"Float cannot represent non numeric value: { print_ast (ast )} " , ast
136
+ "Float cannot represent non numeric value: " + print_ast (value_node ),
137
+ value_node ,
129
138
)
130
- return float (ast .value )
139
+ return float (value_node .value )
131
140
132
141
133
142
GraphQLFloat = GraphQLScalarType (
@@ -142,35 +151,36 @@ def parse_float_literal(ast, _variables=None):
142
151
)
143
152
144
153
145
- def serialize_string (value : Any ) -> str :
146
- if isinstance (value , str ):
147
- return value
148
- if isinstance (value , bool ):
149
- return "true" if value else "false"
150
- if is_finite (value ):
151
- return str (value )
154
+ def serialize_string (output_value : Any ) -> str :
155
+ if isinstance (output_value , str ):
156
+ return output_value
157
+ if isinstance (output_value , bool ):
158
+ return "true" if output_value else "false"
159
+ if is_finite (output_value ):
160
+ return str (output_value )
152
161
# do not serialize builtin types as strings, but allow serialization of custom
153
162
# types via their `__str__` method
154
- if type (value ).__module__ == "builtins" :
155
- raise GraphQLError (f "String cannot represent value: { inspect (value ) } " )
156
- return str (value )
163
+ if type (output_value ).__module__ == "builtins" :
164
+ raise GraphQLError ("String cannot represent value: " + inspect (output_value ) )
165
+ return str (output_value )
157
166
158
167
159
- def coerce_string (value : Any ) -> str :
160
- if not isinstance (value , str ):
168
+ def coerce_string (input_value : Any ) -> str :
169
+ if not isinstance (input_value , str ):
161
170
raise GraphQLError (
162
- f "String cannot represent a non string value: { inspect (value ) } "
171
+ "String cannot represent a non string value: " + inspect (input_value )
163
172
)
164
- return value
173
+ return input_value
165
174
166
175
167
- def parse_string_literal (ast , _variables = None ):
176
+ def parse_string_literal (value_node , _variables = None ) -> str :
168
177
"""Parse a string value node in the AST."""
169
- if not isinstance (ast , StringValueNode ):
178
+ if not isinstance (value_node , StringValueNode ):
170
179
raise GraphQLError (
171
- f"String cannot represent a non string value: { print_ast (ast )} " , ast
180
+ "String cannot represent a non string value: " + print_ast (value_node ),
181
+ value_node ,
172
182
)
173
- return ast .value
183
+ return value_node .value
174
184
175
185
176
186
GraphQLString = GraphQLScalarType (
@@ -185,31 +195,32 @@ def parse_string_literal(ast, _variables=None):
185
195
)
186
196
187
197
188
- def serialize_boolean (value : Any ) -> bool :
189
- if isinstance (value , bool ):
190
- return value
191
- if is_finite (value ):
192
- return bool (value )
198
+ def serialize_boolean (output_value : Any ) -> bool :
199
+ if isinstance (output_value , bool ):
200
+ return output_value
201
+ if is_finite (output_value ):
202
+ return bool (output_value )
193
203
raise GraphQLError (
194
- f "Boolean cannot represent a non boolean value: { inspect (value ) } "
204
+ "Boolean cannot represent a non boolean value: " + inspect (output_value )
195
205
)
196
206
197
207
198
- def coerce_boolean (value : Any ) -> bool :
199
- if not isinstance (value , bool ):
208
+ def coerce_boolean (input_value : Any ) -> bool :
209
+ if not isinstance (input_value , bool ):
200
210
raise GraphQLError (
201
- f "Boolean cannot represent a non boolean value: { inspect (value ) } "
211
+ "Boolean cannot represent a non boolean value: " + inspect (input_value )
202
212
)
203
- return value
213
+ return input_value
204
214
205
215
206
- def parse_boolean_literal (ast , _variables = None ):
216
+ def parse_boolean_literal (value_node , _variables = None ) -> bool :
207
217
"""Parse a boolean value node in the AST."""
208
- if not isinstance (ast , BooleanValueNode ):
218
+ if not isinstance (value_node , BooleanValueNode ):
209
219
raise GraphQLError (
210
- f"Boolean cannot represent a non boolean value: { print_ast (ast )} " , ast
220
+ "Boolean cannot represent a non boolean value: " + print_ast (value_node ),
221
+ value_node ,
211
222
)
212
- return ast .value
223
+ return value_node .value
213
224
214
225
215
226
GraphQLBoolean = GraphQLScalarType (
@@ -221,34 +232,35 @@ def parse_boolean_literal(ast, _variables=None):
221
232
)
222
233
223
234
224
- def serialize_id (value : Any ) -> str :
225
- if isinstance (value , str ):
226
- return value
227
- if is_integer (value ):
228
- return str (int (value ))
235
+ def serialize_id (output_value : Any ) -> str :
236
+ if isinstance (output_value , str ):
237
+ return output_value
238
+ if is_integer (output_value ):
239
+ return str (int (output_value ))
229
240
# do not serialize builtin types as IDs, but allow serialization of custom types
230
241
# via their `__str__` method
231
- if type (value ).__module__ == "builtins" :
232
- raise GraphQLError (f "ID cannot represent value: { inspect (value ) } " )
233
- return str (value )
242
+ if type (output_value ).__module__ == "builtins" :
243
+ raise GraphQLError ("ID cannot represent value: " + inspect (output_value ) )
244
+ return str (output_value )
234
245
235
246
236
- def coerce_id (value : Any ) -> str :
237
- if not isinstance (value , str ) and not is_integer (value ):
238
- raise GraphQLError (f "ID cannot represent value: { inspect (value ) } " )
239
- if isinstance (value , float ):
240
- value = int (value )
241
- return str (value )
247
+ def coerce_id (input_value : Any ) -> str :
248
+ if not isinstance (input_value , str ) and not is_integer (input_value ):
249
+ raise GraphQLError ("ID cannot represent value: " + inspect (input_value ) )
250
+ if isinstance (input_value , float ):
251
+ input_value = int (input_value )
252
+ return str (input_value )
242
253
243
254
244
- def parse_id_literal (ast , _variables = None ):
255
+ def parse_id_literal (value_node , _variables = None ) -> str :
245
256
"""Parse an ID value node in the AST."""
246
- if not isinstance (ast , (StringValueNode , IntValueNode )):
257
+ if not isinstance (value_node , (StringValueNode , IntValueNode )):
247
258
raise GraphQLError (
248
- f"ID cannot represent a non-string and non-integer value: { print_ast (ast )} " ,
249
- ast ,
259
+ "ID cannot represent a non-string and non-integer value: "
260
+ + print_ast (value_node ),
261
+ value_node ,
250
262
)
251
- return ast .value
263
+ return value_node .value
252
264
253
265
254
266
GraphQLID = GraphQLScalarType (
0 commit comments