Sub topic 9
Drafting READMEs and usage guides
This chapter assumes you can run pytest and use Git. It also assumes you have read the chapter on generating docstrings, because the README draws on the same code and the same habits. A README is the first page a new user reads. An assistant can draft one quickly, but it writes plausible text even where it has no facts. Your job is to supply the facts and check the result.
Decide what the README must say
A useful README answers a small set of questions. Each answer has a source in your repository, and that source is what you give the assistant.
| Section | Where the facts come from | What goes wrong if the assistant guesses |
|---|---|---|
| Purpose | Your one-sentence description | Vague or inflated claims |
| Install | `pyproject.toml` or `requirements.txt` | Invented package names or commands |
| Quick start | Public function signatures and docstrings | Wrong argument order or names |
| Errors | `raise` statements and docstring `Raises` sections | Missing or made-up exceptions |
| Testing | Your test command and layout | Wrong runner or path |
Give the assistant the sources
Paste the real files, not a description of them. State what the README is for, and forbid invention.
Draft a README.md for the Python module below.
Context:
- Audience: developers who will import the module, not end users.
- Install command: pip install -e .
- Test command: pytest tests/
- Python 3.10 or later.
Requirements:
- Sections: Overview, Installation, Quick start, Error handling, Running tests.
- Quick start must contain one runnable example that uses only the public function below.
- Describe only behavior visible in the code or docstring.
- If something is unknown (license, version, contact), write "TODO" instead of guessing.
<code and docstring of apply_discount here>The "TODO" rule matters. Without it, the assistant fills gaps with confident invention, such as a license name or a --verbose flag that does not exist.
Make the examples executable
A README example is a claim about your code. You can check that claim by running it. Here is a Quick start example an assistant might draft for apply_discount, saved as a script:
from pricing import apply_discount
print(apply_discount(100, 10))
try:
apply_discount(100, 150)
except ValueError as error:
print(f"Rejected: {error}")Running it with pricing.py on the import path should print 90.0 and then Rejected: percent must be between 0 and 100. If the README shows different output, either the README or the code is wrong, and you need to find out which.
Review the prose, not just the code
Executable checks catch wrong commands. They do not catch wrong descriptions. Read the draft for these problems:
- Claims about behavior you cannot trace to a line of code, such as "thread-safe" or "fast".
- Missing edge cases. For example, the README should say that
percentof 0 and 100 are both valid. - Placeholder badges, links, or version numbers that point nowhere.
- Marketing wording that hides what the module does.
Keep the README short and maintainable
Ask for a README that is small enough to keep current. Link to the docstrings or generated API reference instead of copying every parameter description into the README. Duplicated detail drifts out of date when the code changes. When you change a function's signature, regenerate or edit the Quick start in the same commit.
Exercise
An assistant drafts a README Quick start that calls apply_discount(percent=10, price=100) and says it "returns 90". You run the example. What two things do you check, and what do you fix?
Show answer
Check that the keyword arguments match the real signature, which they do because both are named parameters, and check the actual output. The function returns the float 90.0, not the integer 90, so the README text should say 90.0. Fix the README to match the observed output, and keep the example in the file so a later change to the code is caught when someone reruns it.