Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Feb 25, 2024
1 parent c4627a1 commit 1f85438
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,15 @@ Node classes
For example, to create and populate an :class:`ast.UnaryOp` node, you could
use ::

node = ast.UnaryOp()
node.op = ast.USub()
node.operand = ast.Constant()
node.operand.value = 5
node.operand.lineno = 0
node.operand.col_offset = 0
node.lineno = 0
node.col_offset = 0

or the more compact ::

node = ast.UnaryOp(ast.USub(), ast.Constant(5, lineno=0, col_offset=0),
lineno=0, col_offset=0)

If a field that is optional in the grammar is omitted from the constructor,
it defaults to ``None``. If a list field is omitted, it defaults to the empty
list. If any other field is omitted, a :exc:`DeprecationWarning` is raised
and the AST node will not have this field. In Python 3.15, this condition will
raise an error.

.. versionchanged:: 3.8

Class :class:`ast.Constant` is now used for all constants.
Expand All @@ -140,6 +135,14 @@ Node classes
In the meantime, instantiating them will return an instance of
a different class.

.. deprecated:: 3.13

Previous versions of Python allowed the creation of AST nodes missing
required fields. Similarly, AST node constructors allowed arbitrary keyword
arguments that were set as attributes of the AST node, even if they didd not
match any of the fields of the AST node. This behavior is deprecated and will
be removed in Python 3.15.

.. note::
The descriptions of the specific node classes displayed here
were initially adapted from the fantastic `Green Tree
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ array
ast
---

* The constructors of node types in the :mod:`ast` module are stricter
in the arguments they accept. If an optional field is not passed to the
constructor, it is set to ``None``, and if a list field is not passed,
it is set to the empty list. (Previously, the attribute would be missing.)
If any other field is not passed to the constructor, a :exc:`DeprecationWarning`
is raised. This will raise an exception in Python 3.15. Similarly, passing
a keyword argument that does not map to a field on the AST node now raises
a :exc:`DeprecationWarning` and will raise an exception in Python 3.15.
* :func:`ast.parse` now accepts an optional argument ``optimize``
which is passed on to the :func:`compile` built-in. This makes it
possible to obtain an optimized ``AST``.
Expand Down

0 comments on commit 1f85438

Please sign in to comment.