In order to determine the extent of your documentation coverage, you can iterate through all symbols of interest and count the number of docstrings:To see your current documentation coverage, you can iterate through all symbols of interest and count the number of docstrings:
# Track directory statsdir_stats = {}# Analyze each directoryfor directory in codebase.directories: # Skip test, sql and alembic directories if any(x in directory.path.lower() for x in ['test', 'sql', 'alembic']): continue # Get undecorated functions funcs = [f for f in directory.functions if not f.is_decorated] total = len(funcs) # Only analyze dirs with >10 functions if total > 10: documented = sum(1 for f in funcs if f.docstring) coverage = (documented / total * 100) dir_stats[directory.path] = { 'total': total, 'documented': documented, 'coverage': coverage }# Find lowest coverage directoryif dir_stats: lowest_dir = min(dir_stats.items(), key=lambda x: x[1]['coverage']) path, stats = lowest_dir print(f"📉 Lowest coverage directory: '{path}'") print(f" • Total functions: {stats['total']}") print(f" • Documented: {stats['documented']}") print(f" • Coverage: {stats['coverage']:.1f}%") # Print all directory stats for comparison print("\n📊 All directory coverage rates:") for path, stats in sorted(dir_stats.items(), key=lambda x: x[1]['coverage']): print(f" '{path}': {stats['coverage']:.1f}% ({stats['documented']}/{stats['total']} functions)")
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.
# Import datetime for timestampfrom datetime import datetime# Get current timestamptimestamp = datetime.now().strftime("%B %d, %Y")print("📚 Generating and Updating Function Documentation")# Process all functions in the codebasefor function in codebase.functions: current_docstring = function.docstring() if current_docstring: # Update existing docstring to be more descriptive new_docstring = codebase.ai( f"Update the docstring for {function.name} to be more descriptive and comprehensive.", target=function ) new_docstring += f"\n\nUpdated on: {timestamp}" else: # Generate new docstring for function new_docstring = codebase.ai( f"Generate a comprehensive docstring for {function.name} including parameters, return type, and description.", target=function ) new_docstring += f"\n\nCreated on: {timestamp}" # Set the new or updated docstring function.set_docstring(new_docstring)
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:
python
Copy
Ask AI
# Iterate through all functions in the codebasefor function in codebase.functions: # Skip if function already has a docstring if function.docstring: continue # Build parameter documentation param_docs = [] for param in function.parameters: param_type = param.type.source if param.is_typed else "Any" param_docs.append(f" {param.name} ({param_type}): Description of {param.name}") # Get return type if present return_type = function.return_type.source if function.return_type else "None" # Create Google-style docstring docstring = f'''""" Description of {function.name}. Args:{chr(10).join(param_docs)} Returns: {return_type}: Description of return value """''' # Set the new docstring function.set_docstring(docstring)