@@ -146,6 +146,102 @@ Node classes
146
146
Snakes <https://greentreesnakes.readthedocs.io/en/latest/> `__ project and
147
147
all its contributors.
148
148
149
+
150
+ .. _ast-root-nodes :
151
+
152
+ Root nodes
153
+ ^^^^^^^^^^
154
+
155
+ .. class :: Module(body, type_ignores)
156
+
157
+ A Python module, as with :ref: `file input <file-input >`.
158
+ Node type generated by :func: `ast.parse ` in the default ``"exec" `` *mode *.
159
+
160
+ *body * is a :class: `list ` of the module's :ref: `ast-statements `.
161
+
162
+ *type_ignores * is a :class: `list ` of the module's type ignore comments;
163
+ see :func: `ast.parse ` for more details.
164
+
165
+ .. doctest ::
166
+
167
+ >>> print (ast.dump(ast.parse(' x = 1' ), indent = 4 ))
168
+ Module(
169
+ body=[
170
+ Assign(
171
+ targets=[
172
+ Name(id='x', ctx=Store())],
173
+ value=Constant(value=1))],
174
+ type_ignores=[])
175
+
176
+
177
+ .. class :: Expression(body)
178
+
179
+ A single Python :ref: `expression input <expression-input >`.
180
+ Node type generated by :func: `ast.parse ` when *mode * is ``"eval" ``.
181
+
182
+ *body * is a single node,
183
+ one of the :ref: `expression types <ast-expressions >`.
184
+
185
+ .. doctest ::
186
+
187
+ >>> print (ast.dump(ast.parse(' 123' , mode = ' eval' ), indent = 4 ))
188
+ Expression(
189
+ body=Constant(value=123))
190
+
191
+
192
+ .. class :: Interactive(body)
193
+
194
+ A single :ref: `interactive input <interactive >`, like in :ref: `tut-interac `.
195
+ Node type generated by :func: `ast.parse ` when *mode * is ``"single" ``.
196
+
197
+ *body * is a :class: `list ` of :ref: `statement nodes <ast-statements >`.
198
+
199
+ .. doctest ::
200
+
201
+ >>> print (ast.dump(ast.parse(' x = 1; y = 2' , mode = ' single' ), indent = 4 ))
202
+ Interactive(
203
+ body=[
204
+ Assign(
205
+ targets=[
206
+ Name(id='x', ctx=Store())],
207
+ value=Constant(value=1)),
208
+ Assign(
209
+ targets=[
210
+ Name(id='y', ctx=Store())],
211
+ value=Constant(value=2))])
212
+
213
+
214
+ .. class :: FunctionType(argtypes, returns)
215
+
216
+ A representation of an old-style type comments for functions,
217
+ as Python versions prior to 3.5 didn't support :pep: `484 ` annotations.
218
+ Node type generated by :func: `ast.parse ` when *mode * is ``"func_type" ``.
219
+
220
+ Such type comments would look like this::
221
+
222
+ def sum_two_number(a, b):
223
+ # type: (int, int) -> int
224
+ return a + b
225
+
226
+ *argtypes * is a :class: `list ` of :ref: `expression nodes <ast-expressions >`.
227
+
228
+ *returns * is a single :ref: `expression node <ast-expressions >`.
229
+
230
+ .. doctest ::
231
+
232
+ >>> print (ast.dump(ast.parse(' (int, str) -> List[int]' , mode = ' func_type' ), indent = 4 ))
233
+ FunctionType(
234
+ argtypes=[
235
+ Name(id='int', ctx=Load()),
236
+ Name(id='str', ctx=Load())],
237
+ returns=Subscript(
238
+ value=Name(id='List', ctx=Load()),
239
+ slice=Name(id='int', ctx=Load()),
240
+ ctx=Load()))
241
+
242
+ .. versionadded :: 3.8
243
+
244
+
149
245
Literals
150
246
^^^^^^^^
151
247
@@ -344,6 +440,8 @@ Variables
344
440
type_ignores=[])
345
441
346
442
443
+ .. _ast-expressions :
444
+
347
445
Expressions
348
446
^^^^^^^^^^^
349
447
@@ -735,6 +833,9 @@ Comprehensions
735
833
ifs=[],
736
834
is_async=1)]))
737
835
836
+
837
+ .. _ast-statements :
838
+
738
839
Statements
739
840
^^^^^^^^^^
740
841
0 commit comments