[Python-checkins] bpo-38348: Extend command line options of ast parsing tool (GH-16540)

Victor Stinner webhook-mailer at python.org
Mon Dec 16 13:23:37 EST 2019


https://github.com/python/cpython/commit/814d687c7df3e0c60036943b68ece13f9f19dfef
commit: 814d687c7df3e0c60036943b68ece13f9f19dfef
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Victor Stinner <vstinner at python.org>
date: 2019-12-16T19:23:27+01:00
summary:

bpo-38348: Extend command line options of ast parsing tool (GH-16540)

Add -i and --indent (indentation level), and --no-type-comments
(type comments) command line options to ast parsing tool.

files:
A Misc/NEWS.d/next/Library/2019-10-02-18-15-28.bpo-38348._-5eq2.rst
M Doc/library/ast.rst
M Lib/ast.py

diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index a7e0729b902d2..baf563f5d7877 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -381,10 +381,19 @@ The following options are accepted:
    Specify what kind of code must be compiled, like the *mode* argument
    in :func:`parse`.
 
+.. cmdoption:: --no-type-comments
+
+   Don't parse type comments.
+
 .. cmdoption:: -a, --include-attributes
 
    Include attributes such as line numbers and column offsets.
 
+.. cmdoption:: -i <indent>
+               --indent <indent>
+
+   Indentation of nodes in AST (number of spaces).
+
 If :file:`infile` is specified its contents are parsed to AST and dumped
 to stdout.  Otherwise, the content is read from stdin.
 
diff --git a/Lib/ast.py b/Lib/ast.py
index 13ae9e0a70eae..ee3f74358ee12 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1258,15 +1258,19 @@ def main():
     parser.add_argument('-m', '--mode', default='exec',
                         choices=('exec', 'single', 'eval', 'func_type'),
                         help='specify what kind of code must be parsed')
+    parser.add_argument('--no-type-comments', default=True, action='store_false',
+                        help="don't add information about type comments")
     parser.add_argument('-a', '--include-attributes', action='store_true',
                         help='include attributes such as line numbers and '
                              'column offsets')
+    parser.add_argument('-i', '--indent', type=int, default=3,
+                        help='indentation of nodes (number of spaces)')
     args = parser.parse_args()
 
     with args.infile as infile:
         source = infile.read()
-    tree = parse(source, args.infile.name, args.mode, type_comments=True)
-    print(dump(tree, include_attributes=args.include_attributes, indent=3))
+    tree = parse(source, args.infile.name, args.mode, type_comments=args.no_type_comments)
+    print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
 
 if __name__ == '__main__':
     main()
diff --git a/Misc/NEWS.d/next/Library/2019-10-02-18-15-28.bpo-38348._-5eq2.rst b/Misc/NEWS.d/next/Library/2019-10-02-18-15-28.bpo-38348._-5eq2.rst
new file mode 100644
index 0000000000000..5ca72dea97702
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-10-02-18-15-28.bpo-38348._-5eq2.rst
@@ -0,0 +1,2 @@
+Add ``-i`` and ``--indent`` (indentation level), and ``--no-type-comments``
+(type comments) command line options to ast parsing tool.



More information about the Python-checkins mailing list