Skip to main content
In this lesson we cover practical, code-first techniques for writing clear in-code documentation. We’ll demonstrate inline comments, simple docstrings, common styles (PEP 257, Google, NumPy), and how to accelerate the process with tools like GitHub Copilot. The aim is to help you generate useful, accurate documentation quickly — while emphasizing the essential verification and editing steps. Key topics:
  • Inline comments and compact docstrings for endpoints
  • Using AI assistants (e.g., GitHub Copilot) to generate or improve comments
  • Before/after examples to illustrate improvements
  • Generating README.md skeletons with AI
  • Console/runtime troubleshooting and practical tips

1. Inline comments and simple docstrings

For small endpoints, a concise docstring plus a couple of inline comments is often sufficient. Keep the docstring focused on purpose, inputs, outputs, and example request/response shapes. Example (FastAPI):
Best practices:
  • Keep the docstring short and focused on the function’s role, inputs, and output format.
  • Use inline comments sparingly for non-obvious logic, edge cases, or performance implications.
  • Use typing annotations to make the signature self-documenting.
AI-generated comments are a great starting point, but always review them for accuracy and to remove stale or incorrect details.

2. Asking GitHub Copilot (or other assistants) to comment code

If you prefer automated assistance, you can ask Copilot to annotate code. Typical workflow:
  • Open the file you want documented in your editor.
  • Prompt the assistant, for example: “Please comment this code to explain it clearly.”
  • Review the generated docstrings and inline comments; edit them to ensure correctness.
Example prompt:
Checklist after generation:
  • Verify that parameter names match the current model and code (AI may refer to removed fields).
  • Confirm examples reflect actual requests/responses.
  • Remove any speculative or hallucinated details (e.g., fields that no longer exist).
AI assistants can hallucinate parameters or behavior. Always validate generated comments against the current code and tests before committing.

3. Example — before and after

Before (minimal comments):
After (annotated and documented):
This before/after demonstrates how small docstrings and targeted comments improve clarity for maintainers and API consumers.

4. Generate or improve README.md with GitHub Copilot

Copilot and similar assistants can quickly scaffold a README. Use a clear prompt and then verify every section (dependencies, setup steps, env vars, example calls). Typical steps:
  1. Create README.md.
  2. Prompt: “Generate a README for a FastAPI-based fake data generator repository.”
  3. Edit the generated content to match your repo (e.g., exact dependencies, environment variables, and example commands).
Example README snippet (cleaned and reviewed):
Always verify and adapt the generated README content — don’t publish without confirming accuracy.

5. Console and runtime notes

When running a FastAPI app with uvicorn you typically see logs like:
Common runtime issues:
  • 422 Unprocessable Entity: request JSON does not match the expected Pydantic model.
    • Inspect the Pydantic model (e.g., FakeDataRequest) for required fields.
    • Confirm request keys and types match the model.
    • Update docstrings and README examples to reflect the actual schema.

6. Practical tips

  • Use consistent import styles (absolute or relative) across the codebase to avoid confusion in docs and examples.
  • Document public modules, exported functions, and class methods — not just endpoints.
  • Prefer short, testable docstrings that are easy to keep up-to-date.
  • When using AI to generate docs or READMEs:
    • Provide a precise prompt and example inputs/outputs.
    • Review outputs for factual accuracy and completeness.
  • Include exact setup steps, environment variables, and curl/postman examples in README for better onboarding.
Table — Common docstring styles at-a-glance:

Conclusion

Automated tools like GitHub Copilot can speed up writing in-code documentation and README scaffolding. They provide useful stubs and suggestions, but the final verification and refinement step is essential to ensure accuracy. With short, consistent docstrings, selective inline comments, and validated AI outputs, you can maintain a codebase that’s easier for current and future maintainers to understand. Thank you for reading this lesson.

Watch Video