mirror of
https://github.com/python/cpython
synced 2026-09-28 01:13:17 +03:00
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>
This commit is contained in:
parent
5a1559d949
commit
ed4dfd8825
10 changed files with 4675 additions and 49 deletions
|
|
@ -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``.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue