forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverser.py
430 lines (338 loc) · 12.9 KB
/
traverser.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
"""Generic node traverser visitor"""
from typing import List, Tuple
from mypy_extensions import mypyc_attr
from mypy.patterns import (
AsPattern, OrPattern, ValuePattern, SequencePattern, StarredPattern, MappingPattern,
ClassPattern
)
from mypy.visitor import NodeVisitor
from mypy.nodes import (
Block, MypyFile, FuncBase, FuncItem, CallExpr, ClassDef, Decorator, FuncDef,
ExpressionStmt, AssignmentStmt, OperatorAssignmentStmt, WhileStmt,
ForStmt, ReturnStmt, AssertStmt, DelStmt, IfStmt, RaiseStmt,
TryStmt, WithStmt, MatchStmt, NameExpr, MemberExpr, OpExpr, SliceExpr, CastExpr,
RevealExpr, UnaryExpr, ListExpr, TupleExpr, DictExpr, SetExpr, IndexExpr, AssignmentExpr,
GeneratorExpr, ListComprehension, SetComprehension, DictionaryComprehension,
ConditionalExpr, TypeApplication, ExecStmt, Import, ImportFrom,
LambdaExpr, ComparisonExpr, OverloadedFuncDef, YieldFromExpr,
YieldExpr, StarExpr, BackquoteExpr, AwaitExpr, PrintStmt, SuperExpr, Node, REVEAL_TYPE,
)
@mypyc_attr(allow_interpreted_subclasses=True)
class TraverserVisitor(NodeVisitor[None]):
"""A parse tree visitor that traverses the parse tree during visiting.
It does not perform any actions outside the traversal. Subclasses
should override visit methods to perform actions during
traversal. Calling the superclass method allows reusing the
traversal implementation.
"""
def __init__(self) -> None:
pass
# Visit methods
def visit_mypy_file(self, o: MypyFile) -> None:
for d in o.defs:
d.accept(self)
def visit_block(self, block: Block) -> None:
for s in block.body:
s.accept(self)
def visit_func(self, o: FuncItem) -> None:
if o.arguments is not None:
for arg in o.arguments:
init = arg.initializer
if init is not None:
init.accept(self)
for arg in o.arguments:
self.visit_var(arg.variable)
o.body.accept(self)
def visit_func_def(self, o: FuncDef) -> None:
self.visit_func(o)
def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
for item in o.items:
item.accept(self)
if o.impl:
o.impl.accept(self)
def visit_class_def(self, o: ClassDef) -> None:
for d in o.decorators:
d.accept(self)
for base in o.base_type_exprs:
base.accept(self)
if o.metaclass:
o.metaclass.accept(self)
for v in o.keywords.values():
v.accept(self)
o.defs.accept(self)
if o.analyzed:
o.analyzed.accept(self)
def visit_decorator(self, o: Decorator) -> None:
o.func.accept(self)
o.var.accept(self)
for decorator in o.decorators:
decorator.accept(self)
def visit_expression_stmt(self, o: ExpressionStmt) -> None:
o.expr.accept(self)
def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
o.rvalue.accept(self)
for l in o.lvalues:
l.accept(self)
def visit_operator_assignment_stmt(self, o: OperatorAssignmentStmt) -> None:
o.rvalue.accept(self)
o.lvalue.accept(self)
def visit_while_stmt(self, o: WhileStmt) -> None:
o.expr.accept(self)
o.body.accept(self)
if o.else_body:
o.else_body.accept(self)
def visit_for_stmt(self, o: ForStmt) -> None:
o.index.accept(self)
o.expr.accept(self)
o.body.accept(self)
if o.else_body:
o.else_body.accept(self)
def visit_return_stmt(self, o: ReturnStmt) -> None:
if o.expr is not None:
o.expr.accept(self)
def visit_assert_stmt(self, o: AssertStmt) -> None:
if o.expr is not None:
o.expr.accept(self)
if o.msg is not None:
o.msg.accept(self)
def visit_del_stmt(self, o: DelStmt) -> None:
if o.expr is not None:
o.expr.accept(self)
def visit_if_stmt(self, o: IfStmt) -> None:
for e in o.expr:
e.accept(self)
for b in o.body:
b.accept(self)
if o.else_body:
o.else_body.accept(self)
def visit_raise_stmt(self, o: RaiseStmt) -> None:
if o.expr is not None:
o.expr.accept(self)
if o.from_expr is not None:
o.from_expr.accept(self)
def visit_try_stmt(self, o: TryStmt) -> None:
o.body.accept(self)
for i in range(len(o.types)):
tp = o.types[i]
if tp is not None:
tp.accept(self)
o.handlers[i].accept(self)
for v in o.vars:
if v is not None:
v.accept(self)
if o.else_body is not None:
o.else_body.accept(self)
if o.finally_body is not None:
o.finally_body.accept(self)
def visit_with_stmt(self, o: WithStmt) -> None:
for i in range(len(o.expr)):
o.expr[i].accept(self)
targ = o.target[i]
if targ is not None:
targ.accept(self)
o.body.accept(self)
def visit_match_stmt(self, o: MatchStmt) -> None:
o.subject.accept(self)
for i in range(len(o.patterns)):
o.patterns[i].accept(self)
guard = o.guards[i]
if guard is not None:
guard.accept(self)
o.bodies[i].accept(self)
def visit_member_expr(self, o: MemberExpr) -> None:
o.expr.accept(self)
def visit_yield_from_expr(self, o: YieldFromExpr) -> None:
o.expr.accept(self)
def visit_yield_expr(self, o: YieldExpr) -> None:
if o.expr:
o.expr.accept(self)
def visit_call_expr(self, o: CallExpr) -> None:
for a in o.args:
a.accept(self)
o.callee.accept(self)
if o.analyzed:
o.analyzed.accept(self)
def visit_op_expr(self, o: OpExpr) -> None:
o.left.accept(self)
o.right.accept(self)
def visit_comparison_expr(self, o: ComparisonExpr) -> None:
for operand in o.operands:
operand.accept(self)
def visit_slice_expr(self, o: SliceExpr) -> None:
if o.begin_index is not None:
o.begin_index.accept(self)
if o.end_index is not None:
o.end_index.accept(self)
if o.stride is not None:
o.stride.accept(self)
def visit_cast_expr(self, o: CastExpr) -> None:
o.expr.accept(self)
def visit_reveal_expr(self, o: RevealExpr) -> None:
if o.kind == REVEAL_TYPE:
assert o.expr is not None
o.expr.accept(self)
else:
# RevealLocalsExpr doesn't have an inner expression
pass
def visit_assignment_expr(self, o: AssignmentExpr) -> None:
o.target.accept(self)
o.value.accept(self)
def visit_unary_expr(self, o: UnaryExpr) -> None:
o.expr.accept(self)
def visit_list_expr(self, o: ListExpr) -> None:
for item in o.items:
item.accept(self)
def visit_tuple_expr(self, o: TupleExpr) -> None:
for item in o.items:
item.accept(self)
def visit_dict_expr(self, o: DictExpr) -> None:
for k, v in o.items:
if k is not None:
k.accept(self)
v.accept(self)
def visit_set_expr(self, o: SetExpr) -> None:
for item in o.items:
item.accept(self)
def visit_index_expr(self, o: IndexExpr) -> None:
o.base.accept(self)
o.index.accept(self)
if o.analyzed:
o.analyzed.accept(self)
def visit_generator_expr(self, o: GeneratorExpr) -> None:
for index, sequence, conditions in zip(o.indices, o.sequences,
o.condlists):
sequence.accept(self)
index.accept(self)
for cond in conditions:
cond.accept(self)
o.left_expr.accept(self)
def visit_dictionary_comprehension(self, o: DictionaryComprehension) -> None:
for index, sequence, conditions in zip(o.indices, o.sequences,
o.condlists):
sequence.accept(self)
index.accept(self)
for cond in conditions:
cond.accept(self)
o.key.accept(self)
o.value.accept(self)
def visit_list_comprehension(self, o: ListComprehension) -> None:
o.generator.accept(self)
def visit_set_comprehension(self, o: SetComprehension) -> None:
o.generator.accept(self)
def visit_conditional_expr(self, o: ConditionalExpr) -> None:
o.cond.accept(self)
o.if_expr.accept(self)
o.else_expr.accept(self)
def visit_type_application(self, o: TypeApplication) -> None:
o.expr.accept(self)
def visit_lambda_expr(self, o: LambdaExpr) -> None:
self.visit_func(o)
def visit_star_expr(self, o: StarExpr) -> None:
o.expr.accept(self)
def visit_backquote_expr(self, o: BackquoteExpr) -> None:
o.expr.accept(self)
def visit_await_expr(self, o: AwaitExpr) -> None:
o.expr.accept(self)
def visit_super_expr(self, o: SuperExpr) -> None:
o.call.accept(self)
def visit_as_pattern(self, o: AsPattern) -> None:
if o.pattern is not None:
o.pattern.accept(self)
if o.name is not None:
o.name.accept(self)
def visit_or_pattern(self, o: OrPattern) -> None:
for p in o.patterns:
p.accept(self)
def visit_value_pattern(self, o: ValuePattern) -> None:
o.expr.accept(self)
def visit_sequence_pattern(self, o: SequencePattern) -> None:
for p in o.patterns:
p.accept(self)
def visit_starred_patten(self, o: StarredPattern) -> None:
if o.capture is not None:
o.capture.accept(self)
def visit_mapping_pattern(self, o: MappingPattern) -> None:
for key in o.keys:
key.accept(self)
for value in o.values:
value.accept(self)
if o.rest is not None:
o.rest.accept(self)
def visit_class_pattern(self, o: ClassPattern) -> None:
o.class_ref.accept(self)
for p in o.positionals:
p.accept(self)
for v in o.keyword_values:
v.accept(self)
def visit_import(self, o: Import) -> None:
for a in o.assignments:
a.accept(self)
def visit_import_from(self, o: ImportFrom) -> None:
for a in o.assignments:
a.accept(self)
def visit_print_stmt(self, o: PrintStmt) -> None:
for arg in o.args:
arg.accept(self)
def visit_exec_stmt(self, o: ExecStmt) -> None:
o.expr.accept(self)
if o.globals:
o.globals.accept(self)
if o.locals:
o.locals.accept(self)
class ReturnSeeker(TraverserVisitor):
def __init__(self) -> None:
self.found = False
def visit_return_stmt(self, o: ReturnStmt) -> None:
if (o.expr is None or isinstance(o.expr, NameExpr) and o.expr.name == 'None'):
return
self.found = True
def has_return_statement(fdef: FuncBase) -> bool:
"""Find if a function has a non-trivial return statement.
Plain 'return' and 'return None' don't count.
"""
seeker = ReturnSeeker()
fdef.accept(seeker)
return seeker.found
class FuncCollectorBase(TraverserVisitor):
def __init__(self) -> None:
self.inside_func = False
def visit_func_def(self, defn: FuncDef) -> None:
if not self.inside_func:
self.inside_func = True
super().visit_func_def(defn)
self.inside_func = False
class YieldSeeker(FuncCollectorBase):
def __init__(self) -> None:
super().__init__()
self.found = False
def visit_yield_expr(self, o: YieldExpr) -> None:
self.found = True
def has_yield_expression(fdef: FuncBase) -> bool:
seeker = YieldSeeker()
fdef.accept(seeker)
return seeker.found
class ReturnCollector(FuncCollectorBase):
def __init__(self) -> None:
super().__init__()
self.return_statements: List[ReturnStmt] = []
def visit_return_stmt(self, stmt: ReturnStmt) -> None:
self.return_statements.append(stmt)
def all_return_statements(node: Node) -> List[ReturnStmt]:
v = ReturnCollector()
node.accept(v)
return v.return_statements
class YieldCollector(FuncCollectorBase):
def __init__(self) -> None:
super().__init__()
self.in_assignment = False
self.yield_expressions: List[Tuple[YieldExpr, bool]] = []
def visit_assignment_stmt(self, stmt: AssignmentStmt) -> None:
self.in_assignment = True
super().visit_assignment_stmt(stmt)
self.in_assignment = False
def visit_yield_expr(self, expr: YieldExpr) -> None:
self.yield_expressions.append((expr, self.in_assignment))
def all_yield_expressions(node: Node) -> List[Tuple[YieldExpr, bool]]:
v = YieldCollector()
node.accept(v)
return v.yield_expressions