You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add mktestdocs to catch docs/code drift (closes#1083)
Adds automated testing of Python code examples in the documentation
to prevent examples from drifting out of sync with the library.
Changes:
- Add `mktestdocs` and `pytest` to the `doc` dependency group in
`pyproject.toml` so they are available alongside the other doc-build
tools without pulling in the full `dev` group.
- Add `scripts/check-docs-drift.py`: a pytest-based script that uses
`mktestdocs.grab_code_blocks()` to collect every ```python fenced
block under `docs/`, skips any block whose first line is `# skip`,
and executes the rest via `exec_python()`. A new taskipy task
`docs-check-drift` runs it with `pytest scripts/check-docs-drift.py -v`.
- Fix all ```python code blocks across `docs/` so they are correctly
picked up by mktestdocs:
- Remove the stray space in ``` python fences (changed to ```python)
so that mktestdocs can identify them (it matches on the exact string
"python" immediately after the backticks).
- Add `save_to_file=False, log_level="error"` to `EmissionsTracker`
and `OfflineEmissionsTracker` instantiations to avoid creating CSV
files or noisy output during CI runs.
- Add `# skip` as the first line of blocks that cannot run in CI
because they depend on external services or optional heavy
dependencies (TensorFlow, Prometheus, Logfire, Google Cloud,
Comet ML, live CodeCarbon API).
- Correct a `pip install` command that was incorrectly fenced as
```python in `comet.md`; changed to ```console.
- Update `.github/workflows/build-docs.yml` to run `docs-check-drift`
as a step before the docs build, triggered on changes to `docs/**`,
`mkdocs.yml`, or `scripts/check-docs-drift.py`.
- Document the drift check and the `# skip` convention in
`CONTRIBUTING.md` under the "Build Documentation" section.
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -284,6 +284,28 @@ uv run --only-group doc task docs
284
284
285
285
to regenerate the html files. For local preview with live reload, run `uv run --only-group doc task docs-serve`.
286
286
287
+
#### Testing documentation code examples
288
+
289
+
Python code blocks in the docs can be checked to catch examples that have drifted out of sync with the library. Run the check with:
290
+
291
+
```sh
292
+
uv run task docs-check-drift
293
+
```
294
+
295
+
This executes every ` ```python ` fenced block found under `docs/` using [mktestdocs](https://github.com/koaning/mktestdocs). The check is driven by `scripts/check-docs-drift.py`.
296
+
297
+
**Adding a new code block to the docs?** Use a ` ```python ` fence so it is picked up. If the block cannot run in CI (e.g. it requires TensorFlow, a live API, or other external dependencies), add `# skip` as the first line inside the block:
298
+
299
+
````markdown
300
+
```python
301
+
# skip
302
+
import tensorflow as tf
303
+
...
304
+
```
305
+
````
306
+
307
+
Blocks marked `# skip` are excluded from the check but still rendered normally in the documentation.
308
+
287
309
### Rebase your branch on master
288
310
289
311
Before creating a PR, please make sure to rebase your branch on master to avoid merge conflicts and make the review easier. You can do it with the following command:
0 commit comments