MCPcopy Create free account
hub / github.com/fastapi/fastapi / replace_multiline_code_blocks_in_text

Function replace_multiline_code_blocks_in_text

scripts/doc_parsing_utils.py:643–669  ·  view source on GitHub ↗

Update each code block in `text` with the corresponding code block from `original_code_blocks` with comments taken from `code_blocks`. Raises ValueError if the number, language, or shape of code blocks do not match.

(
    text: list[str],
    code_blocks: list[MultilineCodeBlockInfo],
    original_code_blocks: list[MultilineCodeBlockInfo],
)

Source from the content-addressed store, hash-verified

641
642
643def replace_multiline_code_blocks_in_text(
644 text: list[str],
645 code_blocks: list[MultilineCodeBlockInfo],
646 original_code_blocks: list[MultilineCodeBlockInfo],
647) -> list[str]:
648 """
649 Update each code block in `text` with the corresponding code block from
650 `original_code_blocks` with comments taken from `code_blocks`.
651
652 Raises ValueError if the number, language, or shape of code blocks do not match.
653 """
654
655 if len(code_blocks) != len(original_code_blocks):
656 raise ValueError(
657 "Number of code blocks does not match the number in the original document "
658 f"({len(code_blocks)} vs {len(original_code_blocks)})"
659 )
660
661 modified_text = text.copy()
662 for block, original_block in zip(code_blocks, original_code_blocks, strict=True):
663 updated_content = replace_multiline_code_block(block, original_block)
664
665 start_line_index = block["start_line_no"] - 1
666 for i, updated_line in enumerate(updated_content):
667 modified_text[start_line_index + i] = updated_line
668
669 return modified_text
670
671
672# All checks

Callers 1

check_translationFunction · 0.85

Calls 1

Tested by

no test coverage detected