1
1
import ast
2
- from typing import Any , Dict , List , Optional , Union
2
+ import sys
3
+ from typing import Any , Dict , List , Optional , Union , cast
3
4
4
5
from graphql import (
5
6
GraphQLEnumType ,
24
25
from .exceptions import ParsingError
25
26
26
27
27
- def generate_import (names : List [str ], level : int = 0 ) -> ast .Import :
28
- """Generate import statement."""
29
- return ast .Import (names = [ast .alias (n ) for n in names ], level = level )
30
-
31
-
32
28
def generate_import_from (
33
29
names : List [str ], from_ : Optional [str ] = None , level : int = 0
34
30
) -> ast .ImportFrom :
@@ -94,17 +90,21 @@ def generate_async_method_definition(
94
90
return_type : Union [ast .Name , ast .Subscript ],
95
91
body : Optional [List [ast .stmt ]] = None ,
96
92
lineno : int = 1 ,
97
- decorator_list : Optional [List [ast .Name ]] = None ,
93
+ decorator_list : Optional [List [ast .expr ]] = None ,
98
94
) -> ast .AsyncFunctionDef :
99
95
"""Generate async function."""
100
- return ast .AsyncFunctionDef (
101
- name = name ,
102
- args = arguments ,
103
- body = body if body else [ast .Pass ()],
104
- decorator_list = decorator_list if decorator_list else [],
105
- returns = return_type ,
106
- lineno = lineno ,
107
- )
96
+ params : Dict [str , Any ] = {
97
+ "name" : name ,
98
+ "args" : arguments ,
99
+ "body" : body if body else [ast .Pass ()],
100
+ "decorator_list" : decorator_list if decorator_list else [],
101
+ "returns" : return_type ,
102
+ "lineno" : lineno ,
103
+ }
104
+ if sys .version_info >= (3 , 12 ):
105
+ params ["type_params" ] = []
106
+
107
+ return ast .AsyncFunctionDef (** params )
108
108
109
109
110
110
def generate_class_def (
@@ -113,14 +113,20 @@ def generate_class_def(
113
113
body : Optional [List [ast .stmt ]] = None ,
114
114
) -> ast .ClassDef :
115
115
"""Generate class definition."""
116
- bases = [ast .Name (id = name ) for name in base_names ] if base_names else []
117
- return ast .ClassDef (
118
- name = name ,
119
- bases = bases ,
120
- keywords = [],
121
- body = body if body else [],
122
- decorator_list = [],
116
+ bases = cast (
117
+ List [ast .expr ], [ast .Name (id = name ) for name in base_names ] if base_names else []
123
118
)
119
+ params : Dict [str , Any ] = {
120
+ "name" : name ,
121
+ "bases" : bases ,
122
+ "keywords" : [],
123
+ "body" : body if body else [],
124
+ "decorator_list" : [],
125
+ }
126
+ if sys .version_info >= (3 , 12 ):
127
+ params ["type_params" ] = []
128
+
129
+ return ast .ClassDef (** params )
124
130
125
131
126
132
def generate_name (name : str ) -> ast .Name :
@@ -153,37 +159,39 @@ def generate_assign(
153
159
) -> ast .Assign :
154
160
"""Generate assign object."""
155
161
return ast .Assign (
156
- targets = [ast .Name (t ) for t in targets ], value = value , lineno = lineno
162
+ targets = [ast .Name (t ) for t in targets ],
163
+ value = value , # type:ignore
164
+ lineno = lineno ,
157
165
)
158
166
159
167
160
168
def generate_ann_assign (
161
- target : Union [str , ast .expr ],
169
+ target : Union [ast . Name , ast .Attribute , ast . Subscript ],
162
170
annotation : Annotation ,
163
171
value : Optional [ast .expr ] = None ,
164
172
lineno : int = 1 ,
165
173
) -> ast .AnnAssign :
166
174
"""Generate ann assign object."""
167
175
return ast .AnnAssign (
168
- target = target if isinstance ( target , ast . expr ) else ast . Name ( id = target ) ,
176
+ target = target ,
169
177
annotation = annotation ,
170
- simple = 1 ,
171
178
value = value ,
179
+ simple = 1 ,
172
180
lineno = lineno ,
173
181
)
174
182
175
183
176
184
def generate_union_annotation (
177
- types : List [Union [ ast .Name , ast . Subscript ] ], nullable : bool = True
185
+ types : List [ast .expr ], nullable : bool = True
178
186
) -> ast .Subscript :
179
187
"""Generate union annotation."""
180
188
result = ast .Subscript (value = ast .Name (id = UNION ), slice = ast .Tuple (elts = types ))
181
189
return result if not nullable else generate_nullable_annotation (result )
182
190
183
191
184
192
def generate_dict (
185
- keys : Optional [List [ast .expr ]] = None ,
186
- values : Optional [List [Optional [ ast .expr ] ]] = None ,
193
+ keys : Optional [List [Optional [ ast .expr ] ]] = None ,
194
+ values : Optional [List [ast .expr ]] = None ,
187
195
) -> ast .Dict :
188
196
"""Generate dict object."""
189
197
return ast .Dict (keys = keys if keys else [], values = values if values else [])
@@ -201,7 +209,9 @@ def generate_call(
201
209
) -> ast .Call :
202
210
"""Generate call object."""
203
211
return ast .Call (
204
- func = func , args = args if args else [], keywords = keywords if keywords else []
212
+ func = func ,
213
+ args = args if args else [], # type:ignore
214
+ keywords = keywords if keywords else [],
205
215
)
206
216
207
217
@@ -240,7 +250,10 @@ def parse_field_type(
240
250
return generate_annotation_name ('"' + type_ .name + '"' , nullable )
241
251
242
252
if isinstance (type_ , GraphQLUnionType ):
243
- subtypes = [parse_field_type (subtype , False ) for subtype in type_ .types ]
253
+ subtypes = cast (
254
+ List [ast .expr ],
255
+ [parse_field_type (subtype , False ) for subtype in type_ .types ],
256
+ )
244
257
return generate_union_annotation (subtypes , nullable )
245
258
246
259
if isinstance (type_ , GraphQLList ):
@@ -255,7 +268,7 @@ def parse_field_type(
255
268
256
269
257
270
def generate_method_call (
258
- object_name : str , method_name : str , args : Optional [List [Optional [ ast .expr ] ]] = None
271
+ object_name : str , method_name : str , args : Optional [List [ast .expr ]] = None
259
272
) -> ast .Call :
260
273
"""Generate object`s method call."""
261
274
return ast .Call (
@@ -287,7 +300,7 @@ def generate_trivial_lambda(name: str, argument_name: str) -> ast.Assign:
287
300
)
288
301
289
302
290
- def generate_list (elements : List [Optional [ ast .expr ] ]) -> ast .List :
303
+ def generate_list (elements : List [ast .expr ]) -> ast .List :
291
304
"""Generate list object."""
292
305
return ast .List (elts = elements )
293
306
@@ -343,16 +356,20 @@ def generate_method_definition(
343
356
return_type : Union [ast .Name , ast .Subscript ],
344
357
body : Optional [List [ast .stmt ]] = None ,
345
358
lineno : int = 1 ,
346
- decorator_list : Optional [List [ast .Name ]] = None ,
359
+ decorator_list : Optional [List [ast .expr ]] = None ,
347
360
) -> ast .FunctionDef :
348
- return ast .FunctionDef (
349
- name = name ,
350
- args = arguments ,
351
- body = body if body else [ast .Pass ()],
352
- decorator_list = decorator_list if decorator_list else [],
353
- returns = return_type ,
354
- lineno = lineno ,
355
- )
361
+ params : Dict [str , Any ] = {
362
+ "name" : name ,
363
+ "args" : arguments ,
364
+ "body" : body if body else [ast .Pass ()],
365
+ "decorator_list" : decorator_list if decorator_list else [],
366
+ "returns" : return_type ,
367
+ "lineno" : lineno ,
368
+ }
369
+ if sys .version_info >= (3 , 12 ):
370
+ params ["type_params" ] = []
371
+
372
+ return ast .FunctionDef (** params )
356
373
357
374
358
375
def generate_async_for (
0 commit comments