Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] gh-92788: Add docs for ast.Module, ast.Expression, and others (GH-101055) #106138

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,102 @@ Node classes
Snakes <https://greentreesnakes.readthedocs.io/en/latest/>`__ project and
all its contributors.


.. _ast-root-nodes:

Root nodes
^^^^^^^^^^

.. class:: Module(body, type_ignores)

A Python module, as with :ref:`file input <file-input>`.
Node type generated by :func:`ast.parse` in the default ``"exec"`` *mode*.

*body* is a :class:`list` of the module's :ref:`ast-statements`.

*type_ignores* is a :class:`list` of the module's type ignore comments;
see :func:`ast.parse` for more details.

.. doctest::

>>> print(ast.dump(ast.parse('x = 1'), indent=4))
Module(
body=[
Assign(
targets=[
Name(id='x', ctx=Store())],
value=Constant(value=1))],
type_ignores=[])


.. class:: Expression(body)

A single Python :ref:`expression input <expression-input>`.
Node type generated by :func:`ast.parse` when *mode* is ``"eval"``.

*body* is a single node,
one of the :ref:`expression types <ast-expressions>`.

.. doctest::

>>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
Expression(
body=Constant(value=123))


.. class:: Interactive(body)

A single :ref:`interactive input <interactive>`, like in :ref:`tut-interac`.
Node type generated by :func:`ast.parse` when *mode* is ``"single"``.

*body* is a :class:`list` of :ref:`statement nodes <ast-statements>`.

.. doctest::

>>> print(ast.dump(ast.parse('x = 1; y = 2', mode='single'), indent=4))
Interactive(
body=[
Assign(
targets=[
Name(id='x', ctx=Store())],
value=Constant(value=1)),
Assign(
targets=[
Name(id='y', ctx=Store())],
value=Constant(value=2))])


.. class:: FunctionType(argtypes, returns)

A representation of an old-style type comments for functions,
as Python versions prior to 3.5 didn't support :pep:`484` annotations.
Node type generated by :func:`ast.parse` when *mode* is ``"func_type"``.

Such type comments would look like this::

def sum_two_number(a, b):
# type: (int, int) -> int
return a + b

*argtypes* is a :class:`list` of :ref:`expression nodes <ast-expressions>`.

*returns* is a single :ref:`expression node <ast-expressions>`.

.. doctest::

>>> print(ast.dump(ast.parse('(int, str) -> List[int]', mode='func_type'), indent=4))
FunctionType(
argtypes=[
Name(id='int', ctx=Load()),
Name(id='str', ctx=Load())],
returns=Subscript(
value=Name(id='List', ctx=Load()),
slice=Name(id='int', ctx=Load()),
ctx=Load()))

.. versionadded:: 3.8


Literals
^^^^^^^^

Expand Down Expand Up @@ -344,6 +440,8 @@ Variables
type_ignores=[])


.. _ast-expressions:

Expressions
^^^^^^^^^^^

Expand Down Expand Up @@ -735,6 +833,9 @@ Comprehensions
ifs=[],
is_async=1)]))


.. _ast-statements:

Statements
^^^^^^^^^^

Expand Down