- Inline documentation using docstrings (with a cleaned Flask example).
- Using an AI assistant to generate or enhance documentation in your editor.
- Creating documentation rules that guide the assistant so generated content is consistent.
Inline documentation (docstrings)
A reliable place to put documentation is directly in the source code using docstrings. Keep docstrings concise, follow PEP 8 style, and include purpose, parameters, return values, and short examples where appropriate. Below is a minimal Flask application fragment illustrating clear docstrings for a CSV reader and simple database helpers. Use this pattern to make functions self-describing and easier for both humans and tools to consume.- Keep them short and factual.
- Document each parameter and the return value.
- Include small usage or output examples for clarity.
- Prefer reStructuredText or Google-style docstrings depending on your project conventions.
Generating or enhancing docs with an AI assistant
An AI assistant integrated into your editor can:- Add or rewrite docstrings to follow your style guidelines.
- Insert usage examples or expected outputs.
- Reformat comments to match standards (e.g., PEP 8).
- Add high-level headers for modules and tests.
When working with the assistant:
- Provide the target file(s) and preferred doc style.
- Specify which rules should be applied (e.g., PEP 8, required sections).
- Iterate: ask for refinements or stricter enforcement if the first result is not acceptable.
Using model selection and agent mode
Many editor integrations let you choose both the AI model and the mode of operation (for example, an “ask” mode for single requests or an “agent” mode for multi-step tasks). Different models and modes can produce varying levels of detail or adherence to style rules. Below is an example screenshot from an editor where the user hovers over the model selection dropdown before requesting documentation for a file. This illustrates the typical UI pattern: file view on the left, chat assistant on the right, and a model dropdown you can change before making the request.
- Add module headers.
- Generate or rewrite docstrings for functions and tests.
- Enumerate fixtures and explain expected behavior.
- Provide examples that follow your rules file.
Creating documentation rules to guide the AI
To produce consistent outputs, create a repository-level rules file the assistant can read and apply. Rules can enforce language style, required docstring sections, formatting, and whether examples are mandatory. Below is an example Python snippet that creates a documentation rule file. The assistant can use this file to enforce concise language, PEP 8 compliance, and inclusion of examples.
Once rule files are added to the workspace, most assistants will allow you to auto-attach them or explicitly apply them when generating docs. Scope rules to specific file patterns and decide whether they apply automatically or on-demand.
Best practice: Keep rules concise and focused on measurable formatting and content requirements (for example, required sections, parameter/return descriptions, and example usage). Clear, testable rules produce more consistent generated documentation.
Best practices for AI-assisted documentation
- Keep your requests to the assistant specific: target a file, state the docstring style, and describe required sections.
- Include examples in rule files so the assistant knows the expected snippet format.
- Group related information within docstrings: purpose, args, returns, and a short example.
- Review and iterate: ask the assistant to refine outputs until they match your standards.
- Version your documentation rules and store them in the repository so teams share the same conventions.
- PEP 8 — Style Guide for Python Code
- Flask — A lightweight WSGI web application framework
- Kubernetes Documentation (example external reference)