Alexander Belopolsky | f0a0d14 | 2010-10-27 03:06:43 +0000 | [diff] [blame] | 1 | :mod:`ast` --- Abstract Syntax Trees |
| 2 | ==================================== |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 3 | |
| 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 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 10 | .. testsetup:: |
| 11 | |
| 12 | import ast |
| 13 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 14 | **Source code:** :source:`Lib/ast.py` |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 15 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 16 | -------------- |
| 17 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 18 | The :mod:`ast` module helps Python applications to process trees of the Python |
| 19 | abstract syntax grammar. The abstract syntax itself might change with each |
| 20 | Python release; this module helps to find out programmatically what the current |
| 21 | grammar looks like. |
| 22 | |
Benjamin Peterson | ec9199b | 2008-11-08 17:05:00 +0000 | [diff] [blame] | 23 | An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 24 | a flag to the :func:`compile` built-in function, or using the :func:`parse` |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 25 | helper provided in this module. The result will be a tree of objects whose |
Benjamin Peterson | ec9199b | 2008-11-08 17:05:00 +0000 | [diff] [blame] | 26 | classes all inherit from :class:`ast.AST`. An abstract syntax tree can be |
| 27 | compiled into a Python code object using the built-in :func:`compile` function. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 29 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 30 | .. _abstract-grammar: |
| 31 | |
| 32 | Abstract Grammar |
| 33 | ---------------- |
| 34 | |
| 35 | The abstract grammar is currently defined as follows: |
| 36 | |
| 37 | .. literalinclude:: ../../Parser/Python.asdl |
| 38 | :language: none |
| 39 | |
| 40 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 41 | Node classes |
| 42 | ------------ |
| 43 | |
| 44 | .. class:: AST |
| 45 | |
| 46 | This is the base of all AST node classes. The actual node classes are |
| 47 | derived from the :file:`Parser/Python.asdl` file, which is reproduced |
| 48 | :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C |
| 49 | module and re-exported in :mod:`ast`. |
| 50 | |
| 51 | There is one class defined for each left-hand side symbol in the abstract |
| 52 | grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition, |
| 53 | there is one class defined for each constructor on the right-hand side; these |
| 54 | classes inherit from the classes for the left-hand side trees. For example, |
| 55 | :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules |
| 56 | with alternatives (aka "sums"), the left-hand side class is abstract: only |
| 57 | instances of specific constructor nodes are ever created. |
| 58 | |
Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 59 | .. index:: single: ? (question mark); in AST grammar |
| 60 | .. index:: single: * (asterisk); in AST grammar |
| 61 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 62 | .. attribute:: _fields |
| 63 | |
| 64 | Each concrete class has an attribute :attr:`_fields` which gives the names |
| 65 | of all child nodes. |
| 66 | |
| 67 | Each instance of a concrete class has one attribute for each child node, |
| 68 | of the type as defined in the grammar. For example, :class:`ast.BinOp` |
| 69 | instances have an attribute :attr:`left` of type :class:`ast.expr`. |
| 70 | |
| 71 | If these attributes are marked as optional in the grammar (using a |
| 72 | question mark), the value might be ``None``. If the attributes can have |
| 73 | zero-or-more values (marked with an asterisk), the values are represented |
| 74 | as Python lists. All possible attributes must be present and have valid |
| 75 | values when compiling an AST with :func:`compile`. |
| 76 | |
| 77 | .. attribute:: lineno |
| 78 | col_offset |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 79 | end_lineno |
| 80 | end_col_offset |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 81 | |
| 82 | Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 83 | :attr:`lineno`, :attr:`col_offset`, :attr:`lineno`, and :attr:`col_offset` |
| 84 | attributes. The :attr:`lineno` and :attr:`end_lineno` are the first and |
| 85 | last line numbers of source text span (1-indexed so the first line is line 1) |
| 86 | and the :attr:`col_offset` and :attr:`end_col_offset` are the corresponding |
| 87 | UTF-8 byte offsets of the first and last tokens that generated the node. |
| 88 | The UTF-8 offset is recorded because the parser uses UTF-8 internally. |
| 89 | |
| 90 | Note that the end positions are not required by the compiler and are |
| 91 | therefore optional. The end offset is *after* the last symbol, for example |
| 92 | one can get the source segment of a one-line expression node using |
| 93 | ``source_line[node.col_offset : node.end_col_offset]``. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 94 | |
| 95 | The constructor of a class :class:`ast.T` parses its arguments as follows: |
| 96 | |
| 97 | * If there are positional arguments, there must be as many as there are items |
| 98 | in :attr:`T._fields`; they will be assigned as attributes of these names. |
| 99 | * If there are keyword arguments, they will set the attributes of the same |
| 100 | names to the given values. |
| 101 | |
| 102 | For example, to create and populate an :class:`ast.UnaryOp` node, you could |
| 103 | use :: |
| 104 | |
| 105 | node = ast.UnaryOp() |
| 106 | node.op = ast.USub() |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 107 | node.operand = ast.Constant() |
| 108 | node.operand.value = 5 |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 109 | node.operand.lineno = 0 |
| 110 | node.operand.col_offset = 0 |
| 111 | node.lineno = 0 |
| 112 | node.col_offset = 0 |
| 113 | |
| 114 | or the more compact :: |
| 115 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 116 | node = ast.UnaryOp(ast.USub(), ast.Constant(5, lineno=0, col_offset=0), |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 117 | lineno=0, col_offset=0) |
| 118 | |
Serhiy Storchaka | 85a2eef | 2020-02-17 11:03:00 +0200 | [diff] [blame] | 119 | .. versionchanged:: 3.8 |
| 120 | |
| 121 | Class :class:`ast.Constant` is now used for all constants. |
| 122 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 123 | .. deprecated:: 3.8 |
| 124 | |
Serhiy Storchaka | 85a2eef | 2020-02-17 11:03:00 +0200 | [diff] [blame] | 125 | Old classes :class:`ast.Num`, :class:`ast.Str`, :class:`ast.Bytes`, |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 126 | :class:`ast.NameConstant` and :class:`ast.Ellipsis` are still available, |
Serhiy Storchaka | 85a2eef | 2020-02-17 11:03:00 +0200 | [diff] [blame] | 127 | but they will be removed in future Python releases. In the meanwhile, |
| 128 | instantiating them will return an instance of a different class. |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 129 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 130 | Literals |
| 131 | ^^^^^^^^ |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 132 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 133 | .. class:: Constant(value) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 134 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 135 | A constant value. The ``value`` attribute of the ``Constant`` literal contains the |
| 136 | Python object it represents. The values represented can be simple types |
| 137 | such as a number, string or ``None``, but also immutable container types |
| 138 | (tuples and frozensets) if all of their elements are constant. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 139 | |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 140 | .. doctest:: |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 141 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 142 | >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4)) |
| 143 | Expression( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 144 | body=Constant(value=123)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | .. class:: FormattedValue(value, conversion, format_spec) |
| 148 | |
| 149 | Node representing a single formatting field in an f-string. If the string |
| 150 | contains a single formatting field and nothing else the node can be |
| 151 | isolated otherwise it appears in :class:`JoinedStr`. |
| 152 | |
| 153 | * ``value`` is any expression node (such as a literal, a variable, or a |
| 154 | function call). |
| 155 | * ``conversion`` is an integer: |
| 156 | |
| 157 | * -1: no formatting |
| 158 | * 115: ``!s`` string formatting |
| 159 | * 114: ``!r`` repr formatting |
| 160 | * 97: ``!a`` ascii formatting |
| 161 | |
| 162 | * ``format_spec`` is a :class:`JoinedStr` node representing the formatting |
| 163 | of the value, or ``None`` if no format was specified. Both |
| 164 | ``conversion`` and ``format_spec`` can be set at the same time. |
| 165 | |
| 166 | |
| 167 | .. class:: JoinedStr(values) |
| 168 | |
| 169 | An f-string, comprising a series of :class:`FormattedValue` and :class:`Constant` |
| 170 | nodes. |
| 171 | |
| 172 | .. doctest:: |
| 173 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 174 | >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4)) |
| 175 | Expression( |
| 176 | body=JoinedStr( |
| 177 | values=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 178 | Constant(value='sin('), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 179 | FormattedValue( |
| 180 | value=Name(id='a', ctx=Load()), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 181 | conversion=-1), |
| 182 | Constant(value=') is '), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 183 | FormattedValue( |
| 184 | value=Call( |
| 185 | func=Name(id='sin', ctx=Load()), |
| 186 | args=[ |
| 187 | Name(id='a', ctx=Load())], |
| 188 | keywords=[]), |
| 189 | conversion=-1, |
| 190 | format_spec=JoinedStr( |
| 191 | values=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 192 | Constant(value='.3')]))])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | .. class:: List(elts, ctx) |
| 196 | Tuple(elts, ctx) |
| 197 | |
| 198 | A list or tuple. ``elts`` holds a list of nodes representing the elements. |
| 199 | ``ctx`` is :class:`Store` if the container is an assignment target (i.e. |
| 200 | ``(x,y)=something``), and :class:`Load` otherwise. |
| 201 | |
| 202 | .. doctest:: |
| 203 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 204 | >>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4)) |
| 205 | Expression( |
| 206 | body=List( |
| 207 | elts=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 208 | Constant(value=1), |
| 209 | Constant(value=2), |
| 210 | Constant(value=3)], |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 211 | ctx=Load())) |
| 212 | >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4)) |
| 213 | Expression( |
| 214 | body=Tuple( |
| 215 | elts=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 216 | Constant(value=1), |
| 217 | Constant(value=2), |
| 218 | Constant(value=3)], |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 219 | ctx=Load())) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 220 | |
| 221 | |
| 222 | .. class:: Set(elts) |
| 223 | |
| 224 | A set. ``elts`` holds a list of nodes representing the set's elements. |
| 225 | |
| 226 | .. doctest:: |
| 227 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 228 | >>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4)) |
| 229 | Expression( |
| 230 | body=Set( |
| 231 | elts=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 232 | Constant(value=1), |
| 233 | Constant(value=2), |
| 234 | Constant(value=3)])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 235 | |
| 236 | |
| 237 | .. class:: Dict(keys, values) |
| 238 | |
| 239 | A dictionary. ``keys`` and ``values`` hold lists of nodes representing the |
| 240 | keys and the values respectively, in matching order (what would be returned |
| 241 | when calling :code:`dictionary.keys()` and :code:`dictionary.values()`). |
| 242 | |
| 243 | When doing dictionary unpacking using dictionary literals the expression to be |
| 244 | expanded goes in the ``values`` list, with a ``None`` at the corresponding |
| 245 | position in ``keys``. |
| 246 | |
| 247 | .. doctest:: |
| 248 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 249 | >>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4)) |
| 250 | Expression( |
| 251 | body=Dict( |
| 252 | keys=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 253 | Constant(value='a'), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 254 | None], |
| 255 | values=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 256 | Constant(value=1), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 257 | Name(id='d', ctx=Load())])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 258 | |
| 259 | |
| 260 | Variables |
| 261 | ^^^^^^^^^ |
| 262 | |
| 263 | .. class:: Name(id, ctx) |
| 264 | |
| 265 | A variable name. ``id`` holds the name as a string, and ``ctx`` is one of |
| 266 | the following types. |
| 267 | |
| 268 | |
| 269 | .. class:: Load() |
| 270 | Store() |
| 271 | Del() |
| 272 | |
| 273 | Variable references can be used to load the value of a variable, to assign |
| 274 | a new value to it, or to delete it. Variable references are given a context |
| 275 | to distinguish these cases. |
| 276 | |
| 277 | .. doctest:: |
| 278 | |
| 279 | >>> print(ast.dump(ast.parse('a'), indent=4)) |
| 280 | Module( |
| 281 | body=[ |
| 282 | Expr( |
| 283 | value=Name(id='a', ctx=Load()))], |
| 284 | type_ignores=[]) |
| 285 | |
| 286 | >>> print(ast.dump(ast.parse('a = 1'), indent=4)) |
| 287 | Module( |
| 288 | body=[ |
| 289 | Assign( |
| 290 | targets=[ |
| 291 | Name(id='a', ctx=Store())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 292 | value=Constant(value=1))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 293 | type_ignores=[]) |
| 294 | |
| 295 | >>> print(ast.dump(ast.parse('del a'), indent=4)) |
| 296 | Module( |
| 297 | body=[ |
| 298 | Delete( |
| 299 | targets=[ |
| 300 | Name(id='a', ctx=Del())])], |
| 301 | type_ignores=[]) |
| 302 | |
| 303 | |
| 304 | .. class:: Starred(value, ctx) |
| 305 | |
| 306 | A ``*var`` variable reference. ``value`` holds the variable, typically a |
| 307 | :class:`Name` node. This type must be used when building a :class:`Call` |
| 308 | node with ``*args``. |
| 309 | |
| 310 | .. doctest:: |
| 311 | |
| 312 | >>> print(ast.dump(ast.parse('a, *b = it'), indent=4)) |
| 313 | Module( |
| 314 | body=[ |
| 315 | Assign( |
| 316 | targets=[ |
| 317 | Tuple( |
| 318 | elts=[ |
| 319 | Name(id='a', ctx=Store()), |
| 320 | Starred( |
| 321 | value=Name(id='b', ctx=Store()), |
| 322 | ctx=Store())], |
| 323 | ctx=Store())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 324 | value=Name(id='it', ctx=Load()))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 325 | type_ignores=[]) |
| 326 | |
| 327 | |
| 328 | Expressions |
| 329 | ^^^^^^^^^^^ |
| 330 | |
| 331 | .. class:: Expr(value) |
| 332 | |
| 333 | When an expression, such as a function call, appears as a statement by itself |
| 334 | with its return value not used or stored, it is wrapped in this container. |
| 335 | ``value`` holds one of the other nodes in this section, a :class:`Constant`, a |
| 336 | :class:`Name`, a :class:`Lambda`, a :class:`Yield` or :class:`YieldFrom` node. |
| 337 | |
| 338 | .. doctest:: |
| 339 | |
| 340 | >>> print(ast.dump(ast.parse('-a'), indent=4)) |
| 341 | Module( |
| 342 | body=[ |
| 343 | Expr( |
| 344 | value=UnaryOp( |
| 345 | op=USub(), |
| 346 | operand=Name(id='a', ctx=Load())))], |
| 347 | type_ignores=[]) |
| 348 | |
| 349 | |
| 350 | .. class:: UnaryOp(op, operand) |
| 351 | |
| 352 | A unary operation. ``op`` is the operator, and ``operand`` any expression |
| 353 | node. |
| 354 | |
| 355 | |
| 356 | .. class:: UAdd |
| 357 | USub |
| 358 | Not |
| 359 | Invert |
| 360 | |
| 361 | Unary operator tokens. :class:`Not` is the ``not`` keyword, :class:`Invert` |
| 362 | is the ``~`` operator. |
| 363 | |
| 364 | .. doctest:: |
| 365 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 366 | >>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4)) |
| 367 | Expression( |
| 368 | body=UnaryOp( |
| 369 | op=Not(), |
| 370 | operand=Name(id='x', ctx=Load()))) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 371 | |
| 372 | |
| 373 | .. class:: BinOp(left, op, right) |
| 374 | |
| 375 | A binary operation (like addition or division). ``op`` is the operator, and |
| 376 | ``left`` and ``right`` are any expression nodes. |
| 377 | |
| 378 | .. doctest:: |
| 379 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 380 | >>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4)) |
| 381 | Expression( |
| 382 | body=BinOp( |
| 383 | left=Name(id='x', ctx=Load()), |
| 384 | op=Add(), |
| 385 | right=Name(id='y', ctx=Load()))) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 386 | |
| 387 | |
| 388 | .. class:: Add |
| 389 | Sub |
| 390 | Mult |
| 391 | Div |
| 392 | FloorDiv |
| 393 | Mod |
| 394 | Pow |
| 395 | LShift |
| 396 | RShift |
| 397 | BitOr |
| 398 | BitXor |
| 399 | BitAnd |
| 400 | MatMult |
| 401 | |
| 402 | Binary operator tokens. |
| 403 | |
| 404 | |
| 405 | .. class:: BoolOp(op, values) |
| 406 | |
| 407 | A boolean operation, 'or' or 'and'. ``op`` is :class:`Or` or :class:`And`. |
| 408 | ``values`` are the values involved. Consecutive operations with the same |
| 409 | operator, such as ``a or b or c``, are collapsed into one node with several |
| 410 | values. |
| 411 | |
| 412 | This doesn't include ``not``, which is a :class:`UnaryOp`. |
| 413 | |
| 414 | .. doctest:: |
| 415 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 416 | >>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4)) |
| 417 | Expression( |
| 418 | body=BoolOp( |
| 419 | op=Or(), |
| 420 | values=[ |
| 421 | Name(id='x', ctx=Load()), |
| 422 | Name(id='y', ctx=Load())])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 423 | |
| 424 | |
| 425 | .. class:: And |
| 426 | Or |
| 427 | |
| 428 | Boolean operator tokens. |
| 429 | |
| 430 | |
| 431 | .. class:: Compare(left, ops, comparators) |
| 432 | |
| 433 | A comparison of two or more values. ``left`` is the first value in the |
| 434 | comparison, ``ops`` the list of operators, and ``comparators`` the list |
| 435 | of values after the first element in the comparison. |
| 436 | |
| 437 | .. doctest:: |
| 438 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 439 | >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4)) |
| 440 | Expression( |
| 441 | body=Compare( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 442 | left=Constant(value=1), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 443 | ops=[ |
| 444 | LtE(), |
| 445 | Lt()], |
| 446 | comparators=[ |
| 447 | Name(id='a', ctx=Load()), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 448 | Constant(value=10)])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 449 | |
| 450 | |
| 451 | .. class:: Eq |
| 452 | NotEq |
| 453 | Lt |
| 454 | LtE |
| 455 | Gt |
| 456 | GtE |
| 457 | Is |
| 458 | IsNot |
| 459 | In |
| 460 | NotIn |
| 461 | |
| 462 | Comparison operator tokens. |
| 463 | |
| 464 | |
| 465 | .. class:: Call(func, args, keywords, starargs, kwargs) |
| 466 | |
| 467 | A function call. ``func`` is the function, which will often be a |
| 468 | :class:`Name` or :class:`Attribute` object. Of the arguments: |
| 469 | |
| 470 | * ``args`` holds a list of the arguments passed by position. |
| 471 | * ``keywords`` holds a list of :class:`keyword` objects representing |
| 472 | arguments passed by keyword. |
| 473 | |
| 474 | When creating a ``Call`` node, ``args`` and ``keywords`` are required, but |
| 475 | they can be empty lists. ``starargs`` and ``kwargs`` are optional. |
| 476 | |
| 477 | .. doctest:: |
| 478 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 479 | >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4)) |
| 480 | Expression( |
| 481 | body=Call( |
| 482 | func=Name(id='func', ctx=Load()), |
| 483 | args=[ |
| 484 | Name(id='a', ctx=Load()), |
| 485 | Starred( |
| 486 | value=Name(id='d', ctx=Load()), |
| 487 | ctx=Load())], |
| 488 | keywords=[ |
| 489 | keyword( |
| 490 | arg='b', |
| 491 | value=Name(id='c', ctx=Load())), |
| 492 | keyword( |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 493 | value=Name(id='e', ctx=Load()))])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | .. class:: keyword(arg, value) |
| 497 | |
| 498 | A keyword argument to a function call or class definition. ``arg`` is a raw |
| 499 | string of the parameter name, ``value`` is a node to pass in. |
| 500 | |
| 501 | |
| 502 | .. class:: IfExp(test, body, orelse) |
| 503 | |
| 504 | An expression such as ``a if b else c``. Each field holds a single node, so |
| 505 | in the following example, all three are :class:`Name` nodes. |
| 506 | |
| 507 | .. doctest:: |
| 508 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 509 | >>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4)) |
| 510 | Expression( |
| 511 | body=IfExp( |
| 512 | test=Name(id='b', ctx=Load()), |
| 513 | body=Name(id='a', ctx=Load()), |
| 514 | orelse=Name(id='c', ctx=Load()))) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 515 | |
| 516 | |
| 517 | .. class:: Attribute(value, attr, ctx) |
| 518 | |
| 519 | Attribute access, e.g. ``d.keys``. ``value`` is a node, typically a |
| 520 | :class:`Name`. ``attr`` is a bare string giving the name of the attribute, |
| 521 | and ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del` according to how |
| 522 | the attribute is acted on. |
| 523 | |
| 524 | .. doctest:: |
| 525 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 526 | >>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4)) |
| 527 | Expression( |
| 528 | body=Attribute( |
| 529 | value=Name(id='snake', ctx=Load()), |
| 530 | attr='colour', |
| 531 | ctx=Load())) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | .. class:: NamedExpr(target, value) |
| 535 | |
| 536 | A named expression. This AST node is produced by the assignment expressions |
| 537 | operator (also known as the walrus operator). As opposed to the :class:`Assign` |
| 538 | node in which the first argument can be multiple nodes, in this case both |
| 539 | ``target`` and ``value`` must be single nodes. |
| 540 | |
| 541 | .. doctest:: |
| 542 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 543 | >>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4)) |
| 544 | Expression( |
| 545 | body=NamedExpr( |
| 546 | target=Name(id='x', ctx=Store()), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 547 | value=Constant(value=4))) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | Subscripting |
| 551 | ~~~~~~~~~~~~ |
| 552 | |
| 553 | .. class:: Subscript(value, slice, ctx) |
| 554 | |
| 555 | A subscript, such as ``l[1]``. ``value`` is the object, often a |
| 556 | :class:`Name`. ``slice`` is one of :class:`Index`, :class:`Slice` or |
| 557 | :class:`ExtSlice`. ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del` |
| 558 | according to the action performed with the subscript. |
| 559 | |
| 560 | |
| 561 | .. class:: Index(value) |
| 562 | |
| 563 | Simple subscripting with a single value |
| 564 | |
| 565 | .. doctest:: |
| 566 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 567 | >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4)) |
| 568 | Expression( |
| 569 | body=Subscript( |
| 570 | value=Name(id='l', ctx=Load()), |
| 571 | slice=Index( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 572 | value=Constant(value=1)), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 573 | ctx=Load())) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 574 | |
| 575 | |
| 576 | .. class:: Slice(lower, upper, step) |
| 577 | |
| 578 | Regular slicing (on the form x:y). |
| 579 | |
| 580 | .. doctest:: |
| 581 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 582 | >>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4)) |
| 583 | Expression( |
| 584 | body=Subscript( |
| 585 | value=Name(id='l', ctx=Load()), |
| 586 | slice=Slice( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 587 | lower=Constant(value=1), |
| 588 | upper=Constant(value=2)), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 589 | ctx=Load())) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 590 | |
| 591 | |
| 592 | .. class:: ExtSlice(dims) |
| 593 | |
| 594 | Advanced slicing. ``dims`` holds a list of :class:`Slice` and |
| 595 | :class:`Index` nodes |
| 596 | |
| 597 | .. doctest:: |
| 598 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 599 | >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4)) |
| 600 | Expression( |
| 601 | body=Subscript( |
| 602 | value=Name(id='l', ctx=Load()), |
| 603 | slice=ExtSlice( |
| 604 | dims=[ |
| 605 | Slice( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 606 | lower=Constant(value=1), |
| 607 | upper=Constant(value=2)), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 608 | Index( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 609 | value=Constant(value=3))]), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 610 | ctx=Load())) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 611 | |
| 612 | |
| 613 | Comprehensions |
| 614 | ~~~~~~~~~~~~~~ |
| 615 | |
| 616 | .. class:: ListComp(elt, generators) |
| 617 | SetComp(elt, generators) |
| 618 | GeneratorExp(elt, generators) |
| 619 | DictComp(key, value, generators) |
| 620 | |
| 621 | List and set comprehensions, generator expressions, and dictionary |
| 622 | comprehensions. ``elt`` (or ``key`` and ``value``) is a single node |
| 623 | representing the part that will be evaluated for each item. |
| 624 | |
| 625 | ``generators`` is a list of :class:`comprehension` nodes. |
| 626 | |
| 627 | .. doctest:: |
| 628 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 629 | >>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4)) |
| 630 | Expression( |
| 631 | body=ListComp( |
| 632 | elt=Name(id='x', ctx=Load()), |
| 633 | generators=[ |
| 634 | comprehension( |
| 635 | target=Name(id='x', ctx=Store()), |
| 636 | iter=Name(id='numbers', ctx=Load()), |
| 637 | ifs=[], |
| 638 | is_async=0)])) |
| 639 | >>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4)) |
| 640 | Expression( |
| 641 | body=DictComp( |
| 642 | key=Name(id='x', ctx=Load()), |
| 643 | value=BinOp( |
| 644 | left=Name(id='x', ctx=Load()), |
| 645 | op=Pow(), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 646 | right=Constant(value=2)), |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 647 | generators=[ |
| 648 | comprehension( |
| 649 | target=Name(id='x', ctx=Store()), |
| 650 | iter=Name(id='numbers', ctx=Load()), |
| 651 | ifs=[], |
| 652 | is_async=0)])) |
| 653 | >>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4)) |
| 654 | Expression( |
| 655 | body=SetComp( |
| 656 | elt=Name(id='x', ctx=Load()), |
| 657 | generators=[ |
| 658 | comprehension( |
| 659 | target=Name(id='x', ctx=Store()), |
| 660 | iter=Name(id='numbers', ctx=Load()), |
| 661 | ifs=[], |
| 662 | is_async=0)])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 663 | |
| 664 | |
| 665 | .. class:: comprehension(target, iter, ifs, is_async) |
| 666 | |
| 667 | One ``for`` clause in a comprehension. ``target`` is the reference to use for |
| 668 | each element - typically a :class:`Name` or :class:`Tuple` node. ``iter`` |
| 669 | is the object to iterate over. ``ifs`` is a list of test expressions: each |
| 670 | ``for`` clause can have multiple ``ifs``. |
| 671 | |
| 672 | ``is_async`` indicates a comprehension is asynchronous (using an |
| 673 | ``async for`` instead of ``for``). The value is an integer (0 or 1). |
| 674 | |
| 675 | .. doctest:: |
| 676 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 677 | >>> print(ast.dump(ast.parse('[ord(c) for line in file for c in line]', mode='eval'), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 678 | ... indent=4)) # Multiple comprehensions in one. |
| 679 | Expression( |
| 680 | body=ListComp( |
| 681 | elt=Call( |
| 682 | func=Name(id='ord', ctx=Load()), |
| 683 | args=[ |
| 684 | Name(id='c', ctx=Load())], |
| 685 | keywords=[]), |
| 686 | generators=[ |
| 687 | comprehension( |
| 688 | target=Name(id='line', ctx=Store()), |
| 689 | iter=Name(id='file', ctx=Load()), |
| 690 | ifs=[], |
| 691 | is_async=0), |
| 692 | comprehension( |
| 693 | target=Name(id='c', ctx=Store()), |
| 694 | iter=Name(id='line', ctx=Load()), |
| 695 | ifs=[], |
| 696 | is_async=0)])) |
| 697 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 698 | >>> print(ast.dump(ast.parse('(n**2 for n in it if n>5 if n<10)', mode='eval'), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 699 | ... indent=4)) # generator comprehension |
| 700 | Expression( |
| 701 | body=GeneratorExp( |
| 702 | elt=BinOp( |
| 703 | left=Name(id='n', ctx=Load()), |
| 704 | op=Pow(), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 705 | right=Constant(value=2)), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 706 | generators=[ |
| 707 | comprehension( |
| 708 | target=Name(id='n', ctx=Store()), |
| 709 | iter=Name(id='it', ctx=Load()), |
| 710 | ifs=[ |
| 711 | Compare( |
| 712 | left=Name(id='n', ctx=Load()), |
| 713 | ops=[ |
| 714 | Gt()], |
| 715 | comparators=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 716 | Constant(value=5)]), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 717 | Compare( |
| 718 | left=Name(id='n', ctx=Load()), |
| 719 | ops=[ |
| 720 | Lt()], |
| 721 | comparators=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 722 | Constant(value=10)])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 723 | is_async=0)])) |
| 724 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 725 | >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 726 | ... indent=4)) # Async comprehension |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 727 | Expression( |
| 728 | body=ListComp( |
| 729 | elt=Name(id='i', ctx=Load()), |
| 730 | generators=[ |
| 731 | comprehension( |
| 732 | target=Name(id='i', ctx=Store()), |
| 733 | iter=Name(id='soc', ctx=Load()), |
| 734 | ifs=[], |
| 735 | is_async=1)])) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 736 | |
| 737 | Statements |
| 738 | ^^^^^^^^^^ |
| 739 | |
| 740 | .. class:: Assign(targets, value, type_comment) |
| 741 | |
| 742 | An assignment. ``targets`` is a list of nodes, and ``value`` is a single node. |
| 743 | |
| 744 | Multiple nodes in ``targets`` represents assigning the same value to each. |
| 745 | Unpacking is represented by putting a :class:`Tuple` or :class:`List` |
| 746 | within ``targets``. |
| 747 | |
| 748 | .. attribute:: type_comment |
| 749 | |
| 750 | ``type_comment`` is an optional string with the type annotation as a comment. |
| 751 | |
| 752 | .. doctest:: |
| 753 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 754 | >>> print(ast.dump(ast.parse('a = b = 1'), indent=4)) # Multiple assignment |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 755 | Module( |
| 756 | body=[ |
| 757 | Assign( |
| 758 | targets=[ |
| 759 | Name(id='a', ctx=Store()), |
| 760 | Name(id='b', ctx=Store())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 761 | value=Constant(value=1))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 762 | type_ignores=[]) |
| 763 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 764 | >>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 765 | Module( |
| 766 | body=[ |
| 767 | Assign( |
| 768 | targets=[ |
| 769 | Tuple( |
| 770 | elts=[ |
| 771 | Name(id='a', ctx=Store()), |
| 772 | Name(id='b', ctx=Store())], |
| 773 | ctx=Store())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 774 | value=Name(id='c', ctx=Load()))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 775 | type_ignores=[]) |
| 776 | |
| 777 | |
| 778 | .. class:: AnnAssign(target, annotation, value, simple) |
| 779 | |
| 780 | An assignment with a type annotation. ``target`` is a single node and can |
| 781 | be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`. |
| 782 | ``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name` |
| 783 | node. ``value`` is a single optional node. ``simple`` is a boolean integer |
| 784 | set to True for a :class:`Name` node in ``target`` that do not appear in |
| 785 | between parenthesis and are hence pure names and not expressions. |
| 786 | |
| 787 | .. doctest:: |
| 788 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 789 | >>> print(ast.dump(ast.parse('c: int'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 790 | Module( |
| 791 | body=[ |
| 792 | AnnAssign( |
| 793 | target=Name(id='c', ctx=Store()), |
| 794 | annotation=Name(id='int', ctx=Load()), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 795 | simple=1)], |
| 796 | type_ignores=[]) |
| 797 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 798 | >>> print(ast.dump(ast.parse('(a): int = 1'), indent=4)) # Annotation with parenthesis |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 799 | Module( |
| 800 | body=[ |
| 801 | AnnAssign( |
| 802 | target=Name(id='a', ctx=Store()), |
| 803 | annotation=Name(id='int', ctx=Load()), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 804 | value=Constant(value=1), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 805 | simple=0)], |
| 806 | type_ignores=[]) |
| 807 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 808 | >>> print(ast.dump(ast.parse('a.b: int'), indent=4)) # Attribute annotation |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 809 | Module( |
| 810 | body=[ |
| 811 | AnnAssign( |
| 812 | target=Attribute( |
| 813 | value=Name(id='a', ctx=Load()), |
| 814 | attr='b', |
| 815 | ctx=Store()), |
| 816 | annotation=Name(id='int', ctx=Load()), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 817 | simple=0)], |
| 818 | type_ignores=[]) |
| 819 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 820 | >>> print(ast.dump(ast.parse('a[1]: int'), indent=4)) # Subscript annotation |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 821 | Module( |
| 822 | body=[ |
| 823 | AnnAssign( |
| 824 | target=Subscript( |
| 825 | value=Name(id='a', ctx=Load()), |
| 826 | slice=Index( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 827 | value=Constant(value=1)), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 828 | ctx=Store()), |
| 829 | annotation=Name(id='int', ctx=Load()), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 830 | simple=0)], |
| 831 | type_ignores=[]) |
| 832 | |
| 833 | |
| 834 | .. class:: AugAssign(target, op, value) |
| 835 | |
| 836 | Augmented assignment, such as ``a += 1``. In the following example, |
| 837 | ``target`` is a :class:`Name` node for ``x`` (with the :class:`Store` |
| 838 | context), ``op`` is :class:`Add`, and ``value`` is a :class:`Constant` with |
| 839 | value for 1. |
| 840 | |
| 841 | The ``target`` attribute connot be of class :class:`Tuple` or :class:`List`, |
| 842 | unlike the targets of :class:`Assign`. |
| 843 | |
| 844 | .. doctest:: |
| 845 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 846 | >>> print(ast.dump(ast.parse('x += 2'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 847 | Module( |
| 848 | body=[ |
| 849 | AugAssign( |
| 850 | target=Name(id='x', ctx=Store()), |
| 851 | op=Add(), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 852 | value=Constant(value=2))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 853 | type_ignores=[]) |
| 854 | |
| 855 | |
| 856 | .. class:: Raise(exc, cause) |
| 857 | |
| 858 | A ``raise`` statement. ``exc`` is the exception object to be raised, normally a |
| 859 | :class:`Call` or :class:`Name`, or ``None`` for a standalone ``raise``. |
| 860 | ``cause`` is the optional part for ``y`` in ``raise x from y``. |
| 861 | |
| 862 | .. doctest:: |
| 863 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 864 | >>> print(ast.dump(ast.parse('raise x from y'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 865 | Module( |
| 866 | body=[ |
| 867 | Raise( |
| 868 | exc=Name(id='x', ctx=Load()), |
| 869 | cause=Name(id='y', ctx=Load()))], |
| 870 | type_ignores=[]) |
| 871 | |
| 872 | |
| 873 | .. class:: Assert(test, msg) |
| 874 | |
| 875 | An assertion. ``test`` holds the condition, such as a :class:`Compare` node. |
| 876 | ``msg`` holds the failure message. |
| 877 | |
| 878 | .. doctest:: |
| 879 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 880 | >>> print(ast.dump(ast.parse('assert x,y'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 881 | Module( |
| 882 | body=[ |
| 883 | Assert( |
| 884 | test=Name(id='x', ctx=Load()), |
| 885 | msg=Name(id='y', ctx=Load()))], |
| 886 | type_ignores=[]) |
| 887 | |
| 888 | |
| 889 | .. class:: Delete(targets) |
| 890 | |
| 891 | Represents a ``del`` statement. ``targets`` is a list of nodes, such as |
| 892 | :class:`Name`, :class:`Attribute` or :class:`Subscript` nodes. |
| 893 | |
| 894 | .. doctest:: |
| 895 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 896 | >>> print(ast.dump(ast.parse('del x,y,z'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 897 | Module( |
| 898 | body=[ |
| 899 | Delete( |
| 900 | targets=[ |
| 901 | Name(id='x', ctx=Del()), |
| 902 | Name(id='y', ctx=Del()), |
| 903 | Name(id='z', ctx=Del())])], |
| 904 | type_ignores=[]) |
| 905 | |
| 906 | |
| 907 | .. class:: Pass() |
| 908 | |
| 909 | A ``pass`` statement. |
| 910 | |
| 911 | .. doctest:: |
| 912 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 913 | >>> print(ast.dump(ast.parse('pass'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 914 | Module( |
| 915 | body=[ |
| 916 | Pass()], |
| 917 | type_ignores=[]) |
| 918 | |
| 919 | |
| 920 | Other statements which are only applicable inside functions or loops are |
| 921 | described in other sections. |
| 922 | |
| 923 | Imports |
| 924 | ~~~~~~~ |
| 925 | |
| 926 | .. class:: Import(names) |
| 927 | |
| 928 | An import statement. ``names`` is a list of :class:`alias` nodes. |
| 929 | |
| 930 | .. doctest:: |
| 931 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 932 | >>> print(ast.dump(ast.parse('import x,y,z'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 933 | Module( |
| 934 | body=[ |
| 935 | Import( |
| 936 | names=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 937 | alias(name='x'), |
| 938 | alias(name='y'), |
| 939 | alias(name='z')])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 940 | type_ignores=[]) |
| 941 | |
| 942 | |
| 943 | .. class:: ImportFrom(module, names, level) |
| 944 | |
| 945 | Represents ``from x import y``. ``module`` is a raw string of the 'from' name, |
| 946 | without any leading dots, or ``None`` for statements such as ``from . import foo``. |
| 947 | ``level`` is an integer holding the level of the relative import (0 means |
| 948 | absolute import). |
| 949 | |
| 950 | .. doctest:: |
| 951 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 952 | >>> print(ast.dump(ast.parse('from y import x,y,z'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 953 | Module( |
| 954 | body=[ |
| 955 | ImportFrom( |
| 956 | module='y', |
| 957 | names=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 958 | alias(name='x'), |
| 959 | alias(name='y'), |
| 960 | alias(name='z')], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 961 | level=0)], |
| 962 | type_ignores=[]) |
| 963 | |
| 964 | |
| 965 | .. class:: alias(name, asname) |
| 966 | |
| 967 | Both parameters are raw strings of the names. ``asname`` can be ``None`` if |
| 968 | the regular name is to be used. |
| 969 | |
| 970 | .. doctest:: |
| 971 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 972 | >>> print(ast.dump(ast.parse('from ..foo.bar import a as b, c'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 973 | Module( |
| 974 | body=[ |
| 975 | ImportFrom( |
| 976 | module='foo.bar', |
| 977 | names=[ |
| 978 | alias(name='a', asname='b'), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 979 | alias(name='c')], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 980 | level=2)], |
| 981 | type_ignores=[]) |
| 982 | |
| 983 | Control flow |
| 984 | ^^^^^^^^^^^^ |
| 985 | |
| 986 | .. note:: |
| 987 | Optional clauses such as ``else`` are stored as an empty list if they're |
| 988 | not present. |
| 989 | |
| 990 | .. class:: If(test, body, orelse) |
| 991 | |
| 992 | An ``if`` statement. ``test`` holds a single node, such as a :class:`Compare` |
| 993 | node. ``body`` and ``orelse`` each hold a list of nodes. |
| 994 | |
| 995 | ``elif`` clauses don't have a special representation in the AST, but rather |
| 996 | appear as extra :class:`If` nodes within the ``orelse`` section of the |
| 997 | previous one. |
| 998 | |
| 999 | .. doctest:: |
| 1000 | |
| 1001 | >>> print(ast.dump(ast.parse(""" |
| 1002 | ... if x: |
| 1003 | ... ... |
| 1004 | ... elif y: |
| 1005 | ... ... |
| 1006 | ... else: |
| 1007 | ... ... |
| 1008 | ... """), indent=4)) |
| 1009 | Module( |
| 1010 | body=[ |
| 1011 | If( |
| 1012 | test=Name(id='x', ctx=Load()), |
| 1013 | body=[ |
| 1014 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1015 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1016 | orelse=[ |
| 1017 | If( |
| 1018 | test=Name(id='y', ctx=Load()), |
| 1019 | body=[ |
| 1020 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1021 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1022 | orelse=[ |
| 1023 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1024 | value=Constant(value=Ellipsis))])])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1025 | type_ignores=[]) |
| 1026 | |
| 1027 | |
| 1028 | .. class:: For(target, iter, body, orelse, type_comment) |
| 1029 | |
| 1030 | A ``for`` loop. ``target`` holds the variable(s) the loop assigns to, as a |
| 1031 | single :class:`Name`, :class:`Tuple` or :class:`List` node. ``iter`` holds |
| 1032 | the item to be looped over, again as a single node. ``body`` and ``orelse`` |
| 1033 | contain lists of nodes to execute. Those in ``orelse`` are executed if the |
| 1034 | loop finishes normally, rather than via a ``break`` statement. |
| 1035 | |
| 1036 | .. attribute:: type_comment |
| 1037 | |
| 1038 | ``type_comment`` is an optional string with the type annotation as a comment. |
| 1039 | |
| 1040 | .. doctest:: |
| 1041 | |
| 1042 | >>> print(ast.dump(ast.parse(""" |
| 1043 | ... for x in y: |
| 1044 | ... ... |
| 1045 | ... else: |
| 1046 | ... ... |
| 1047 | ... """), indent=4)) |
| 1048 | Module( |
| 1049 | body=[ |
| 1050 | For( |
| 1051 | target=Name(id='x', ctx=Store()), |
| 1052 | iter=Name(id='y', ctx=Load()), |
| 1053 | body=[ |
| 1054 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1055 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1056 | orelse=[ |
| 1057 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1058 | value=Constant(value=Ellipsis))])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1059 | type_ignores=[]) |
| 1060 | |
| 1061 | |
| 1062 | .. class:: While(test, body, orelse) |
| 1063 | |
| 1064 | A ``while`` loop. ``test`` holds the condition, such as a :class:`Compare` |
| 1065 | node. |
| 1066 | |
| 1067 | .. doctest:: |
| 1068 | |
| 1069 | >> print(ast.dump(ast.parse(""" |
| 1070 | ... while x: |
| 1071 | ... ... |
| 1072 | ... else: |
| 1073 | ... ... |
| 1074 | ... """), indent=4)) |
| 1075 | Module( |
| 1076 | body=[ |
| 1077 | While( |
| 1078 | test=Name(id='x', ctx=Load()), |
| 1079 | body=[ |
| 1080 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1081 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1082 | orelse=[ |
| 1083 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1084 | value=Constant(value=Ellipsis))])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1085 | type_ignores=[]) |
| 1086 | |
| 1087 | |
| 1088 | .. class:: Break |
| 1089 | Continue |
| 1090 | |
| 1091 | The ``break`` and ``continue`` statements. |
| 1092 | |
| 1093 | .. doctest:: |
| 1094 | |
| 1095 | >>> print(ast.dump(ast.parse("""\ |
| 1096 | ... for a in b: |
| 1097 | ... if a > 5: |
| 1098 | ... break |
| 1099 | ... else: |
| 1100 | ... continue |
| 1101 | ... |
| 1102 | ... """), indent=4)) |
| 1103 | Module( |
| 1104 | body=[ |
| 1105 | For( |
| 1106 | target=Name(id='a', ctx=Store()), |
| 1107 | iter=Name(id='b', ctx=Load()), |
| 1108 | body=[ |
| 1109 | If( |
| 1110 | test=Compare( |
| 1111 | left=Name(id='a', ctx=Load()), |
| 1112 | ops=[ |
| 1113 | Gt()], |
| 1114 | comparators=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1115 | Constant(value=5)]), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1116 | body=[ |
| 1117 | Break()], |
| 1118 | orelse=[ |
| 1119 | Continue()])], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1120 | orelse=[])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1121 | type_ignores=[]) |
| 1122 | |
| 1123 | |
| 1124 | .. class:: Try(body, handlers, orelse, finalbody) |
| 1125 | |
| 1126 | ``try`` blocks. All attributes are list of nodes to execute, except for |
| 1127 | ``handlers``, which is a list of :class:`ExceptHandler` nodes. |
| 1128 | |
| 1129 | .. doctest:: |
| 1130 | |
| 1131 | >>> print(ast.dump(ast.parse(""" |
| 1132 | ... try: |
| 1133 | ... ... |
| 1134 | ... except Exception: |
| 1135 | ... ... |
| 1136 | ... except OtherException as e: |
| 1137 | ... ... |
| 1138 | ... else: |
| 1139 | ... ... |
| 1140 | ... finally: |
| 1141 | ... ... |
| 1142 | ... """), indent=4)) |
| 1143 | Module( |
| 1144 | body=[ |
| 1145 | Try( |
| 1146 | body=[ |
| 1147 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1148 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1149 | handlers=[ |
| 1150 | ExceptHandler( |
| 1151 | type=Name(id='Exception', ctx=Load()), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1152 | body=[ |
| 1153 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1154 | value=Constant(value=Ellipsis))]), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1155 | ExceptHandler( |
| 1156 | type=Name(id='OtherException', ctx=Load()), |
| 1157 | name='e', |
| 1158 | body=[ |
| 1159 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1160 | value=Constant(value=Ellipsis))])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1161 | orelse=[ |
| 1162 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1163 | value=Constant(value=Ellipsis))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1164 | finalbody=[ |
| 1165 | Expr( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1166 | value=Constant(value=Ellipsis))])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1167 | type_ignores=[]) |
| 1168 | |
| 1169 | |
| 1170 | .. class:: ExceptHandler(type, name, body) |
| 1171 | |
| 1172 | A single ``except`` clause. ``type`` is the exception type it will match, |
| 1173 | typically a :class:`Name` node (or ``None`` for a catch-all ``except:`` clause). |
| 1174 | ``name`` is a raw string for the name to hold the exception, or ``None`` if |
| 1175 | the clause doesn't have ``as foo``. ``body`` is a list of nodes. |
| 1176 | |
| 1177 | .. doctest:: |
| 1178 | |
| 1179 | >>> print(ast.dump(ast.parse("""\ |
| 1180 | ... try: |
| 1181 | ... a + 1 |
| 1182 | ... except TypeError: |
| 1183 | ... pass |
| 1184 | ... """), indent=4)) |
| 1185 | Module( |
| 1186 | body=[ |
| 1187 | Try( |
| 1188 | body=[ |
| 1189 | Expr( |
| 1190 | value=BinOp( |
| 1191 | left=Name(id='a', ctx=Load()), |
| 1192 | op=Add(), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1193 | right=Constant(value=1)))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1194 | handlers=[ |
| 1195 | ExceptHandler( |
| 1196 | type=Name(id='TypeError', ctx=Load()), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1197 | body=[ |
| 1198 | Pass()])], |
| 1199 | orelse=[], |
| 1200 | finalbody=[])], |
| 1201 | type_ignores=[]) |
| 1202 | |
| 1203 | |
| 1204 | .. class:: With(items, body, type_comment) |
| 1205 | |
| 1206 | A ``with`` block. ``items`` is a list of :class:`withitem` nodes representing |
| 1207 | the context managers, and ``body`` is the indented block inside the context. |
| 1208 | |
| 1209 | .. attribute:: type_comment |
| 1210 | |
| 1211 | ``type_comment`` is an optional string with the type annotation as a comment. |
| 1212 | |
| 1213 | |
| 1214 | .. class:: withitem(context_expr, optional_vars) |
| 1215 | |
| 1216 | A single context manager in a ``with`` block. ``context_expr`` is the context |
| 1217 | manager, often a :class:`Call` node. ``optional_vars`` is a :class:`Name`, |
| 1218 | :class:`Tuple` or :class:`List` for the ``as foo`` part, or ``None`` if that |
| 1219 | isn't used. |
| 1220 | |
| 1221 | .. doctest:: |
| 1222 | |
| 1223 | >>> print(ast.dump(ast.parse("""\ |
| 1224 | ... with a as b, c as d: |
| 1225 | ... something(b, d) |
| 1226 | ... """), indent=4)) |
| 1227 | Module( |
| 1228 | body=[ |
| 1229 | With( |
| 1230 | items=[ |
| 1231 | withitem( |
| 1232 | context_expr=Name(id='a', ctx=Load()), |
| 1233 | optional_vars=Name(id='b', ctx=Store())), |
| 1234 | withitem( |
| 1235 | context_expr=Name(id='c', ctx=Load()), |
| 1236 | optional_vars=Name(id='d', ctx=Store()))], |
| 1237 | body=[ |
| 1238 | Expr( |
| 1239 | value=Call( |
| 1240 | func=Name(id='something', ctx=Load()), |
| 1241 | args=[ |
| 1242 | Name(id='b', ctx=Load()), |
| 1243 | Name(id='d', ctx=Load())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1244 | keywords=[]))])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1245 | type_ignores=[]) |
| 1246 | |
| 1247 | |
| 1248 | Function and class definitions |
| 1249 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1250 | |
| 1251 | .. class:: FunctionDef(name, args, body, decorator_list, returns, type_comment) |
| 1252 | |
| 1253 | A function definition. |
| 1254 | |
| 1255 | * ``name`` is a raw string of the function name. |
| 1256 | * ``args`` is a :class:`arguments` node. |
| 1257 | * ``body`` is the list of nodes inside the function. |
| 1258 | * ``decorator_list`` is the list of decorators to be applied, stored outermost |
| 1259 | first (i.e. the first in the list will be applied last). |
| 1260 | * ``returns`` is the return annotation. |
| 1261 | |
| 1262 | .. attribute:: type_comment |
| 1263 | |
| 1264 | ``type_comment`` is an optional string with the type annotation as a comment. |
| 1265 | |
| 1266 | |
| 1267 | .. class:: Lambda(args, body) |
| 1268 | |
| 1269 | ``lambda`` is a minimal function definition that can be used inside an |
| 1270 | expression. Unlike :class:`FunctionDef`, ``body`` holds a single node. |
| 1271 | |
| 1272 | .. doctest:: |
| 1273 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1274 | >>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1275 | Module( |
| 1276 | body=[ |
| 1277 | Expr( |
| 1278 | value=Lambda( |
| 1279 | args=arguments( |
| 1280 | posonlyargs=[], |
| 1281 | args=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1282 | arg(arg='x'), |
| 1283 | arg(arg='y')], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1284 | kwonlyargs=[], |
| 1285 | kw_defaults=[], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1286 | defaults=[]), |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1287 | body=Constant(value=Ellipsis)))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1288 | type_ignores=[]) |
| 1289 | |
| 1290 | |
| 1291 | .. class:: arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults) |
| 1292 | |
| 1293 | The arguments for a function. |
| 1294 | |
| 1295 | * ``posonlyargs``, ``args`` and ``kwonlyargs`` are lists of :class:`arg` nodes. |
| 1296 | * ``vararg`` and ``kwarg`` are single :class:`arg` nodes, referring to the |
| 1297 | ``*args, **kwargs`` parameters. |
| 1298 | * ``kw_defaults`` is a list of default values for keyword-only arguments. If |
| 1299 | one is ``None``, the corresponding argument is required. |
| 1300 | * ``defaults`` is a list of default values for arguments that can be passed |
| 1301 | positionally. If there are fewer defaults, they correspond to the last n |
| 1302 | arguments. |
| 1303 | |
| 1304 | |
| 1305 | .. class:: arg(arg, annotation, type_comment) |
| 1306 | |
| 1307 | A single argument in a list. ``arg`` is a raw string of the argument |
| 1308 | name, ``annotation`` is its annotation, such as a :class:`Str` or |
| 1309 | :class:`Name` node. |
| 1310 | |
| 1311 | .. attribute:: type_comment |
| 1312 | |
| 1313 | ``type_comment`` is an optional string with the type annotation as a comment |
| 1314 | |
| 1315 | .. doctest:: |
| 1316 | |
| 1317 | >>> print(ast.dump(ast.parse("""\ |
| 1318 | ... @decorator1 |
| 1319 | ... @decorator2 |
| 1320 | ... def f(a: 'annotation', b=1, c=2, *d, e, f=3, **g) -> 'return annotation': |
| 1321 | ... pass |
| 1322 | ... """), indent=4)) |
| 1323 | Module( |
| 1324 | body=[ |
| 1325 | FunctionDef( |
| 1326 | name='f', |
| 1327 | args=arguments( |
| 1328 | posonlyargs=[], |
| 1329 | args=[ |
| 1330 | arg( |
| 1331 | arg='a', |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1332 | annotation=Constant(value='annotation')), |
| 1333 | arg(arg='b'), |
| 1334 | arg(arg='c')], |
| 1335 | vararg=arg(arg='d'), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1336 | kwonlyargs=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1337 | arg(arg='e'), |
| 1338 | arg(arg='f')], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1339 | kw_defaults=[ |
| 1340 | None, |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1341 | Constant(value=3)], |
| 1342 | kwarg=arg(arg='g'), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1343 | defaults=[ |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1344 | Constant(value=1), |
| 1345 | Constant(value=2)]), |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1346 | body=[ |
| 1347 | Pass()], |
| 1348 | decorator_list=[ |
| 1349 | Name(id='decorator1', ctx=Load()), |
| 1350 | Name(id='decorator2', ctx=Load())], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1351 | returns=Constant(value='return annotation'))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1352 | type_ignores=[]) |
| 1353 | |
| 1354 | |
| 1355 | .. class:: Return(value) |
| 1356 | |
| 1357 | A ``return`` statement. |
| 1358 | |
| 1359 | .. doctest:: |
| 1360 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1361 | >>> print(ast.dump(ast.parse('return 4'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1362 | Module( |
| 1363 | body=[ |
| 1364 | Return( |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1365 | value=Constant(value=4))], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1366 | type_ignores=[]) |
| 1367 | |
| 1368 | |
| 1369 | .. class:: Yield(value) |
| 1370 | YieldFrom(value) |
| 1371 | |
| 1372 | A ``yield`` or ``yield from`` expression. Because these are expressions, they |
| 1373 | must be wrapped in a :class:`Expr` node if the value sent back is not used. |
| 1374 | |
| 1375 | .. doctest:: |
| 1376 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1377 | >>> print(ast.dump(ast.parse('yield x'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1378 | Module( |
| 1379 | body=[ |
| 1380 | Expr( |
| 1381 | value=Yield( |
| 1382 | value=Name(id='x', ctx=Load())))], |
| 1383 | type_ignores=[]) |
| 1384 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1385 | >>> print(ast.dump(ast.parse('yield from x'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1386 | Module( |
| 1387 | body=[ |
| 1388 | Expr( |
| 1389 | value=YieldFrom( |
| 1390 | value=Name(id='x', ctx=Load())))], |
| 1391 | type_ignores=[]) |
| 1392 | |
| 1393 | |
| 1394 | .. class:: Global(names) |
| 1395 | Nonlocal(names) |
| 1396 | |
| 1397 | ``global`` and ``nonlocal`` statements. ``names`` is a list of raw strings. |
| 1398 | |
| 1399 | .. doctest:: |
| 1400 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1401 | >>> print(ast.dump(ast.parse('global x,y,z'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1402 | Module( |
| 1403 | body=[ |
| 1404 | Global( |
| 1405 | names=[ |
| 1406 | 'x', |
| 1407 | 'y', |
| 1408 | 'z'])], |
| 1409 | type_ignores=[]) |
| 1410 | |
Pablo Galindo | 02f64cb | 2020-03-07 18:22:58 +0000 | [diff] [blame] | 1411 | >>> print(ast.dump(ast.parse('nonlocal x,y,z'), indent=4)) |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1412 | Module( |
| 1413 | body=[ |
| 1414 | Nonlocal( |
| 1415 | names=[ |
| 1416 | 'x', |
| 1417 | 'y', |
| 1418 | 'z'])], |
| 1419 | type_ignores=[]) |
| 1420 | |
| 1421 | |
| 1422 | .. class:: ClassDef(name, bases, keywords, starargs, kwargs, body, decorator_list) |
| 1423 | |
| 1424 | A class definition. |
| 1425 | |
| 1426 | * ``name`` is a raw string for the class name |
| 1427 | * ``bases`` is a list of nodes for explicitly specified base classes. |
| 1428 | * ``keywords`` is a list of :class:`keyword` nodes, principally for 'metaclass'. |
| 1429 | Other keywords will be passed to the metaclass, as per `PEP-3115 |
| 1430 | <http://www.python.org/dev/peps/pep-3115/>`_. |
| 1431 | * ``starargs`` and ``kwargs`` are each a single node, as in a function call. |
| 1432 | starargs will be expanded to join the list of base classes, and kwargs will |
| 1433 | be passed to the metaclass. |
| 1434 | * ``body`` is a list of nodes representing the code within the class |
| 1435 | definition. |
| 1436 | * ``decorator_list`` is a list of nodes, as in :class:`FunctionDef`. |
| 1437 | |
| 1438 | .. doctest:: |
| 1439 | |
| 1440 | >>> print(ast.dump(ast.parse("""\ |
| 1441 | ... @decorator1 |
| 1442 | ... @decorator2 |
| 1443 | ... class Foo(base1, base2, metaclass=meta): |
| 1444 | ... pass |
| 1445 | ... """), indent=4)) |
| 1446 | Module( |
| 1447 | body=[ |
| 1448 | ClassDef( |
| 1449 | name='Foo', |
| 1450 | bases=[ |
| 1451 | Name(id='base1', ctx=Load()), |
| 1452 | Name(id='base2', ctx=Load())], |
| 1453 | keywords=[ |
| 1454 | keyword( |
| 1455 | arg='metaclass', |
| 1456 | value=Name(id='meta', ctx=Load()))], |
| 1457 | body=[ |
| 1458 | Pass()], |
| 1459 | decorator_list=[ |
| 1460 | Name(id='decorator1', ctx=Load()), |
| 1461 | Name(id='decorator2', ctx=Load())])], |
| 1462 | type_ignores=[]) |
| 1463 | |
| 1464 | Async and await |
| 1465 | ^^^^^^^^^^^^^^^ |
| 1466 | |
| 1467 | .. class:: AsyncFunctionDef(name, args, body, decorator_list, returns, type_comment) |
| 1468 | |
| 1469 | An ``async def`` function definition. Has the same fields as |
| 1470 | :class:`FunctionDef`. |
| 1471 | |
| 1472 | |
| 1473 | .. class:: Await(value) |
| 1474 | |
| 1475 | An ``await`` expression. ``value`` is what it waits for. |
| 1476 | Only valid in the body of an :class:`AsyncFunctionDef`. |
| 1477 | |
| 1478 | .. doctest:: |
| 1479 | |
| 1480 | >>> print(ast.dump(ast.parse("""\ |
| 1481 | ... async def f(): |
| 1482 | ... await other_func() |
| 1483 | ... """), indent=4)) |
| 1484 | Module( |
| 1485 | body=[ |
| 1486 | AsyncFunctionDef( |
| 1487 | name='f', |
| 1488 | args=arguments( |
| 1489 | posonlyargs=[], |
| 1490 | args=[], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1491 | kwonlyargs=[], |
| 1492 | kw_defaults=[], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1493 | defaults=[]), |
| 1494 | body=[ |
| 1495 | Expr( |
| 1496 | value=Await( |
| 1497 | value=Call( |
| 1498 | func=Name(id='other_func', ctx=Load()), |
| 1499 | args=[], |
| 1500 | keywords=[])))], |
Serhiy Storchaka | b7e9525 | 2020-03-10 00:07:47 +0200 | [diff] [blame^] | 1501 | decorator_list=[])], |
Pablo Galindo | 114081f | 2020-03-02 03:14:06 +0000 | [diff] [blame] | 1502 | type_ignores=[]) |
| 1503 | |
| 1504 | |
| 1505 | .. class:: AsyncFor(target, iter, body, orelse, type_comment) |
| 1506 | AsyncWith(items, body, type_comment) |
| 1507 | |
| 1508 | ``async for`` loops and ``async with`` context managers. They have the same |
| 1509 | fields as :class:`For` and :class:`With`, respectively. Only valid in the |
| 1510 | body of an :class:`AsyncFunctionDef`. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1511 | |
| 1512 | |
| 1513 | :mod:`ast` Helpers |
| 1514 | ------------------ |
| 1515 | |
Martin Panter | 2e4571a | 2015-11-14 01:07:43 +0000 | [diff] [blame] | 1516 | Apart from the node classes, the :mod:`ast` module defines these utility functions |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1517 | and classes for traversing abstract syntax trees: |
| 1518 | |
Guido van Rossum | 10b55c1 | 2019-06-11 17:23:12 -0700 | [diff] [blame] | 1519 | .. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1520 | |
Terry Reedy | feac624 | 2011-01-24 21:36:03 +0000 | [diff] [blame] | 1521 | Parse the source into an AST node. Equivalent to ``compile(source, |
Benjamin Peterson | ec9199b | 2008-11-08 17:05:00 +0000 | [diff] [blame] | 1522 | filename, mode, ast.PyCF_ONLY_AST)``. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1523 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1524 | If ``type_comments=True`` is given, the parser is modified to check |
| 1525 | and return type comments as specified by :pep:`484` and :pep:`526`. |
| 1526 | This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the |
| 1527 | flags passed to :func:`compile()`. This will report syntax errors |
| 1528 | for misplaced type comments. Without this flag, type comments will |
| 1529 | be ignored, and the ``type_comment`` field on selected AST nodes |
| 1530 | will always be ``None``. In addition, the locations of ``# type: |
| 1531 | ignore`` comments will be returned as the ``type_ignores`` |
| 1532 | attribute of :class:`Module` (otherwise it is always an empty list). |
| 1533 | |
| 1534 | In addition, if ``mode`` is ``'func_type'``, the input syntax is |
| 1535 | modified to correspond to :pep:`484` "signature type comments", |
| 1536 | e.g. ``(str, int) -> List[str]``. |
| 1537 | |
Guido van Rossum | 10b55c1 | 2019-06-11 17:23:12 -0700 | [diff] [blame] | 1538 | Also, setting ``feature_version`` to a tuple ``(major, minor)`` |
| 1539 | will attempt to parse using that Python version's grammar. |
| 1540 | Currently ``major`` must equal to ``3``. For example, setting |
| 1541 | ``feature_version=(3, 4)`` will allow the use of ``async`` and |
| 1542 | ``await`` as variable names. The lowest supported version is |
| 1543 | ``(3, 4)``; the highest is ``sys.version_info[0:2]``. |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1544 | |
Brett Cannon | 7a7f100 | 2018-03-09 12:03:22 -0800 | [diff] [blame] | 1545 | .. warning:: |
| 1546 | It is possible to crash the Python interpreter with a |
| 1547 | sufficiently large/complex string due to stack depth limitations |
| 1548 | in Python's AST compiler. |
| 1549 | |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1550 | .. versionchanged:: 3.8 |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 1551 | Added ``type_comments``, ``mode='func_type'`` and ``feature_version``. |
Guido van Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 1552 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1553 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 1554 | .. function:: unparse(ast_obj) |
| 1555 | |
| 1556 | Unparse an :class:`ast.AST` object and generate a string with code |
| 1557 | that would produce an equivalent :class:`ast.AST` object if parsed |
| 1558 | back with :func:`ast.parse`. |
| 1559 | |
| 1560 | .. warning:: |
Gurupad Hegde | 6c7bb38 | 2019-12-28 17:16:02 -0500 | [diff] [blame] | 1561 | The produced code string will not necessarily be equal to the original |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 1562 | code that generated the :class:`ast.AST` object. |
| 1563 | |
| 1564 | .. versionadded:: 3.9 |
| 1565 | |
| 1566 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1567 | .. function:: literal_eval(node_or_string) |
| 1568 | |
Georg Brandl | b9b389e | 2014-11-05 20:20:28 +0100 | [diff] [blame] | 1569 | Safely evaluate an expression node or a string containing a Python literal or |
| 1570 | container display. The string or node provided may only consist of the |
| 1571 | following Python literal structures: strings, bytes, numbers, tuples, lists, |
| 1572 | dicts, sets, booleans, and ``None``. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1573 | |
Georg Brandl | b9b389e | 2014-11-05 20:20:28 +0100 | [diff] [blame] | 1574 | This can be used for safely evaluating strings containing Python values from |
| 1575 | untrusted sources without the need to parse the values oneself. It is not |
| 1576 | capable of evaluating arbitrarily complex expressions, for example involving |
| 1577 | operators or indexing. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1578 | |
Brett Cannon | 7a7f100 | 2018-03-09 12:03:22 -0800 | [diff] [blame] | 1579 | .. warning:: |
| 1580 | It is possible to crash the Python interpreter with a |
| 1581 | sufficiently large/complex string due to stack depth limitations |
| 1582 | in Python's AST compiler. |
| 1583 | |
Georg Brandl | 492f3fc | 2010-07-11 09:41:21 +0000 | [diff] [blame] | 1584 | .. versionchanged:: 3.2 |
Georg Brandl | 85f2177 | 2010-07-13 06:38:10 +0000 | [diff] [blame] | 1585 | Now allows bytes and set literals. |
Georg Brandl | 492f3fc | 2010-07-11 09:41:21 +0000 | [diff] [blame] | 1586 | |
Raymond Hettinger | 4fcf5c1 | 2020-01-02 22:21:18 -0700 | [diff] [blame] | 1587 | .. versionchanged:: 3.9 |
| 1588 | Now supports creating empty sets with ``'set()'``. |
| 1589 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1590 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1591 | .. function:: get_docstring(node, clean=True) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1592 | |
| 1593 | Return the docstring of the given *node* (which must be a |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1594 | :class:`FunctionDef`, :class:`AsyncFunctionDef`, :class:`ClassDef`, |
| 1595 | or :class:`Module` node), or ``None`` if it has no docstring. |
| 1596 | If *clean* is true, clean up the docstring's indentation with |
| 1597 | :func:`inspect.cleandoc`. |
| 1598 | |
| 1599 | .. versionchanged:: 3.5 |
| 1600 | :class:`AsyncFunctionDef` is now supported. |
| 1601 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1602 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1603 | .. function:: get_source_segment(source, node, *, padded=False) |
| 1604 | |
| 1605 | Get source code segment of the *source* that generated *node*. |
| 1606 | If some location information (:attr:`lineno`, :attr:`end_lineno`, |
| 1607 | :attr:`col_offset`, or :attr:`end_col_offset`) is missing, return ``None``. |
| 1608 | |
| 1609 | If *padded* is ``True``, the first line of a multi-line statement will |
| 1610 | be padded with spaces to match its original position. |
| 1611 | |
| 1612 | .. versionadded:: 3.8 |
| 1613 | |
| 1614 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1615 | .. function:: fix_missing_locations(node) |
| 1616 | |
| 1617 | When you compile a node tree with :func:`compile`, the compiler expects |
| 1618 | :attr:`lineno` and :attr:`col_offset` attributes for every node that supports |
| 1619 | them. This is rather tedious to fill in for generated nodes, so this helper |
| 1620 | adds these attributes recursively where not already set, by setting them to |
| 1621 | the values of the parent node. It works recursively starting at *node*. |
| 1622 | |
| 1623 | |
| 1624 | .. function:: increment_lineno(node, n=1) |
| 1625 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1626 | Increment the line number and end line number of each node in the tree |
| 1627 | starting at *node* by *n*. This is useful to "move code" to a different |
| 1628 | location in a file. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1629 | |
| 1630 | |
| 1631 | .. function:: copy_location(new_node, old_node) |
| 1632 | |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 1633 | Copy source location (:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`, |
| 1634 | and :attr:`end_col_offset`) from *old_node* to *new_node* if possible, |
| 1635 | and return *new_node*. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1636 | |
| 1637 | |
| 1638 | .. function:: iter_fields(node) |
| 1639 | |
| 1640 | Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` |
| 1641 | that is present on *node*. |
| 1642 | |
| 1643 | |
| 1644 | .. function:: iter_child_nodes(node) |
| 1645 | |
| 1646 | Yield all direct child nodes of *node*, that is, all fields that are nodes |
| 1647 | and all items of fields that are lists of nodes. |
| 1648 | |
| 1649 | |
| 1650 | .. function:: walk(node) |
| 1651 | |
Georg Brandl | 619e7ba | 2011-01-09 07:38:51 +0000 | [diff] [blame] | 1652 | Recursively yield all descendant nodes in the tree starting at *node* |
| 1653 | (including *node* itself), in no specified order. This is useful if you only |
| 1654 | want to modify nodes in place and don't care about the context. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1655 | |
| 1656 | |
| 1657 | .. class:: NodeVisitor() |
| 1658 | |
| 1659 | A node visitor base class that walks the abstract syntax tree and calls a |
| 1660 | visitor function for every node found. This function may return a value |
Georg Brandl | 36ab1ef | 2009-01-03 21:17:04 +0000 | [diff] [blame] | 1661 | which is forwarded by the :meth:`visit` method. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1662 | |
| 1663 | This class is meant to be subclassed, with the subclass adding visitor |
| 1664 | methods. |
| 1665 | |
| 1666 | .. method:: visit(node) |
| 1667 | |
| 1668 | Visit a node. The default implementation calls the method called |
| 1669 | :samp:`self.visit_{classname}` where *classname* is the name of the node |
| 1670 | class, or :meth:`generic_visit` if that method doesn't exist. |
| 1671 | |
| 1672 | .. method:: generic_visit(node) |
| 1673 | |
| 1674 | This visitor calls :meth:`visit` on all children of the node. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1675 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1676 | Note that child nodes of nodes that have a custom visitor method won't be |
| 1677 | visited unless the visitor calls :meth:`generic_visit` or visits them |
| 1678 | itself. |
| 1679 | |
| 1680 | Don't use the :class:`NodeVisitor` if you want to apply changes to nodes |
| 1681 | during traversal. For this a special visitor exists |
| 1682 | (:class:`NodeTransformer`) that allows modifications. |
| 1683 | |
Serhiy Storchaka | c3ea41e | 2019-08-26 10:13:19 +0300 | [diff] [blame] | 1684 | .. deprecated:: 3.8 |
| 1685 | |
| 1686 | Methods :meth:`visit_Num`, :meth:`visit_Str`, :meth:`visit_Bytes`, |
| 1687 | :meth:`visit_NameConstant` and :meth:`visit_Ellipsis` are deprecated |
| 1688 | now and will not be called in future Python versions. Add the |
| 1689 | :meth:`visit_Constant` method to handle all constant nodes. |
| 1690 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1691 | |
| 1692 | .. class:: NodeTransformer() |
| 1693 | |
| 1694 | A :class:`NodeVisitor` subclass that walks the abstract syntax tree and |
| 1695 | allows modification of nodes. |
| 1696 | |
Georg Brandl | 36ab1ef | 2009-01-03 21:17:04 +0000 | [diff] [blame] | 1697 | The :class:`NodeTransformer` will walk the AST and use the return value of |
| 1698 | the visitor methods to replace or remove the old node. If the return value |
| 1699 | of the visitor method is ``None``, the node will be removed from its |
| 1700 | location, otherwise it is replaced with the return value. The return value |
| 1701 | may be the original node in which case no replacement takes place. |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1702 | |
| 1703 | Here is an example transformer that rewrites all occurrences of name lookups |
| 1704 | (``foo``) to ``data['foo']``:: |
| 1705 | |
| 1706 | class RewriteName(NodeTransformer): |
| 1707 | |
| 1708 | def visit_Name(self, node): |
Batuhan Taşkaya | 6680f4a | 2020-01-12 23:38:53 +0300 | [diff] [blame] | 1709 | return Subscript( |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1710 | value=Name(id='data', ctx=Load()), |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 1711 | slice=Index(value=Constant(value=node.id)), |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1712 | ctx=node.ctx |
| 1713 | ), node) |
| 1714 | |
| 1715 | Keep in mind that if the node you're operating on has child nodes you must |
| 1716 | either transform the child nodes yourself or call the :meth:`generic_visit` |
| 1717 | method for the node first. |
| 1718 | |
| 1719 | For nodes that were part of a collection of statements (that applies to all |
| 1720 | statement nodes), the visitor may also return a list of nodes rather than |
| 1721 | just a single node. |
| 1722 | |
Batuhan Taşkaya | 6680f4a | 2020-01-12 23:38:53 +0300 | [diff] [blame] | 1723 | If :class:`NodeTransformer` introduces new nodes (that weren't part of |
| 1724 | original tree) without giving them location information (such as |
| 1725 | :attr:`lineno`), :func:`fix_missing_locations` should be called with |
| 1726 | the new sub-tree to recalculate the location information:: |
| 1727 | |
| 1728 | tree = ast.parse('foo', mode='eval') |
| 1729 | new_tree = fix_missing_locations(RewriteName().visit(tree)) |
| 1730 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1731 | Usually you use the transformer like this:: |
| 1732 | |
| 1733 | node = YourTransformer().visit(node) |
| 1734 | |
| 1735 | |
Serhiy Storchaka | 850573b | 2019-09-09 19:33:13 +0300 | [diff] [blame] | 1736 | .. function:: dump(node, annotate_fields=True, include_attributes=False, *, indent=None) |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 1737 | |
| 1738 | Return a formatted dump of the tree in *node*. This is mainly useful for |
Serhiy Storchaka | e64f948 | 2019-08-29 09:30:23 +0300 | [diff] [blame] | 1739 | debugging purposes. If *annotate_fields* is true (by default), |
| 1740 | the returned string will show the names and the values for fields. |
| 1741 | If *annotate_fields* is false, the result string will be more compact by |
| 1742 | omitting unambiguous field names. Attributes such as line |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 1743 | numbers and column offsets are not dumped by default. If this is wanted, |
Serhiy Storchaka | e64f948 | 2019-08-29 09:30:23 +0300 | [diff] [blame] | 1744 | *include_attributes* can be set to true. |
Senthil Kumaran | f3695bf | 2016-01-06 21:26:53 -0800 | [diff] [blame] | 1745 | |
Serhiy Storchaka | 850573b | 2019-09-09 19:33:13 +0300 | [diff] [blame] | 1746 | If *indent* is a non-negative integer or string, then the tree will be |
| 1747 | pretty-printed with that indent level. An indent level |
| 1748 | of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) |
| 1749 | selects the single line representation. Using a positive integer indent |
| 1750 | indents that many spaces per level. If *indent* is a string (such as ``"\t"``), |
| 1751 | that string is used to indent each level. |
| 1752 | |
| 1753 | .. versionchanged:: 3.9 |
| 1754 | Added the *indent* option. |
| 1755 | |
| 1756 | |
Serhiy Storchaka | 832e864 | 2019-09-09 23:36:13 +0300 | [diff] [blame] | 1757 | .. _ast-cli: |
| 1758 | |
| 1759 | Command-Line Usage |
| 1760 | ------------------ |
| 1761 | |
| 1762 | .. versionadded:: 3.9 |
| 1763 | |
| 1764 | The :mod:`ast` module can be executed as a script from the command line. |
| 1765 | It is as simple as: |
| 1766 | |
| 1767 | .. code-block:: sh |
| 1768 | |
| 1769 | python -m ast [-m <mode>] [-a] [infile] |
| 1770 | |
| 1771 | The following options are accepted: |
| 1772 | |
| 1773 | .. program:: ast |
| 1774 | |
| 1775 | .. cmdoption:: -h, --help |
| 1776 | |
| 1777 | Show the help message and exit. |
| 1778 | |
| 1779 | .. cmdoption:: -m <mode> |
| 1780 | --mode <mode> |
| 1781 | |
| 1782 | Specify what kind of code must be compiled, like the *mode* argument |
| 1783 | in :func:`parse`. |
| 1784 | |
Batuhan Taşkaya | 814d687 | 2019-12-16 21:23:27 +0300 | [diff] [blame] | 1785 | .. cmdoption:: --no-type-comments |
| 1786 | |
| 1787 | Don't parse type comments. |
| 1788 | |
Serhiy Storchaka | 832e864 | 2019-09-09 23:36:13 +0300 | [diff] [blame] | 1789 | .. cmdoption:: -a, --include-attributes |
| 1790 | |
| 1791 | Include attributes such as line numbers and column offsets. |
| 1792 | |
Batuhan Taşkaya | 814d687 | 2019-12-16 21:23:27 +0300 | [diff] [blame] | 1793 | .. cmdoption:: -i <indent> |
| 1794 | --indent <indent> |
| 1795 | |
| 1796 | Indentation of nodes in AST (number of spaces). |
| 1797 | |
Serhiy Storchaka | 832e864 | 2019-09-09 23:36:13 +0300 | [diff] [blame] | 1798 | If :file:`infile` is specified its contents are parsed to AST and dumped |
| 1799 | to stdout. Otherwise, the content is read from stdin. |
| 1800 | |
| 1801 | |
Senthil Kumaran | f3695bf | 2016-01-06 21:26:53 -0800 | [diff] [blame] | 1802 | .. seealso:: |
| 1803 | |
Sanyam Khurana | 338cd83 | 2018-01-20 05:55:37 +0530 | [diff] [blame] | 1804 | `Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external documentation resource, has good |
Senthil Kumaran | f3695bf | 2016-01-06 21:26:53 -0800 | [diff] [blame] | 1805 | details on working with Python ASTs. |