|
| 1 | +# Runnable code blocks |
| 2 | + |
| 3 | +This document describes runnable Python markdown fences handled by `doc-builder`. |
| 4 | + |
| 5 | +## Authoring |
| 6 | + |
| 7 | +`doc-builder` recognizes fenced `py` and `python` blocks tagged with `runnable` or `runnable:<label>`: |
| 8 | + |
| 9 | +During conversion: |
| 10 | + |
| 11 | +- the runnable annotation is removed from the fence in rendered docs |
| 12 | +- the code content is preserved unless lines are hidden with `# doc-builder: hide` |
| 13 | +- `# doc-builder: hide` can hide a single line or a full indented block from rendered docs |
| 14 | +- `# doc-builder: ignore-bare-assert` keeps a bare `assert` in the block while suppressing the bare-assert warning for that line |
| 15 | +- the `# doc-builder: ignore-bare-assert` directive is removed from rendered docs |
| 16 | + |
| 17 | +The label is used in warning messages and generated test names. |
| 18 | + |
| 19 | +Example: |
| 20 | + |
| 21 | +````md |
| 22 | +```py runnable:quickstart |
| 23 | +import torch |
| 24 | +from transformers import AutoProcessor, GlmAsrForConditionalGeneration |
| 25 | + |
| 26 | +checkpoint_name = "zai-org/GLM-ASR-Nano-2512" |
| 27 | +audio_url = "https://huggingface.co/datasets/eustlb/audio-samples/resolve/main/bcn_weather.mp3" |
| 28 | + |
| 29 | +processor = AutoProcessor.from_pretrained(checkpoint_name) |
| 30 | +model = GlmAsrForConditionalGeneration.from_pretrained(checkpoint_name, device_map="auto", dtype="auto") |
| 31 | + |
| 32 | +conversation = [ |
| 33 | + [ |
| 34 | + { |
| 35 | + "role": "user", |
| 36 | + "content": [ |
| 37 | + {"type": "audio", "url": audio_url}, |
| 38 | + {"type": "text", "text": "Please transcribe this audio into text"}, |
| 39 | + ], |
| 40 | + } |
| 41 | + ] |
| 42 | +] |
| 43 | + |
| 44 | +inputs = processor.apply_chat_template( |
| 45 | + conversation, tokenize=True, add_generation_prompt=True, return_dict=True |
| 46 | +).to(model.device, dtype=model.dtype) |
| 47 | + |
| 48 | +inputs_transcription = processor.apply_transcription_request([audio_url]).to(model.device, dtype=model.dtype) |
| 49 | + |
| 50 | +for key in inputs: # doc-builder: hide |
| 51 | + assert torch.equal(inputs[key], inputs_transcription[key]) |
| 52 | + |
| 53 | +outputs = model.generate(**inputs, do_sample=False, max_new_tokens=128) |
| 54 | +decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1] :], skip_special_tokens=True) |
| 55 | + |
| 56 | +print(decoded_outputs) |
| 57 | +assert decoded_outputs == [ |
| 58 | + "Yesterday it was thirty five degrees in Barcelona, but today the temperature will go down to minus twenty degrees." |
| 59 | +] # doc-builder: ignore-bare-assert |
| 60 | +``` |
| 61 | +```` |
| 62 | + |
| 63 | +## Continuation blocks |
| 64 | + |
| 65 | +Use `:2`, `:3`, and so on to continue a runnable block in the same namespace: |
| 66 | + |
| 67 | +````md |
| 68 | +```py runnable:test_basic |
| 69 | +processor = AutoProcessor.from_pretrained("suno/bark") |
| 70 | +inputs = processor("Hello, my dog is cute", voice_preset=voice_preset) |
| 71 | +``` |
| 72 | + |
| 73 | +```py runnable:test_basic:2 |
| 74 | +inputs = processor("Amazing! I can speak English too.") |
| 75 | +``` |
| 76 | +```` |
| 77 | + |
| 78 | +`runnable:test_basic:2` and later continuations are grouped with `runnable:test_basic`, so later snippets can build on earlier setup. |
| 79 | + |
| 80 | +## Test decorators |
| 81 | + |
| 82 | +Runnable code blocks can declare test decorators with `# pytest-decorator:` comments. The decorator is imported and applied to the generated execution function at runtime. |
| 83 | + |
| 84 | +````md |
| 85 | +```py runnable:test_basic |
| 86 | +# pytest-decorator: transformers.testing_utils.slow |
| 87 | +# pytest-decorator: transformers.testing_utils.require_torch |
| 88 | +from transformers import pipeline |
| 89 | +pipe = pipeline("sentiment-analysis") |
| 90 | +print(pipe("I love this!")) |
| 91 | +``` |
| 92 | +```` |
| 93 | + |
| 94 | +Multiple decorators on the same line are also supported: |
| 95 | + |
| 96 | +````md |
| 97 | +```py runnable:test_basic |
| 98 | +# pytest-decorator: transformers.testing_utils.slow, transformers.testing_utils.require_torch |
| 99 | +from transformers import pipeline |
| 100 | +pipe = pipeline("sentiment-analysis") |
| 101 | +print(pipe("I love this!")) |
| 102 | +``` |
| 103 | +```` |
| 104 | + |
| 105 | +How it works: |
| 106 | + |
| 107 | +- each `# pytest-decorator: <dotted.import.path>` line is parsed during collection |
| 108 | +- the decorator is imported and applied to the code block execution function |
| 109 | +- skip-style decorators such as `@slow` or `@require_torch` raise `unittest.SkipTest`, which pytest reports as a skip |
| 110 | +- `# pytest-decorator:` lines are stripped from executed code and rendered documentation |
| 111 | + |
| 112 | +## Running runnable blocks in tests |
| 113 | + |
| 114 | +When `hf-doc-builder` is installed, pytest auto-loads the `doc-builder` plugin. This makes running runnable blocks against `.md` files a supported workflow, and it is the recommended way to execute them in most projects. |
| 115 | + |
| 116 | +You can point pytest directly at a markdown page: |
| 117 | + |
| 118 | +```bash |
| 119 | +pytest -q docs/source/en/my_page.md |
| 120 | +``` |
| 121 | + |
| 122 | +Or at a directory of markdown docs: |
| 123 | + |
| 124 | +```bash |
| 125 | +pytest -q docs/source/en/ |
| 126 | +``` |
| 127 | + |
| 128 | +What the pytest plugin does: |
| 129 | + |
| 130 | +- collects runnable blocks from the `.md` files passed to pytest |
| 131 | +- finds fenced `py`/`python` blocks marked with `runnable` or `runnable:<label>` |
| 132 | +- groups continuation blocks such as `runnable:test_basic:2` with the earlier block |
| 133 | +- creates one pytest item per runnable block |
| 134 | +- applies `# pytest-decorator:` directives before execution |
| 135 | +- reports failures with the markdown file path and runnable code snippet |
| 136 | + |
| 137 | +`DocIntegrationTest` is a lower-level helper. It is not required for markdown-based doc tests. Use it only if you want to manage doc execution from regular Python test files in your project, or if you need tighter control over the Python-side test wrapper. |
| 138 | + |
| 139 | +Example: |
| 140 | + |
| 141 | +```python |
| 142 | +from pathlib import Path |
| 143 | + |
| 144 | +from doc_builder.testing import DocIntegrationTest |
| 145 | + |
| 146 | + |
| 147 | +class MyPageDocIntegrationTest(DocIntegrationTest): |
| 148 | + doc_path = Path(__file__).resolve().parents[2] / "docs" / "source" / "en" / "my_page.md" |
| 149 | +``` |
| 150 | + |
| 151 | +`DocIntegrationTest` reads one markdown file, creates one Python test per runnable block (`runnable:my_case` becomes `test_my_case`), and executes those blocks from a standard pytest test module. |
| 152 | + |
| 153 | +Run locally with: |
| 154 | + |
| 155 | +```bash |
| 156 | +pytest -q tests/docs/test_my_page_docs.py |
| 157 | +``` |
| 158 | + |
| 159 | +Both approaches execute trusted documentation code with `exec`, so keep them limited to repo-controlled docs and CI. |
| 160 | + |
| 161 | +Both approaches run raw markdown code blocks. If you want test behavior to match rendered docs exactly, preprocess the blocks first so `# doc-builder: hide` lines are removed before execution. |
| 162 | + |
| 163 | +## Bare-assert warnings |
| 164 | + |
| 165 | +`doc-builder build` can emit warnings for bare `assert` statements inside runnable Python markdown fences. |
| 166 | + |
| 167 | +This behavior is opt-in: |
| 168 | + |
| 169 | +```bash |
| 170 | +doc-builder build {package_name} {path_to_docs} --build_dir {build_dir} --emit-warning |
| 171 | +``` |
| 172 | + |
| 173 | +When enabled: |
| 174 | + |
| 175 | +- bare `assert` lines in runnable blocks emit warnings with file and line information |
| 176 | +- `# doc-builder: hide` removes the line from rendered docs and does not warn |
| 177 | +- `# doc-builder: ignore-bare-assert` keeps the line, silences the warning, and is removed from rendered docs |
| 178 | + |
| 179 | +Warning formatting depends on where the build runs: |
| 180 | + |
| 181 | +- local runs: `Warning: docs/source/en/example.md:3: ...` |
| 182 | +- GitHub Actions: `::warning file=docs/source/en/example.md,line=3::...` |
| 183 | + |
| 184 | +## GitHub pipeline integration |
| 185 | + |
| 186 | +If you use the reusable PR documentation workflow, pass `--emit-warning` through `additional_args`: |
| 187 | + |
| 188 | +```yaml |
| 189 | +jobs: |
| 190 | + build: |
| 191 | + uses: huggingface/doc-builder/.github/workflows/build_pr_documentation.yml@main |
| 192 | + with: |
| 193 | + # ... |
| 194 | + additional_args: --emit-warning |
| 195 | +``` |
| 196 | +
|
| 197 | +To fail the PR documentation build on warnings, also set `fail_on_warning: true`: |
| 198 | + |
| 199 | +```yaml |
| 200 | +jobs: |
| 201 | + build: |
| 202 | + uses: huggingface/doc-builder/.github/workflows/build_pr_documentation.yml@main |
| 203 | + with: |
| 204 | + # ... |
| 205 | + additional_args: --emit-warning |
| 206 | + fail_on_warning: true |
| 207 | +``` |
0 commit comments