Codegen’s GraphSitter library provides powerful APIs for editing docstrings and adding documentation to your codebase. This guide will walk you through the process of modifying existing docstrings and adding new documentation to functions, classes, and modules.

Common use cases include:

  • Updating outdated docstrings
  • Adding missing parameter and return type descriptions
  • Standardizing documentation format across the codebase
  • Adding examples to function docstrings

set_docstring

Editing docstrings and adding documentation involves accessing and modifying the docstring property of various code elements. Here’s a basic example:

By default, set_docstring tries to intelligently format the docstring to match the existing format. This can be turned on and off using auto_format and clean_format flags.

Determine Documentation Coverage

To see your current documentation coverage, you can iterate through all symbols of interest and count the number of docstrings:

Leveraging AI for Generating Documentation

For non-trivial codebases, it can be challenging to achieve full documentation coverage.

The most efficient way to edit informative docstrings is to use codebase.ai to generate docstrings, then use the set_docstring method to update the docstring.

More info on using AI with Codebase can be found here.

Adding Explicit Parameter Names and Types

Alternatively, you can also rely on deterministic string formatting to edit docstrings.

To add “Google-style” parameter names and types to a function docstring, you can use the following code snippet: