summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-02-28 02:13:03 (GMT)
committerGitHub <noreply@github.com>2024-02-28 02:13:03 (GMT)
commited4dfd8825b49e16a0fcb9e67baf1b58bb8d438f (patch)
tree88935b427cd68a5a249f0876f3a1cbe5ce241ce8 /Doc
parent5a1559d9493dd298a08c4be32b52295aa3eb89e5 (diff)
downloadcpython-ed4dfd8825b49e16a0fcb9e67baf1b58bb8d438f.zip
cpython-ed4dfd8825b49e16a0fcb9e67baf1b58bb8d438f.tar.gz
cpython-ed4dfd8825b49e16a0fcb9e67baf1b58bb8d438f.tar.bz2
gh-105858: Improve AST node constructors (#105880)
Demonstration: >>> ast.FunctionDef.__annotations__ {'name': <class 'str'>, 'args': <class 'ast.arguments'>, 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'returns': ast.expr | None, 'type_comment': str | None, 'type_params': list[ast.type_param]} >>> ast.FunctionDef() <stdin>:1: DeprecationWarning: FunctionDef.__init__ missing 1 required positional argument: 'name'. This will become an error in Python 3.15. <stdin>:1: DeprecationWarning: FunctionDef.__init__ missing 1 required positional argument: 'args'. This will become an error in Python 3.15. <ast.FunctionDef object at 0x101959460> >>> node = ast.FunctionDef(name="foo", args=ast.arguments()) >>> node.decorator_list [] >>> ast.FunctionDef(whatever="you want", name="x", args=ast.arguments()) <stdin>:1: DeprecationWarning: FunctionDef.__init__ got an unexpected keyword argument 'whatever'. Support for arbitrary keyword arguments is deprecated and will be removed in Python 3.15. <ast.FunctionDef object at 0x1019581f0>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/ast.rst25
-rw-r--r--Doc/whatsnew/3.13.rst15
2 files changed, 29 insertions, 11 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index c943c2f..d629393 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -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.
@@ -140,6 +135,14 @@ Node classes
In the meantime, instantiating them will return an instance of
a different class.
+.. deprecated-removed:: 3.13 3.15
+
+ Previous versions of Python allowed the creation of AST nodes that were missing
+ required fields. Similarly, AST node constructors allowed arbitrary keyword
+ arguments that were set as attributes of the AST node, even if they did 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
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index ca5a0e7..3a277d7 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -206,6 +206,21 @@ array
ast
---
+* The constructors of node types in the :mod:`ast` module are now stricter
+ in the arguments they accept, and have more intuitive behaviour when
+ arguments are omitted.
+
+ If an optional field on an AST node is not included as an argument when
+ constructing an instance, the field will now be set to ``None``. Similarly,
+ if a list field is omitted, that field will now be set to an empty list.
+ (Previously, in both cases, the attribute would be missing on the newly
+ constructed AST node instance.)
+
+ If other arguments are omitted, a :exc:`DeprecationWarning` is emitted.
+ This will cause an exception in Python 3.15. Similarly, passing a keyword
+ argument that does not map to a field on the AST node is now deprecated,
+ 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``.