Sub topic 10
Checking documentation against the code
This chapter assumes you can run pytest, read a Python docstring, and review a pull request. It also assumes you have already generated a docstring with an assistant. A generated docstring reads fluently whether or not it is true, so it needs checking before you merge it.
Why documentation drifts
An assistant writes documentation from the code you paste and from patterns in its training data. It can describe a parameter that does not exist, promise an exception the function never raises, or give an example output it guessed. Documentation can also be correct on the day you write it and wrong after the next refactor.
Checks fall into two groups:
- Claims a tool can run: examples, code snippets, command lines.
- Claims only a reader can judge: prose about behavior, argument meanings, error conditions.
Use tools for the first group so your attention goes to the second.
Turn examples into executable checks
Python's doctest module runs every >>> example in a docstring and compares the output. Here is the docstring from the previous chapter, with an error example added:
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Args:
price: Original price. Must be non-negative.
percent: Discount from 0 to 100 inclusive.
Returns:
The discounted price rounded to two decimal places.
Raises:
ValueError: If price is negative or percent is outside 0-100.
Examples:
>>> apply_discount(100, 10)
90.0
>>> apply_discount(80, 25)
60.0
>>> apply_discount(19.99, 15)
16.99
>>> apply_discount(-1, 10)
Traceback (most recent call last):
...
ValueError: price must be non-negative
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)Save it as pricing.py and run either command:
python -m doctest -v pricing.py
pytest --doctest-modules pricing.pyIf the assistant had guessed 17.0 for the third example, the run would report a failed example. The report shows the expected value, the actual value, and the line. The docstring is now checked against the real behavior.
The error example also checks the message. Doctest matches the exception type and message but ignores the traceback body. If the message in the code changes, the example fails and tells you the docs are stale.
What doctest cannot catch
Doctest only verifies the examples. It says nothing about the prose around them. Compare each prose claim against the code by hand:
| Docstring claim | How to check |
|---|---|
| Parameter names and types | Compare with the function signature. |
| "Raises ValueError if..." | Find each `raise` in the body. Every path should be documented, and nothing extra. |
| Boundary wording such as "0 to 100 inclusive" | Compare with the comparison operator. Here `0 <= percent <= 100` is inclusive. |
| "Rounded to two decimals" | Find the `round` call and its second argument. |
| Default values | Compare with the signature. |
The assistant gets boundaries wrong more often than names. "Greater than 0" and "at least 0" read almost the same but describe different behavior.
Check README snippets too
README code blocks drift the same way. Extract each snippet, run it in a clean environment, and compare the result with what the README says. Confirm that imports match real module paths and that every command line option exists. For a command-line tool, run --help and compare it with the options the README lists.
When a check fails, paste the failing output back into the conversation. The assistant can fix the docstring using real evidence. Decide whether the code or the docs are wrong before you accept its fix. A failed example sometimes exposes a genuine bug.
Keep the checks running
Add --doctest-modules to your pytest configuration or CI command. Examples are then re-run on every change, and a later refactor that breaks a documented example fails the build instead of misleading readers.
Exercise
A generated docstring says "Raises ValueError if percent is below 0 or above 100", and its doctest examples all pass. What two things should you still check by hand, and why can't doctest check them?
Show answer
Check that every raise in the function body is documented, including the negative-price path, and that the boundary wording matches the code's comparison operators (inclusive or exclusive). Doctest only runs the examples written in the docstring. It cannot see prose claims or undocumented code paths.