blob: bfd571deb4fd1f3f2441614048187575f532d0ae [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001:mod:`ast` --- Abstract Syntax Trees
2====================================
Georg Brandl0c77a822008-06-10 16:37:50 +00003
4.. module:: ast
5 :synopsis: Abstract Syntax Tree classes and manipulation.
6
7.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
8.. sectionauthor:: Georg Brandl <georg@python.org>
9
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/ast.py`
Georg Brandl0c77a822008-06-10 16:37:50 +000011
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl0c77a822008-06-10 16:37:50 +000014The :mod:`ast` module helps Python applications to process trees of the Python
15abstract syntax grammar. The abstract syntax itself might change with each
16Python release; this module helps to find out programmatically what the current
17grammar looks like.
18
Benjamin Petersonec9199b2008-11-08 17:05:00 +000019An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
Georg Brandl22b34312009-07-26 14:54:51 +000020a flag to the :func:`compile` built-in function, or using the :func:`parse`
Georg Brandl0c77a822008-06-10 16:37:50 +000021helper provided in this module. The result will be a tree of objects whose
Benjamin Petersonec9199b2008-11-08 17:05:00 +000022classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
23compiled into a Python code object using the built-in :func:`compile` function.
Georg Brandl0c77a822008-06-10 16:37:50 +000024
Georg Brandl0c77a822008-06-10 16:37:50 +000025
26Node classes
27------------
28
29.. class:: AST
30
31 This is the base of all AST node classes. The actual node classes are
32 derived from the :file:`Parser/Python.asdl` file, which is reproduced
33 :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
34 module and re-exported in :mod:`ast`.
35
36 There is one class defined for each left-hand side symbol in the abstract
37 grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
38 there is one class defined for each constructor on the right-hand side; these
39 classes inherit from the classes for the left-hand side trees. For example,
40 :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
41 with alternatives (aka "sums"), the left-hand side class is abstract: only
42 instances of specific constructor nodes are ever created.
43
Serhiy Storchaka913876d2018-10-28 13:41:26 +020044 .. index:: single: ? (question mark); in AST grammar
45 .. index:: single: * (asterisk); in AST grammar
46
Georg Brandl0c77a822008-06-10 16:37:50 +000047 .. attribute:: _fields
48
49 Each concrete class has an attribute :attr:`_fields` which gives the names
50 of all child nodes.
51
52 Each instance of a concrete class has one attribute for each child node,
53 of the type as defined in the grammar. For example, :class:`ast.BinOp`
54 instances have an attribute :attr:`left` of type :class:`ast.expr`.
55
56 If these attributes are marked as optional in the grammar (using a
57 question mark), the value might be ``None``. If the attributes can have
58 zero-or-more values (marked with an asterisk), the values are represented
59 as Python lists. All possible attributes must be present and have valid
60 values when compiling an AST with :func:`compile`.
61
62 .. attribute:: lineno
63 col_offset
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000064 end_lineno
65 end_col_offset
Georg Brandl0c77a822008-06-10 16:37:50 +000066
67 Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000068 :attr:`lineno`, :attr:`col_offset`, :attr:`lineno`, and :attr:`col_offset`
69 attributes. The :attr:`lineno` and :attr:`end_lineno` are the first and
70 last line numbers of source text span (1-indexed so the first line is line 1)
71 and the :attr:`col_offset` and :attr:`end_col_offset` are the corresponding
72 UTF-8 byte offsets of the first and last tokens that generated the node.
73 The UTF-8 offset is recorded because the parser uses UTF-8 internally.
74
75 Note that the end positions are not required by the compiler and are
76 therefore optional. The end offset is *after* the last symbol, for example
77 one can get the source segment of a one-line expression node using
78 ``source_line[node.col_offset : node.end_col_offset]``.
Georg Brandl0c77a822008-06-10 16:37:50 +000079
80 The constructor of a class :class:`ast.T` parses its arguments as follows:
81
82 * If there are positional arguments, there must be as many as there are items
83 in :attr:`T._fields`; they will be assigned as attributes of these names.
84 * If there are keyword arguments, they will set the attributes of the same
85 names to the given values.
86
87 For example, to create and populate an :class:`ast.UnaryOp` node, you could
88 use ::
89
90 node = ast.UnaryOp()
91 node.op = ast.USub()
Serhiy Storchaka3f228112018-09-27 17:42:37 +030092 node.operand = ast.Constant()
93 node.operand.value = 5
Georg Brandl0c77a822008-06-10 16:37:50 +000094 node.operand.lineno = 0
95 node.operand.col_offset = 0
96 node.lineno = 0
97 node.col_offset = 0
98
99 or the more compact ::
100
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300101 node = ast.UnaryOp(ast.USub(), ast.Constant(5, lineno=0, col_offset=0),
Georg Brandl0c77a822008-06-10 16:37:50 +0000102 lineno=0, col_offset=0)
103
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200104.. versionchanged:: 3.8
105
106 Class :class:`ast.Constant` is now used for all constants.
107
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300108.. deprecated:: 3.8
109
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200110 Old classes :class:`ast.Num`, :class:`ast.Str`, :class:`ast.Bytes`,
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300111 :class:`ast.NameConstant` and :class:`ast.Ellipsis` are still available,
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200112 but they will be removed in future Python releases. In the meanwhile,
113 instantiating them will return an instance of a different class.
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300114
Georg Brandl0c77a822008-06-10 16:37:50 +0000115
116.. _abstract-grammar:
117
118Abstract Grammar
119----------------
120
Georg Brandl0c77a822008-06-10 16:37:50 +0000121The abstract grammar is currently defined as follows:
122
123.. literalinclude:: ../../Parser/Python.asdl
Martin Panter1050d2d2016-07-26 11:18:21 +0200124 :language: none
Georg Brandl0c77a822008-06-10 16:37:50 +0000125
126
127:mod:`ast` Helpers
128------------------
129
Martin Panter2e4571a2015-11-14 01:07:43 +0000130Apart from the node classes, the :mod:`ast` module defines these utility functions
Georg Brandl0c77a822008-06-10 16:37:50 +0000131and classes for traversing abstract syntax trees:
132
Guido van Rossum10b55c12019-06-11 17:23:12 -0700133.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
Georg Brandl0c77a822008-06-10 16:37:50 +0000134
Terry Reedyfeac6242011-01-24 21:36:03 +0000135 Parse the source into an AST node. Equivalent to ``compile(source,
Benjamin Petersonec9199b2008-11-08 17:05:00 +0000136 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl0c77a822008-06-10 16:37:50 +0000137
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800138 If ``type_comments=True`` is given, the parser is modified to check
139 and return type comments as specified by :pep:`484` and :pep:`526`.
140 This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the
141 flags passed to :func:`compile()`. This will report syntax errors
142 for misplaced type comments. Without this flag, type comments will
143 be ignored, and the ``type_comment`` field on selected AST nodes
144 will always be ``None``. In addition, the locations of ``# type:
145 ignore`` comments will be returned as the ``type_ignores``
146 attribute of :class:`Module` (otherwise it is always an empty list).
147
148 In addition, if ``mode`` is ``'func_type'``, the input syntax is
149 modified to correspond to :pep:`484` "signature type comments",
150 e.g. ``(str, int) -> List[str]``.
151
Guido van Rossum10b55c12019-06-11 17:23:12 -0700152 Also, setting ``feature_version`` to a tuple ``(major, minor)``
153 will attempt to parse using that Python version's grammar.
154 Currently ``major`` must equal to ``3``. For example, setting
155 ``feature_version=(3, 4)`` will allow the use of ``async`` and
156 ``await`` as variable names. The lowest supported version is
157 ``(3, 4)``; the highest is ``sys.version_info[0:2]``.
Guido van Rossum495da292019-03-07 12:38:08 -0800158
Brett Cannon7a7f1002018-03-09 12:03:22 -0800159 .. warning::
160 It is possible to crash the Python interpreter with a
161 sufficiently large/complex string due to stack depth limitations
162 in Python's AST compiler.
163
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800164 .. versionchanged:: 3.8
Guido van Rossum495da292019-03-07 12:38:08 -0800165 Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800166
Georg Brandl48310cd2009-01-03 21:18:54 +0000167
Pablo Galindo27fc3b62019-11-24 23:02:40 +0000168.. function:: unparse(ast_obj)
169
170 Unparse an :class:`ast.AST` object and generate a string with code
171 that would produce an equivalent :class:`ast.AST` object if parsed
172 back with :func:`ast.parse`.
173
174 .. warning::
Gurupad Hegde6c7bb382019-12-28 17:16:02 -0500175 The produced code string will not necessarily be equal to the original
Pablo Galindo27fc3b62019-11-24 23:02:40 +0000176 code that generated the :class:`ast.AST` object.
177
178 .. versionadded:: 3.9
179
180
Georg Brandl0c77a822008-06-10 16:37:50 +0000181.. function:: literal_eval(node_or_string)
182
Georg Brandlb9b389e2014-11-05 20:20:28 +0100183 Safely evaluate an expression node or a string containing a Python literal or
184 container display. The string or node provided may only consist of the
185 following Python literal structures: strings, bytes, numbers, tuples, lists,
186 dicts, sets, booleans, and ``None``.
Georg Brandl0c77a822008-06-10 16:37:50 +0000187
Georg Brandlb9b389e2014-11-05 20:20:28 +0100188 This can be used for safely evaluating strings containing Python values from
189 untrusted sources without the need to parse the values oneself. It is not
190 capable of evaluating arbitrarily complex expressions, for example involving
191 operators or indexing.
Georg Brandl0c77a822008-06-10 16:37:50 +0000192
Brett Cannon7a7f1002018-03-09 12:03:22 -0800193 .. warning::
194 It is possible to crash the Python interpreter with a
195 sufficiently large/complex string due to stack depth limitations
196 in Python's AST compiler.
197
Georg Brandl492f3fc2010-07-11 09:41:21 +0000198 .. versionchanged:: 3.2
Georg Brandl85f21772010-07-13 06:38:10 +0000199 Now allows bytes and set literals.
Georg Brandl492f3fc2010-07-11 09:41:21 +0000200
Raymond Hettinger4fcf5c12020-01-02 22:21:18 -0700201 .. versionchanged:: 3.9
202 Now supports creating empty sets with ``'set()'``.
203
Georg Brandl0c77a822008-06-10 16:37:50 +0000204
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +0000205.. function:: get_docstring(node, clean=True)
Georg Brandl0c77a822008-06-10 16:37:50 +0000206
207 Return the docstring of the given *node* (which must be a
INADA Naokicb41b272017-02-23 00:31:59 +0900208 :class:`FunctionDef`, :class:`AsyncFunctionDef`, :class:`ClassDef`,
209 or :class:`Module` node), or ``None`` if it has no docstring.
210 If *clean* is true, clean up the docstring's indentation with
211 :func:`inspect.cleandoc`.
212
213 .. versionchanged:: 3.5
214 :class:`AsyncFunctionDef` is now supported.
215
Georg Brandl0c77a822008-06-10 16:37:50 +0000216
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000217.. function:: get_source_segment(source, node, *, padded=False)
218
219 Get source code segment of the *source* that generated *node*.
220 If some location information (:attr:`lineno`, :attr:`end_lineno`,
221 :attr:`col_offset`, or :attr:`end_col_offset`) is missing, return ``None``.
222
223 If *padded* is ``True``, the first line of a multi-line statement will
224 be padded with spaces to match its original position.
225
226 .. versionadded:: 3.8
227
228
Georg Brandl0c77a822008-06-10 16:37:50 +0000229.. function:: fix_missing_locations(node)
230
231 When you compile a node tree with :func:`compile`, the compiler expects
232 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
233 them. This is rather tedious to fill in for generated nodes, so this helper
234 adds these attributes recursively where not already set, by setting them to
235 the values of the parent node. It works recursively starting at *node*.
236
237
238.. function:: increment_lineno(node, n=1)
239
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000240 Increment the line number and end line number of each node in the tree
241 starting at *node* by *n*. This is useful to "move code" to a different
242 location in a file.
Georg Brandl0c77a822008-06-10 16:37:50 +0000243
244
245.. function:: copy_location(new_node, old_node)
246
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000247 Copy source location (:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`,
248 and :attr:`end_col_offset`) from *old_node* to *new_node* if possible,
249 and return *new_node*.
Georg Brandl0c77a822008-06-10 16:37:50 +0000250
251
252.. function:: iter_fields(node)
253
254 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
255 that is present on *node*.
256
257
258.. function:: iter_child_nodes(node)
259
260 Yield all direct child nodes of *node*, that is, all fields that are nodes
261 and all items of fields that are lists of nodes.
262
263
264.. function:: walk(node)
265
Georg Brandl619e7ba2011-01-09 07:38:51 +0000266 Recursively yield all descendant nodes in the tree starting at *node*
267 (including *node* itself), in no specified order. This is useful if you only
268 want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +0000269
270
271.. class:: NodeVisitor()
272
273 A node visitor base class that walks the abstract syntax tree and calls a
274 visitor function for every node found. This function may return a value
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000275 which is forwarded by the :meth:`visit` method.
Georg Brandl0c77a822008-06-10 16:37:50 +0000276
277 This class is meant to be subclassed, with the subclass adding visitor
278 methods.
279
280 .. method:: visit(node)
281
282 Visit a node. The default implementation calls the method called
283 :samp:`self.visit_{classname}` where *classname* is the name of the node
284 class, or :meth:`generic_visit` if that method doesn't exist.
285
286 .. method:: generic_visit(node)
287
288 This visitor calls :meth:`visit` on all children of the node.
Georg Brandl48310cd2009-01-03 21:18:54 +0000289
Georg Brandl0c77a822008-06-10 16:37:50 +0000290 Note that child nodes of nodes that have a custom visitor method won't be
291 visited unless the visitor calls :meth:`generic_visit` or visits them
292 itself.
293
294 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
295 during traversal. For this a special visitor exists
296 (:class:`NodeTransformer`) that allows modifications.
297
Serhiy Storchakac3ea41e2019-08-26 10:13:19 +0300298 .. deprecated:: 3.8
299
300 Methods :meth:`visit_Num`, :meth:`visit_Str`, :meth:`visit_Bytes`,
301 :meth:`visit_NameConstant` and :meth:`visit_Ellipsis` are deprecated
302 now and will not be called in future Python versions. Add the
303 :meth:`visit_Constant` method to handle all constant nodes.
304
Georg Brandl0c77a822008-06-10 16:37:50 +0000305
306.. class:: NodeTransformer()
307
308 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
309 allows modification of nodes.
310
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000311 The :class:`NodeTransformer` will walk the AST and use the return value of
312 the visitor methods to replace or remove the old node. If the return value
313 of the visitor method is ``None``, the node will be removed from its
314 location, otherwise it is replaced with the return value. The return value
315 may be the original node in which case no replacement takes place.
Georg Brandl0c77a822008-06-10 16:37:50 +0000316
317 Here is an example transformer that rewrites all occurrences of name lookups
318 (``foo``) to ``data['foo']``::
319
320 class RewriteName(NodeTransformer):
321
322 def visit_Name(self, node):
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +0300323 return Subscript(
Georg Brandl0c77a822008-06-10 16:37:50 +0000324 value=Name(id='data', ctx=Load()),
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300325 slice=Index(value=Constant(value=node.id)),
Georg Brandl0c77a822008-06-10 16:37:50 +0000326 ctx=node.ctx
327 ), node)
328
329 Keep in mind that if the node you're operating on has child nodes you must
330 either transform the child nodes yourself or call the :meth:`generic_visit`
331 method for the node first.
332
333 For nodes that were part of a collection of statements (that applies to all
334 statement nodes), the visitor may also return a list of nodes rather than
335 just a single node.
336
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +0300337 If :class:`NodeTransformer` introduces new nodes (that weren't part of
338 original tree) without giving them location information (such as
339 :attr:`lineno`), :func:`fix_missing_locations` should be called with
340 the new sub-tree to recalculate the location information::
341
342 tree = ast.parse('foo', mode='eval')
343 new_tree = fix_missing_locations(RewriteName().visit(tree))
344
Georg Brandl0c77a822008-06-10 16:37:50 +0000345 Usually you use the transformer like this::
346
347 node = YourTransformer().visit(node)
348
349
Serhiy Storchaka850573b2019-09-09 19:33:13 +0300350.. function:: dump(node, annotate_fields=True, include_attributes=False, *, indent=None)
Georg Brandl0c77a822008-06-10 16:37:50 +0000351
352 Return a formatted dump of the tree in *node*. This is mainly useful for
Serhiy Storchakae64f9482019-08-29 09:30:23 +0300353 debugging purposes. If *annotate_fields* is true (by default),
354 the returned string will show the names and the values for fields.
355 If *annotate_fields* is false, the result string will be more compact by
356 omitting unambiguous field names. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000357 numbers and column offsets are not dumped by default. If this is wanted,
Serhiy Storchakae64f9482019-08-29 09:30:23 +0300358 *include_attributes* can be set to true.
Senthil Kumaranf3695bf2016-01-06 21:26:53 -0800359
Serhiy Storchaka850573b2019-09-09 19:33:13 +0300360 If *indent* is a non-negative integer or string, then the tree will be
361 pretty-printed with that indent level. An indent level
362 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
363 selects the single line representation. Using a positive integer indent
364 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
365 that string is used to indent each level.
366
367 .. versionchanged:: 3.9
368 Added the *indent* option.
369
370
Serhiy Storchaka832e8642019-09-09 23:36:13 +0300371.. _ast-cli:
372
373Command-Line Usage
374------------------
375
376.. versionadded:: 3.9
377
378The :mod:`ast` module can be executed as a script from the command line.
379It is as simple as:
380
381.. code-block:: sh
382
383 python -m ast [-m <mode>] [-a] [infile]
384
385The following options are accepted:
386
387.. program:: ast
388
389.. cmdoption:: -h, --help
390
391 Show the help message and exit.
392
393.. cmdoption:: -m <mode>
394 --mode <mode>
395
396 Specify what kind of code must be compiled, like the *mode* argument
397 in :func:`parse`.
398
Batuhan Taşkaya814d6872019-12-16 21:23:27 +0300399.. cmdoption:: --no-type-comments
400
401 Don't parse type comments.
402
Serhiy Storchaka832e8642019-09-09 23:36:13 +0300403.. cmdoption:: -a, --include-attributes
404
405 Include attributes such as line numbers and column offsets.
406
Batuhan Taşkaya814d6872019-12-16 21:23:27 +0300407.. cmdoption:: -i <indent>
408 --indent <indent>
409
410 Indentation of nodes in AST (number of spaces).
411
Serhiy Storchaka832e8642019-09-09 23:36:13 +0300412If :file:`infile` is specified its contents are parsed to AST and dumped
413to stdout. Otherwise, the content is read from stdin.
414
415
Senthil Kumaranf3695bf2016-01-06 21:26:53 -0800416.. seealso::
417
Sanyam Khurana338cd832018-01-20 05:55:37 +0530418 `Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external documentation resource, has good
Senthil Kumaranf3695bf2016-01-06 21:26:53 -0800419 details on working with Python ASTs.