- 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):- 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.
- 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):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:- Create
README.md. - Prompt: “Generate a README for a FastAPI-based fake data generator repository.”
- Edit the generated content to match your repo (e.g., exact dependencies, environment variables, and example commands).
5. Console and runtime notes
When running a FastAPI app with uvicorn you typically see logs like:- 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.
- Inspect the Pydantic model (e.g.,
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.