MCPcopy Index your code
hub / github.com/PyGithub/PyGithub / add_future_import

Method add_future_import

scripts/openapi.py:537–564  ·  view source on GitHub ↗
(node: cst.Module)

Source from the content-addressed store, hash-verified

535
536 @staticmethod
537 def add_future_import(node: cst.Module) -> cst.Module:
538 stmts = list(node.body)
539 first_stmt = stmts[0] if stmts else None
540 if not (
541 first_stmt
542 and isinstance(first_stmt, cst.SimpleStatementLine)
543 and isinstance(first_stmt.body[0], cst.ImportFrom)
544 and isinstance(first_stmt.body[0].module, cst.Name)
545 and first_stmt.body[0].module.value == "__future__"
546 and first_stmt.body[0].names
547 and isinstance(first_stmt.body[0].names[0], cst.ImportAlias)
548 and isinstance(first_stmt.body[0].names[0].name, cst.Name)
549 and first_stmt.body[0].names[0].name.value == "annotations"
550 ):
551 import_stmt = cst.SimpleStatementLine(
552 [cst.ImportFrom(cst.Name("__future__"), [cst.ImportAlias(cst.Name("annotations"))])]
553 )
554 if (
555 isinstance(first_stmt, cst.SimpleStatementLine)
556 and isinstance(first_stmt.body[0], (cst.Import, cst.ImportFrom))
557 and not first_stmt.leading_lines
558 ):
559 first_stmt = first_stmt.with_changes(leading_lines=[cst.EmptyLine()])
560 stmts = [first_stmt] + stmts[1:]
561
562 node = node.with_changes(body=[import_stmt] + stmts)
563
564 return node
565
566
567class IndexPythonClassesVisitor(CstVisitorBase):

Callers 2

leave_ModuleMethod · 0.80
leave_ModuleMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected