Skip to content

Commit 92d5308

Browse files
authored
stdlib/3/ast: add visit_* methods to NodeVisitor (#3796)
NodeVisitor recurses over an AST tree. When encountering a node, it checks if a method called `visit_{node.__class__.__name__}` exists, and calls it if so, otherwise calls the generic visitor. Add the possible methods to NodeVisitor. This is not exactly correct, since the methods don't *actually* exist on NodeVisitor, e.g. `NodeVisitor().visit_Module(...)` doesn't work. But it's nice for subclasses to know which methods they can override and which type they should have.
1 parent 3370515 commit 92d5308

File tree

5 files changed

+230
-1
lines changed

5 files changed

+230
-1
lines changed

stdlib/3/ast.pyi

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,128 @@ else:
1919
class NodeVisitor:
2020
def visit(self, node: AST) -> Any: ...
2121
def generic_visit(self, node: AST) -> Any: ...
22-
if sys.version_info >= (3, 8):
22+
def visit_Module(self, node: Module) -> Any: ...
23+
def visit_Interactive(self, node: Interactive) -> Any: ...
24+
def visit_Expression(self, node: Expression) -> Any: ...
25+
def visit_Suite(self, node: Suite) -> Any: ...
26+
def visit_FunctionDef(self, node: FunctionDef) -> Any: ...
27+
def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> Any: ...
28+
def visit_ClassDef(self, node: ClassDef) -> Any: ...
29+
def visit_Return(self, node: Return) -> Any: ...
30+
def visit_Delete(self, node: Delete) -> Any: ...
31+
def visit_Assign(self, node: Assign) -> Any: ...
32+
def visit_AugAssign(self, node: AugAssign) -> Any: ...
33+
if sys.version_info >= (3, 6):
34+
def visit_AnnAssign(self, node: AnnAssign) -> Any: ...
35+
def visit_For(self, node: For) -> Any: ...
36+
def visit_AsyncFor(self, node: AsyncFor) -> Any: ...
37+
def visit_While(self, node: While) -> Any: ...
38+
def visit_If(self, node: If) -> Any: ...
39+
def visit_With(self, node: With) -> Any: ...
40+
def visit_AsyncWith(self, node: AsyncWith) -> Any: ...
41+
def visit_Raise(self, node: Raise) -> Any: ...
42+
def visit_Try(self, node: Try) -> Any: ...
43+
def visit_Assert(self, node: Assert) -> Any: ...
44+
def visit_Import(self, node: Import) -> Any: ...
45+
def visit_ImportFrom(self, node: ImportFrom) -> Any: ...
46+
def visit_Global(self, node: Global) -> Any: ...
47+
def visit_Nonlocal(self, node: Nonlocal) -> Any: ...
48+
def visit_Expr(self, node: Expr) -> Any: ...
49+
def visit_Pass(self, node: Pass) -> Any: ...
50+
def visit_Break(self, node: Break) -> Any: ...
51+
def visit_Continue(self, node: Continue) -> Any: ...
52+
def visit_Slice(self, node: Slice) -> Any: ...
53+
def visit_ExtSlice(self, node: ExtSlice) -> Any: ...
54+
def visit_Index(self, node: Index) -> Any: ...
55+
def visit_BoolOp(self, node: BoolOp) -> Any: ...
56+
def visit_BinOp(self, node: BinOp) -> Any: ...
57+
def visit_UnaryOp(self, node: UnaryOp) -> Any: ...
58+
def visit_Lambda(self, node: Lambda) -> Any: ...
59+
def visit_IfExp(self, node: IfExp) -> Any: ...
60+
def visit_Dict(self, node: Dict) -> Any: ...
61+
def visit_Set(self, node: Set) -> Any: ...
62+
def visit_ListComp(self, node: ListComp) -> Any: ...
63+
def visit_SetComp(self, node: SetComp) -> Any: ...
64+
def visit_DictComp(self, node: DictComp) -> Any: ...
65+
def visit_GeneratorExp(self, node: GeneratorExp) -> Any: ...
66+
def visit_Await(self, node: Await) -> Any: ...
67+
def visit_Yield(self, node: Yield) -> Any: ...
68+
def visit_YieldFrom(self, node: YieldFrom) -> Any: ...
69+
def visit_Compare(self, node: Compare) -> Any: ...
70+
def visit_Call(self, node: Call) -> Any: ...
71+
if sys.version_info >= (3, 6):
72+
def visit_FormattedValue(self, node: FormattedValue) -> Any: ...
73+
def visit_JoinedStr(self, node: JoinedStr) -> Any: ...
74+
if sys.version_info < (3, 8):
75+
def visit_Num(self, node: Num) -> Any: ...
76+
def visit_Str(self, node: Str) -> Any: ...
77+
def visit_Bytes(self, node: Bytes) -> Any: ...
78+
def visit_NameConstant(self, node: NameConstant) -> Any: ...
79+
def visit_Ellipsis(self, node: Ellipsis) -> Any: ...
80+
else:
81+
def visit_Num(self, node: Constant) -> Any: ...
82+
def visit_Str(self, node: Constant) -> Any: ...
83+
def visit_Bytes(self, node: Constant) -> Any: ...
84+
def visit_NameConstant(self, node: Constant) -> Any: ...
85+
def visit_Ellipsis(self, node: Constant) -> Any: ...
86+
if sys.version_info >= (3, 6):
2387
def visit_Constant(self, node: Constant) -> Any: ...
88+
if sys.version_info >= (3, 8):
89+
def visit_NamedExpr(self, node: NamedExpr) -> Any: ...
90+
def visit_Attribute(self, node: Attribute) -> Any: ...
91+
def visit_Subscript(self, node: Subscript) -> Any: ...
92+
def visit_Starred(self, node: Starred) -> Any: ...
93+
def visit_Name(self, node: Name) -> Any: ...
94+
def visit_List(self, node: List) -> Any: ...
95+
def visit_Tuple(self, node: Tuple) -> Any: ...
96+
def visit_AugLoad(self, node: AugLoad) -> Any: ...
97+
def visit_AugStore(self, node: AugStore) -> Any: ...
98+
def visit_Del(self, node: Del) -> Any: ...
99+
def visit_Load(self, node: Load) -> Any: ...
100+
def visit_Param(self, node: Param) -> Any: ...
101+
def visit_Store(self, node: Store) -> Any: ...
102+
def visit_And(self, node: And) -> Any: ...
103+
def visit_Or(self, node: Or) -> Any: ...
104+
def visit_Add(self, node: Add) -> Any: ...
105+
def visit_BitAnd(self, node: BitAnd) -> Any: ...
106+
def visit_BitOr(self, node: BitOr) -> Any: ...
107+
def visit_BitXor(self, node: BitXor) -> Any: ...
108+
def visit_Div(self, node: Div) -> Any: ...
109+
def visit_FloorDiv(self, node: FloorDiv) -> Any: ...
110+
def visit_LShift(self, node: LShift) -> Any: ...
111+
def visit_Mod(self, node: Mod) -> Any: ...
112+
def visit_Mult(self, node: Mult) -> Any: ...
113+
def visit_MatMult(self, node: MatMult) -> Any: ...
114+
def visit_Pow(self, node: Pow) -> Any: ...
115+
def visit_RShift(self, node: RShift) -> Any: ...
116+
def visit_Sub(self, node: Sub) -> Any: ...
117+
def visit_Invert(self, node: Invert) -> Any: ...
118+
def visit_Not(self, node: Not) -> Any: ...
119+
def visit_UAdd(self, node: UAdd) -> Any: ...
120+
def visit_USub(self, node: USub) -> Any: ...
121+
def visit_Eq(self, node: Eq) -> Any: ...
122+
def visit_Gt(self, node: Gt) -> Any: ...
123+
def visit_GtE(self, node: GtE) -> Any: ...
124+
def visit_In(self, node: In) -> Any: ...
125+
def visit_Is(self, node: Is) -> Any: ...
126+
def visit_IsNot(self, node: IsNot) -> Any: ...
127+
def visit_Lt(self, node: Lt) -> Any: ...
128+
def visit_LtE(self, node: LtE) -> Any: ...
129+
def visit_NotEq(self, node: NotEq) -> Any: ...
130+
def visit_NotIn(self, node: NotIn) -> Any: ...
131+
def visit_comprehension(self, node: comprehension) -> Any: ...
132+
def visit_ExceptHandler(self, node: ExceptHandler) -> Any: ...
133+
def visit_arguments(self, node: arguments) -> Any: ...
134+
def visit_arg(self, node: arg) -> Any: ...
135+
def visit_keyword(self, node: keyword) -> Any: ...
136+
def visit_alias(self, node: alias) -> Any: ...
137+
def visit_withitem(self, node: withitem) -> Any: ...
24138

25139
class NodeTransformer(NodeVisitor):
26140
def generic_visit(self, node: AST) -> Optional[AST]: ...
141+
# TODO: Override the visit_* methods with better return types.
142+
# The usual return type is Optional[AST], but Iterable[AST]
143+
# is also allowed in some cases -- this needs to be mapped.
27144

28145
_T = TypeVar("_T", bound=AST)
29146

tests/stubtest_whitelists/py36.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
ast.NodeVisitor.visit_AnnAssign
2+
ast.NodeVisitor.visit_Constant
3+
ast.NodeVisitor.visit_FormattedValue
4+
ast.NodeVisitor.visit_JoinedStr
15
asyncio.Future.__init__
26
asyncio.Task._wakeup
37
asyncio.constants.SENDFILE_FALLBACK_READBUFFER_SIZE

tests/stubtest_whitelists/py37.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ _bisect.bisect
22
_bisect.insort
33
_tracemalloc._get_object_traceback
44
_tracemalloc.start
5+
ast.NodeVisitor.visit_AnnAssign
6+
ast.NodeVisitor.visit_Constant
7+
ast.NodeVisitor.visit_FormattedValue
8+
ast.NodeVisitor.visit_JoinedStr
59
asyncio.AbstractEventLoop.create_unix_server
610
asyncio.AbstractEventLoop.sock_sendfile
711
asyncio.Future.__init__

tests/stubtest_whitelists/py38.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ _thread._ExceptHookArgs
55
_tracemalloc._get_object_traceback
66
_tracemalloc.start
77
_weakref.getweakrefcount
8+
ast.NodeVisitor.visit_AnnAssign
9+
ast.NodeVisitor.visit_FormattedValue
10+
ast.NodeVisitor.visit_JoinedStr
11+
ast.NodeVisitor.visit_NamedExpr
812
asyncio.AbstractEventLoop.create_unix_server
913
asyncio.AbstractEventLoop.sock_sendfile
1014
asyncio.Future.__init__

tests/stubtest_whitelists/py3_common.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,106 @@ abc.abstractstaticmethod
1515
aifc.open
1616
aifc.openfp
1717
argparse.Namespace.__getattr__
18+
ast.NodeVisitor.visit_Add
19+
ast.NodeVisitor.visit_And
20+
ast.NodeVisitor.visit_Assert
21+
ast.NodeVisitor.visit_Assign
22+
ast.NodeVisitor.visit_AsyncFor
23+
ast.NodeVisitor.visit_AsyncFunctionDef
24+
ast.NodeVisitor.visit_AsyncWith
25+
ast.NodeVisitor.visit_Attribute
26+
ast.NodeVisitor.visit_AugAssign
27+
ast.NodeVisitor.visit_AugLoad
28+
ast.NodeVisitor.visit_AugStore
29+
ast.NodeVisitor.visit_Await
30+
ast.NodeVisitor.visit_BinOp
31+
ast.NodeVisitor.visit_BitAnd
32+
ast.NodeVisitor.visit_BitOr
33+
ast.NodeVisitor.visit_BitXor
34+
ast.NodeVisitor.visit_BoolOp
35+
ast.NodeVisitor.visit_Break
36+
ast.NodeVisitor.visit_Bytes
37+
ast.NodeVisitor.visit_Call
38+
ast.NodeVisitor.visit_ClassDef
39+
ast.NodeVisitor.visit_Compare
40+
ast.NodeVisitor.visit_Continue
41+
ast.NodeVisitor.visit_Del
42+
ast.NodeVisitor.visit_Delete
43+
ast.NodeVisitor.visit_Dict
44+
ast.NodeVisitor.visit_DictComp
45+
ast.NodeVisitor.visit_Div
46+
ast.NodeVisitor.visit_Ellipsis
47+
ast.NodeVisitor.visit_Eq
48+
ast.NodeVisitor.visit_ExceptHandler
49+
ast.NodeVisitor.visit_Expr
50+
ast.NodeVisitor.visit_Expression
51+
ast.NodeVisitor.visit_ExtSlice
52+
ast.NodeVisitor.visit_FloorDiv
53+
ast.NodeVisitor.visit_For
54+
ast.NodeVisitor.visit_FunctionDef
55+
ast.NodeVisitor.visit_GeneratorExp
56+
ast.NodeVisitor.visit_Global
57+
ast.NodeVisitor.visit_Gt
58+
ast.NodeVisitor.visit_GtE
59+
ast.NodeVisitor.visit_If
60+
ast.NodeVisitor.visit_IfExp
61+
ast.NodeVisitor.visit_Import
62+
ast.NodeVisitor.visit_ImportFrom
63+
ast.NodeVisitor.visit_In
64+
ast.NodeVisitor.visit_Index
65+
ast.NodeVisitor.visit_Interactive
66+
ast.NodeVisitor.visit_Invert
67+
ast.NodeVisitor.visit_Is
68+
ast.NodeVisitor.visit_IsNot
69+
ast.NodeVisitor.visit_LShift
70+
ast.NodeVisitor.visit_Lambda
71+
ast.NodeVisitor.visit_List
72+
ast.NodeVisitor.visit_ListComp
73+
ast.NodeVisitor.visit_Load
74+
ast.NodeVisitor.visit_Lt
75+
ast.NodeVisitor.visit_LtE
76+
ast.NodeVisitor.visit_MatMult
77+
ast.NodeVisitor.visit_Mod
78+
ast.NodeVisitor.visit_Module
79+
ast.NodeVisitor.visit_Mult
80+
ast.NodeVisitor.visit_Name
81+
ast.NodeVisitor.visit_NameConstant
82+
ast.NodeVisitor.visit_Nonlocal
83+
ast.NodeVisitor.visit_Not
84+
ast.NodeVisitor.visit_NotEq
85+
ast.NodeVisitor.visit_NotIn
86+
ast.NodeVisitor.visit_Num
87+
ast.NodeVisitor.visit_Or
88+
ast.NodeVisitor.visit_Param
89+
ast.NodeVisitor.visit_Pass
90+
ast.NodeVisitor.visit_Pow
91+
ast.NodeVisitor.visit_RShift
92+
ast.NodeVisitor.visit_Raise
93+
ast.NodeVisitor.visit_Return
94+
ast.NodeVisitor.visit_Set
95+
ast.NodeVisitor.visit_SetComp
96+
ast.NodeVisitor.visit_Slice
97+
ast.NodeVisitor.visit_Starred
98+
ast.NodeVisitor.visit_Store
99+
ast.NodeVisitor.visit_Str
100+
ast.NodeVisitor.visit_Sub
101+
ast.NodeVisitor.visit_Subscript
102+
ast.NodeVisitor.visit_Suite
103+
ast.NodeVisitor.visit_Try
104+
ast.NodeVisitor.visit_Tuple
105+
ast.NodeVisitor.visit_UAdd
106+
ast.NodeVisitor.visit_USub
107+
ast.NodeVisitor.visit_UnaryOp
108+
ast.NodeVisitor.visit_While
109+
ast.NodeVisitor.visit_With
110+
ast.NodeVisitor.visit_Yield
111+
ast.NodeVisitor.visit_YieldFrom
112+
ast.NodeVisitor.visit_alias
113+
ast.NodeVisitor.visit_arg
114+
ast.NodeVisitor.visit_arguments
115+
ast.NodeVisitor.visit_comprehension
116+
ast.NodeVisitor.visit_keyword
117+
ast.NodeVisitor.visit_withitem
18118
asyncio.AbstractEventLoop.connect_accepted_socket
19119
asyncio.AbstractEventLoop.create_unix_connection
20120
asyncio.BaseChildWatcher

0 commit comments

Comments
 (0)