forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixedtraverser.py
112 lines (88 loc) · 3.13 KB
/
mixedtraverser.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
from __future__ import annotations
from mypy.nodes import (
AssertTypeExpr,
AssignmentStmt,
CastExpr,
ClassDef,
ForStmt,
FuncItem,
NamedTupleExpr,
NewTypeExpr,
PromoteExpr,
TypeAliasExpr,
TypeApplication,
TypedDictExpr,
TypeVarExpr,
Var,
WithStmt,
)
from mypy.traverser import TraverserVisitor
from mypy.types import Type
from mypy.typetraverser import TypeTraverserVisitor
class MixedTraverserVisitor(TraverserVisitor, TypeTraverserVisitor):
"""Recursive traversal of both Node and Type objects."""
def __init__(self) -> None:
self.in_type_alias_expr = False
# Symbol nodes
def visit_var(self, var: Var) -> None:
self.visit_optional_type(var.type)
def visit_func(self, o: FuncItem) -> None:
super().visit_func(o)
self.visit_optional_type(o.type)
def visit_class_def(self, o: ClassDef) -> None:
# TODO: Should we visit generated methods/variables as well, either here or in
# TraverserVisitor?
super().visit_class_def(o)
info = o.info
if info:
for base in info.bases:
base.accept(self)
def visit_type_alias_expr(self, o: TypeAliasExpr) -> None:
super().visit_type_alias_expr(o)
self.in_type_alias_expr = True
o.node.target.accept(self)
self.in_type_alias_expr = False
def visit_type_var_expr(self, o: TypeVarExpr) -> None:
super().visit_type_var_expr(o)
o.upper_bound.accept(self)
for value in o.values:
value.accept(self)
def visit_typeddict_expr(self, o: TypedDictExpr) -> None:
super().visit_typeddict_expr(o)
self.visit_optional_type(o.info.typeddict_type)
def visit_namedtuple_expr(self, o: NamedTupleExpr) -> None:
super().visit_namedtuple_expr(o)
assert o.info.tuple_type
o.info.tuple_type.accept(self)
def visit__promote_expr(self, o: PromoteExpr) -> None:
super().visit__promote_expr(o)
o.type.accept(self)
def visit_newtype_expr(self, o: NewTypeExpr) -> None:
super().visit_newtype_expr(o)
self.visit_optional_type(o.old_type)
# Statements
def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
super().visit_assignment_stmt(o)
self.visit_optional_type(o.type)
def visit_for_stmt(self, o: ForStmt) -> None:
super().visit_for_stmt(o)
self.visit_optional_type(o.index_type)
def visit_with_stmt(self, o: WithStmt) -> None:
super().visit_with_stmt(o)
for typ in o.analyzed_types:
typ.accept(self)
# Expressions
def visit_cast_expr(self, o: CastExpr) -> None:
super().visit_cast_expr(o)
o.type.accept(self)
def visit_assert_type_expr(self, o: AssertTypeExpr) -> None:
super().visit_assert_type_expr(o)
o.type.accept(self)
def visit_type_application(self, o: TypeApplication) -> None:
super().visit_type_application(o)
for t in o.types:
t.accept(self)
# Helpers
def visit_optional_type(self, t: Type | None) -> None:
if t:
t.accept(self)