Find the line number after the last import statement.
(tree: ast.Module)
| 329 | |
| 330 | |
| 331 | def _find_import_insert_line(tree: ast.Module) -> int: |
| 332 | """Find the line number after the last import statement.""" |
| 333 | last_import_line = None |
| 334 | for node in tree.body: |
| 335 | if isinstance(node, (ast.Import, ast.ImportFrom)): |
| 336 | last_import_line = node.end_lineno or node.lineno |
| 337 | if last_import_line is not None: |
| 338 | return last_import_line |
| 339 | # No imports found - insert after module docstring if present, else at top |
| 340 | if ( |
| 341 | tree.body |
| 342 | and isinstance(tree.body[0], ast.Expr) |
| 343 | and isinstance(tree.body[0].value, ast.Constant) |
| 344 | and isinstance(tree.body[0].value.value, str) |
| 345 | ): |
| 346 | return tree.body[0].end_lineno or tree.body[0].lineno |
| 347 | return 0 |
| 348 | |
| 349 | |
| 350 | def apply_patches(contents: str, patches: Patches) -> str: |