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)
| 257 | |
| 258 | |
| 259 | def increment_lineno(node, n=1): |
| 260 | """ |
| 261 | Increment the line number and end line number of each node in the tree |
| 262 | starting at *node* by *n*. This is useful to "move code" to a different |
| 263 | location in a file. |
| 264 | """ |
| 265 | for child in walk(node): |
| 266 | # TypeIgnore is a special case where lineno is not an attribute |
| 267 | # but rather a field of the node itself. |
| 268 | if isinstance(child, TypeIgnore): |
| 269 | child.lineno = getattr(child, 'lineno', 0) + n |
| 270 | continue |
| 271 | |
| 272 | if 'lineno' in child._attributes: |
| 273 | child.lineno = getattr(child, 'lineno', 0) + n |
| 274 | if ( |
| 275 | "end_lineno" in child._attributes |
| 276 | and (end_lineno := getattr(child, "end_lineno", 0)) is not None |
| 277 | ): |
| 278 | child.end_lineno = end_lineno + n |
| 279 | return node |
| 280 | |
| 281 | |
| 282 | def iter_fields(node): |
nothing calls this directly
no test coverage detected
searching dependent graphs…