Skip to content

gh-133367: Add missing options to ast CLI #133369

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

Merged
merged 13 commits into from
May 5, 2025
28 changes: 24 additions & 4 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ compiled into a Python code object using the built-in :func:`compile` function.

.. _abstract-grammar:

Abstract Grammar
Abstract grammar
----------------

The abstract grammar is currently defined as follows:
Expand Down Expand Up @@ -2159,7 +2159,7 @@ Async and await
occurrences of the same value (e.g. :class:`ast.Add`).


:mod:`ast` Helpers
:mod:`ast` helpers
------------------

Apart from the node classes, the :mod:`ast` module defines these utility functions
Expand Down Expand Up @@ -2484,7 +2484,7 @@ and classes for traversing abstract syntax trees:

.. _ast-compiler-flags:

Compiler Flags
Compiler flags
--------------

The following flags may be passed to :func:`compile` in order to change
Expand Down Expand Up @@ -2533,7 +2533,7 @@ effects on the compilation of a program:

.. _ast-cli:

Command-Line Usage
Command-line usage
------------------

.. versionadded:: 3.9
Expand Down Expand Up @@ -2572,6 +2572,26 @@ The following options are accepted:

Indentation of nodes in AST (number of spaces).

.. option:: --feature-version <version>

Python version in the format 3.x (for example, 3.10).

.. versionadded:: next

.. option:: -o <level>
--optimize <level>

Optimization level for parser.

.. versionadded:: next

.. option:: --show-empty

Show empty lists and fields that are ``None``.

.. versionadded:: next


If :file:`infile` is specified its contents are parsed to AST and dumped
to stdout. Otherwise, the content is read from stdin.

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,10 @@ ast
that the root node type is appropriate.
(Contributed by Irit Katriel in :gh:`130139`.)

* Add new ``--feature-version``, ``--optimize``, ``--show-empty`` options to
command-line interface.
(Contributed by Semyon Moroz in :gh:`133367`.)

bdb
---

Expand Down
26 changes: 24 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ def main():
'column offsets')
parser.add_argument('-i', '--indent', type=int, default=3,
help='indentation of nodes (number of spaces)')
parser.add_argument('--feature-version',
type=str, default=None, metavar='VERSION',
help='python version in the format 3.x (e.g., 3.10)')
parser.add_argument('-o', '--optimize',
type=int, default=-1, metavar='LEVEL',
help='optimization level for parser (default -1)')
parser.add_argument('--show-empty', default=False, action='store_true',
help='show empty lists and fields in dump output')
args = parser.parse_args()

if args.infile == '-':
Expand All @@ -652,8 +660,22 @@ def main():
name = args.infile
with open(args.infile, 'rb') as infile:
source = infile.read()
tree = parse(source, name, args.mode, type_comments=args.no_type_comments)
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))

# Process feature_version
feature_version = None
if args.feature_version:
try:
major, minor = map(int, args.feature_version.split('.', 1))
except ValueError:
parser.error('Invalid format for --feature-version; '
'expected format 3.x (e.g., 3.10)')

feature_version = (major, minor)

tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
feature_version=feature_version, optimize=args.optimize)
print(dump(tree, include_attributes=args.include_attributes,
indent=args.indent, show_empty=args.show_empty))

if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``--feature-version``, ``--optimize``, ``--show-empty`` options for
:mod:`ast` command-line interface. Patch by Semyon Moroz.
Loading