Increment the line number and end line number of each node in the tree starting at *node* by *n*. This is useful to "move code" to a different location in a file.
(node, n=1)
| 230 | |
| 231 | |
| 232 | def increment_lineno(node, n=1): |
| 233 | """ |
| 234 | Increment the line number and end line number of each node in the tree |
| 235 | starting at *node* by *n*. This is useful to "move code" to a different |
| 236 | location in a file. |
| 237 | """ |
| 238 | for child in walk(node): |
| 239 | # TypeIgnore is a special case where lineno is not an attribute |
| 240 | # but rather a field of the node itself. |
| 241 | if isinstance(child, TypeIgnore): |
| 242 | child.lineno = getattr(child, 'lineno', 0) + n |
| 243 | continue |
| 244 | |
| 245 | if 'lineno' in child._attributes: |
| 246 | child.lineno = getattr(child, 'lineno', 0) + n |
| 247 | if ( |
| 248 | "end_lineno" in child._attributes |
| 249 | and (end_lineno := getattr(child, "end_lineno", 0)) is not None |
| 250 | ): |
| 251 | child.end_lineno = end_lineno + n |
| 252 | return node |
| 253 | |
| 254 | |
| 255 | def iter_fields(node): |