Comments and Docstrings
Codegen enables reading, modifying, and manipulating comments and docstrings while preserving proper formatting.
This guide describes proper usage of the following classes:
- Comment - Represents a single comment.
- CommentGroup - Represents a group of comments.
Accessing with Comments
Comments can be accessed through any symbol or directly from code blocks. Each comment is represented by a Comment
object that provides access to both the raw source and parsed text:
Editing Comments
Comments can be modified using the edit_text()
method, which handles formatting and delimiters automatically:
Comment Groups
Multiple consecutive comments are automatically grouped into a CommentGroup
, which can be edited as a single unit:
Working with Docstrings
Docstrings are special comments that document functions, classes, and modules. Codegen provides similar APIs for working with docstrings:
Adding Docstrings
You can add docstrings to any symbol that supports them:
Language-Specific Formatting
Codegen automatically handles language-specific docstring formatting:
Editing Docstrings
Like comments, docstrings can be modified while preserving formatting:
Comment Operations
Codegen provides utilities for working with comments at scale. For example, you can update or remove specific types of comments across your codebase:
When editing multi-line comments or docstrings, Codegen automatically handles indentation and maintains the existing comment style.
Special APIs and AI Integration
Google Style Docstrings
Codegen supports Google-style docstrings and can handle their specific formatting, using the CommentGroup.to_google_docstring(…) method.
Using AI for Documentation
Codegen integrates with LLMs to help generate and improve documentation. You can use the Codebase.ai(…) method to:
- Generate comprehensive docstrings
- Update existing documentation
- Convert between documentation styles
- Add parameter descriptions
Learn more about AI documentation capabilities in our Documentation Guide and LLM Integration Guide.
Documentation Coverage
You can analyze and improve documentation coverage across your codebase:
Check out the Documentation Guide for more advanced coverage analysis and bulk documentation generation.
Was this page helpful?