MCPcopy Index your code
hub / github.com/RustPython/RustPython / _find_import_insert_line

Function _find_import_insert_line

scripts/update_lib/patch_spec.py:331–347  ·  view source on GitHub ↗

Find the line number after the last import statement.

(tree: ast.Module)

Source from the content-addressed store, hash-verified

329
330
331def _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
350def apply_patches(contents: str, patches: Patches) -> str:

Callers 4

test_with_importsMethod · 0.90
apply_patchesFunction · 0.85

Calls 1

isinstanceFunction · 0.85

Tested by 3

test_with_importsMethod · 0.72