Skip to content

Commit af76190

Browse files
etserendclaude
andcommitted
fix(docs-gen): keep RST literal-block examples and notes as prose plus code (SAO-18112)
Address Astra review on #320: - Examples with an RST `::` lead-in are routed through _sanitize_description so only the indented code is fenced; the prose and `::` marker are no longer swallowed into a python block (e.g. get_metric_aggregate). - Notes use _sanitize_description so `::` blocks in notes are fenced and their braces are not escaped. - Add regression tests for both shapes. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 252ab66 commit af76190

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎scripts/create_docs.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,9 @@ def _strip_doctest_prompts(code: str) -> str:
579579
def _render_examples(parts: list[str], examples: list[dict[str, Any]]) -> None:
580580
"""Append an **Examples** section where every example is a fenced ```python block.
581581
582-
Examples that already contain a code fence are emitted unchanged. Everything else
583-
(bare code, doctest ``>>>`` code, or a mix) is fenced, with doctest prompts stripped.
582+
Examples that already contain a code fence are emitted unchanged. Examples with an RST
583+
``::`` lead-in go through ``_sanitize_description`` so only the indented code is fenced.
584+
Everything else (bare code, doctest ``>>>`` code, or a mix) is fenced, with doctest prompts stripped.
584585
Fenced content is left unescaped, so braces inside examples are safe for MDX.
585586
"""
586587
parts.append("**Examples**\n")
@@ -591,6 +592,11 @@ def _render_examples(parts: list[str], examples: list[dict[str, Any]]) -> None:
591592
if "```" in code:
592593
parts.append(code)
593594
continue
595+
if any(line.rstrip().endswith("::") for line in code.split("\n")):
596+
# RST literal block with a prose lead-in: fence only the indented code, keep the prose as prose.
597+
parts.append(_sanitize_description(code))
598+
parts.append("")
599+
continue
594600
parts.append("```python")
595601
parts.append(_strip_doctest_prompts(code).rstrip())
596602
parts.append("```\n")
@@ -669,7 +675,7 @@ def write_function(parts: list[str], fn: Any, heading_level: int = 3) -> None:
669675
if fdoc.get("notes"):
670676
parts.append("**Notes**\n")
671677
for note in fdoc.get("notes", []):
672-
ntext = _escape_curly_braces(note.get("note") or "")
678+
ntext = _sanitize_description(note.get("note") or "")
673679
if ntext:
674680
parts.append(f"{ntext}")
675681
parts.append("")
@@ -740,7 +746,7 @@ def write_class(parts: list[str], cls: Any) -> None:
740746
if cdoc.get("notes"):
741747
parts.append("**Notes**\n")
742748
for note in cdoc.get("notes", []):
743-
ntext = _escape_curly_braces(note.get("note") or "")
749+
ntext = _sanitize_description(note.get("note") or "")
744750
if ntext:
745751
parts.append(f"{ntext}")
746752
parts.append("")
@@ -814,7 +820,7 @@ def write_module(fd: FileDoc, parts: list[str]) -> None:
814820
if md.get("notes"):
815821
parts.append("**Notes**\n")
816822
for note in md.get("notes", []):
817-
ntext = _escape_curly_braces(note.get("note") or "")
823+
ntext = _sanitize_description(note.get("note") or "")
818824
if ntext:
819825
parts.append(f"{ntext}")
820826
parts.append("")

‎tests/test_create_docs_helpers.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,36 @@ def test_module_examples_are_rendered_fenced(self):
265265
out = "\n".join(parts)
266266
assert "**Examples**" in out
267267
assert "```python\nimport x\nx.run({'a': 1})\n```" in out
268+
269+
270+
class TestRstLiteralBlockExamples:
271+
"""RST ``::`` lead-in examples fence only the code, not the prose."""
272+
273+
RST = (
274+
"Poll until a specific metric is computed, then assert::\n\n"
275+
" while agg(x) is None:\n"
276+
" cfg = {'a': 1}\n"
277+
)
278+
279+
def test_prose_stays_outside_fence_and_marker_removed(self):
280+
"""The lead-in sentence is prose, the ``::`` marker is dropped, and only the code is fenced."""
281+
parts: list[str] = []
282+
_render_examples(parts, [{"code": self.RST}])
283+
out = "\n".join(parts)
284+
assert "Poll until a specific metric is computed, then assert\n```python" in out
285+
assert "::" not in out
286+
assert "while agg(x) is None:\n cfg = {'a': 1}\n```" in out
287+
assert out.count("```python") == 1
288+
289+
def test_note_with_rst_block_is_fenced_and_braces_kept(self):
290+
"""A ``::`` block inside a note is converted to a fence so its braces stay literal."""
291+
from types import SimpleNamespace
292+
293+
doc = {"description": "d", "notes": [{"note": "Use it like so::\n\n x = {'k': 1}\n and {prose}"}]}
294+
fd = SimpleNamespace(module=SimpleNamespace(doc=doc))
295+
parts: list[str] = []
296+
write_module(fd, parts)
297+
out = "\n".join(parts)
298+
assert "```python\n" in out
299+
assert "x = {'k': 1}\n```" in out
300+
assert r"\{prose\}" in out

0 commit comments

Comments
 (0)