replace_multiline_code_block() — fastapi Function Reference
Architecture documentation for the replace_multiline_code_block() function in doc_parsing_utils.py from the fastapi codebase.
Entity Profile
Dependency Diagram
graph TD 49a5cb30_408f_f3be_8707_68a8c1644c3d["replace_multiline_code_block()"] c463d6a9_085f_f272_b9fc_455b7e9b6a57["doc_parsing_utils.py"] 49a5cb30_408f_f3be_8707_68a8c1644c3d -->|defined in| c463d6a9_085f_f272_b9fc_455b7e9b6a57 4defc455_7357_b387_391c_a1048a88a459["replace_multiline_code_blocks_in_text()"] 4defc455_7357_b387_391c_a1048a88a459 -->|calls| 49a5cb30_408f_f3be_8707_68a8c1644c3d 37002dc8_e4b0_59f2_57f8_e9102dcb3c6b["_split_hash_comment()"] 49a5cb30_408f_f3be_8707_68a8c1644c3d -->|calls| 37002dc8_e4b0_59f2_57f8_e9102dcb3c6b 690d336c_b2dd_dd3c_12b2_25cb48e58f54["_split_slashes_comment()"] 49a5cb30_408f_f3be_8707_68a8c1644c3d -->|calls| 690d336c_b2dd_dd3c_12b2_25cb48e58f54 style 49a5cb30_408f_f3be_8707_68a8c1644c3d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
scripts/doc_parsing_utils.py lines 570–640
def replace_multiline_code_block(
block_a: MultilineCodeBlockInfo, block_b: MultilineCodeBlockInfo
) -> list[str]:
"""
Replace multiline code block `a` with block `b` leaving comments intact.
Syntax of comments depends on the language of the code block.
Raises ValueError if the blocks are not compatible (different languages or different number of lines).
"""
start_line = block_a["start_line_no"]
end_line_no = start_line + len(block_a["content"]) - 1
if block_a["lang"] != block_b["lang"]:
raise ValueError(
f"Code block (lines {start_line}-{end_line_no}) "
"has different language than the original block "
f"('{block_a['lang']}' vs '{block_b['lang']}')"
)
if len(block_a["content"]) != len(block_b["content"]):
raise ValueError(
f"Code block (lines {start_line}-{end_line_no}) "
"has different number of lines than the original block "
f"({len(block_a['content'])} vs {len(block_b['content'])})"
)
block_language = block_a["lang"].lower()
if block_language in {"mermaid"}:
if block_a != block_b:
print(
f"Skipping mermaid code block replacement (lines {start_line}-{end_line_no}). "
"This should be checked manually."
)
return block_a["content"].copy() # We don't handle mermaid code blocks for now
code_block: list[str] = []
for line_a, line_b in zip(block_a["content"], block_b["content"]):
line_a_comment: Union[str, None] = None
line_b_comment: Union[str, None] = None
# Handle comments based on language
if block_language in {
"python",
"py",
"sh",
"bash",
"dockerfile",
"requirements",
"gitignore",
"toml",
"yaml",
"yml",
"hash-style-comments",
}:
_line_a_code, line_a_comment = _split_hash_comment(line_a)
_line_b_code, line_b_comment = _split_hash_comment(line_b)
res_line = line_b
if line_b_comment:
res_line = res_line.replace(line_b_comment, line_a_comment, 1)
code_block.append(res_line)
elif block_language in {"console", "json", "slash-style-comments"}:
_line_a_code, line_a_comment = _split_slashes_comment(line_a)
_line_b_code, line_b_comment = _split_slashes_comment(line_b)
res_line = line_b
if line_b_comment:
res_line = res_line.replace(line_b_comment, line_a_comment, 1)
code_block.append(res_line)
else:
code_block.append(line_b)
return code_block
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does replace_multiline_code_block() do?
replace_multiline_code_block() is a function in the fastapi codebase, defined in scripts/doc_parsing_utils.py.
Where is replace_multiline_code_block() defined?
replace_multiline_code_block() is defined in scripts/doc_parsing_utils.py at line 570.
What does replace_multiline_code_block() call?
replace_multiline_code_block() calls 2 function(s): _split_hash_comment, _split_slashes_comment.
What calls replace_multiline_code_block()?
replace_multiline_code_block() is called by 1 function(s): replace_multiline_code_blocks_in_text.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free