Sub topic 3
Drafting READMEs and Usage Guides
Assume you know basic Python, pytest, Git, code review, and how to use an AI assistant. Treat generated documentation as a draft. The assistant can organize examples quickly, but it cannot guarantee that the examples match the current code.
Drafting a README section
Give the assistant the public function, its docstring, installation method, supported inputs, and the audience. Also specify the headings and commands your project uses. For example, ask it to write a “Usage” section for apply_discount, include valid and invalid examples, and avoid claiming features that are not present.
A useful usage example should run against the real interface:
def apply_discount(price: float, percent: float) -> float:
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)
print(apply_discount(100, 10))
# 90.0Ask for concise explanations of inputs, return values, errors, and setup steps. Request the project’s existing tone and Markdown conventions so the new section fits the rest of the README.
Checking documentation against the code
Compare every documented command and claim with the repository. Run examples, inspect function signatures, and check that error behavior and boundary values are accurate. Documentation that says “accepts any percentage” is wrong if the implementation rejects values outside 0–100.
Refining output and managing risks
Give targeted feedback: name the incorrect claim, provide the actual behavior, and state the required replacement. Do not paste secrets, private customer data, proprietary source, or unnecessary repository history into an assistant prompt. Remove sensitive values before sharing context.
Review for invented options, stale commands, misleading guarantees, and examples that expose unsafe practices. Documentation needs maintenance when APIs, dependencies, or workflows change.
Integrating review and CI
Keep documentation changes in the same pull request as the related code when possible. Ask a reviewer to check both behavior and clarity. Run unit tests, doctests, example scripts, and link or Markdown checks in continuous integration (CI), which is automated validation run for each change. Update the README when those checks fail.
Exercise
What is the strongest way to validate a generated usage example?
Show answer
Run the example against the current code in a test or doctest, then review its claims and setup instructions.