Skip to content

Commit 33608fd

Browse files
authored
gh-92788: Add docs for ast.Module, ast.Expression, and others (#101055)
1 parent a8210b6 commit 33608fd

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Doc/library/ast.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,102 @@ Node classes
146146
Snakes <https://greentreesnakes.readthedocs.io/en/latest/>`__ project and
147147
all its contributors.
148148

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+
149245
Literals
150246
^^^^^^^^
151247

@@ -344,6 +440,8 @@ Variables
344440
type_ignores=[])
345441

346442

443+
.. _ast-expressions:
444+
347445
Expressions
348446
^^^^^^^^^^^
349447

@@ -735,6 +833,9 @@ Comprehensions
735833
ifs=[],
736834
is_async=1)]))
737835

836+
837+
.. _ast-statements:
838+
738839
Statements
739840
^^^^^^^^^^
740841

0 commit comments

Comments
 (0)