def patch_file()

in scripts/convert-types.py [0:0]


def patch_file(file_path: str, dry_run: bool = False, quiet: bool = False) -> None:
    with open(file_path) as f:
        before = f.read()
    try:
        lines = [line.rstrip() for line in before.splitlines()]
        if types := find_typing_imports(lines):
            lines = insert_import_annotations(lines)
            lines = [patched for line in lines for patched in patch_imports(line)]
            lines = sort_imports(lines)
            after = patch_type_hints("\n".join(lines), types) + "\n"
            if before == after:
                return

            if not dry_run:
                with open(file_path, "w") as f:
                    f.write(after)
                print(file_path)
            elif not quiet:
                print(f"| {file_path}")
                print(f"+--{'-' * len(file_path)}")
                diffs = difflib.context_diff(
                    before.splitlines(keepends=True),
                    after.splitlines(keepends=True),
                    fromfile="Before changes",
                    tofile="After changes",
                    n=1,
                )
                sys.stdout.writelines(diffs)
                print(f"+{'=' * 100}")
                print("| Press [ENTER] to continue to the next file")
                input()
    except Exception:
        logging.exception(f"Could not process file: {file_path}")