Codegen enables reading, modifying, and manipulating comments and docstrings while preserving proper formatting.This guide describes proper usage of the following classes:
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:
Copy
Ask AI
# Find all comments in a filefile = codebase.get_file("my_file.py")for comment in file.code_block.comments: print(comment.text)# Access comments associated with a symbolsymbol = file.get_symbol("my_function")if symbol.comment: print(symbol.comment.text) # Comment text without delimiters print(symbol.comment.source) # Full comment including delimiters# Access inline commentsif symbol.inline_comment: print(symbol.inline_comment.text)# Accessing all comments in a functionfor comment in symbol.code_block.comments: print(comment.text)
Multiple consecutive comments are automatically grouped into a CommentGroup, which can be edited as a single unit:
Copy
Ask AI
# Original comments:# First line# Second line# Third linecomment_group = symbol.commentprint(comment_group.text) # "First line\nSecond line\nThird line"# Edit the entire group at oncecomment_group.edit_text("New first line\nNew second line")
Docstrings are special comments that document functions, classes, and modules. Codegen provides similar APIs for working with docstrings:
Copy
Ask AI
function = file.get_symbol("my_function")if function.docstring: print(function.docstring.text) # Docstring content print(function.docstring.source) # Full docstring with delimiters
You can add docstrings to any symbol that supports them:
Copy
Ask AI
# Add a single-line docstringfunction.set_docstring("A brief description")# Add a multi-line docstringfunction.set_docstring(""" A longer description that spans multiple lines. Args: param1: Description of first parameter""")
Like comments, docstrings can be modified while preserving formatting:
Copy
Ask AI
# Edit a docstringfunction.docstring.edit_text("Updated documentation")# Edit a multi-line docstringfunction.docstring.edit_text(""" Updated multi-line documentation that preserves indentation and formatting.""")
Codegen provides utilities for working with comments at scale. For example, you can update or remove specific types of comments across your codebase:
Copy
Ask AI
# Example: Remove eslint disable comments for a specific rulefor file in codebase.files: for comment in file.code_block.comments: if "eslint-disable" in comment.source: # Check if comment disables specific rule if "@typescript-eslint/no-explicit-any" in comment.text: comment.remove()
When editing multi-line comments or docstrings, Codegen automatically handles
indentation and maintains the existing comment style.
# Edit while preserving Google stylesymbol_a = file.get_symbol("SymbolA")func_b = symbol_a.get_method("funcB")func_b.docstring.to_google_docstring(func_b)
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
Copy
Ask AI
# Generate a docstring using AIfunction = codebase.get_function("my_function")new_docstring = codebase.ai( "Generate a comprehensive docstring in Google style", target=function context={ # provide additional context to the LLM 'usages': function.usages, 'dependencies': function.dependencies })function.set_docstring(new_docstring)
You can analyze and improve documentation coverage across your codebase:
Copy
Ask AI
# Count documented vs undocumented functionstotal = 0documented = 0for function in codebase.functions: total += 1 if function.docstring: documented += 1coverage = (documented / total * 100) if total > 0 else 0print(f"Documentation coverage: {coverage:.1f}%")
Check out the Documentation Guide for
more advanced coverage analysis and bulk documentation generation.