Using AI to Document Undocumented Legacy Code
Generate accurate, PEP 257 compliant docstrings and type hints for legacy Python modules using targeted AI prompts.
You can quickly document legacy modules without manual rewriting by feeding unannotated functions to an AI assistant with explicit instructions. Request PEP 257 compliant docstrings and standard type hints while prohibiting functional code changes. This method improves overall code readability and reduces technical debt across older codebases instantly.
Structure prompts for legacy Python documentation
High-level architecture diagrams and user-facing documentation require separate manual effort, but internal module documentation can be automated effectively. Construct prompts that isolate individual functions or classes. Tell the AI assistant to parse the control flow, infer types from operations, and return updated code decorated with standard docstrings Python standard tutorial.
Instruct the assistant to follow the standard library conventions. Specify that parameter types, return types, and potential exceptions must be documented in a structured docstring format. Emphasize that internal logical statements must remain completely unchanged to preserve original application behavior.
Transform undocumented functions with AI suggestions
Consider this synthetic example of undocumented legacy Python code processed by an AI prompt configured for documentation tasks.
Legacy input code:
def calc_total(items, tax_rate):
val = sum(i[‘price’] for i in items)
return val + (val * tax_rate)
Generated output code with complete docstrings:
def calc_total(items: list[dict], tax_rate: float) -> float:
“””Calculate the total price of items including tax.
Parameters:
items -- A list of dictionaries, where each dict contains a price key.
tax_rate -- The tax percentage expressed as a float.
Returns:
The final combined value as a float.
"""
val = sum(i['price'] for i in items)
return val + (val * tax_rate)
Prevent silent logical shifts during docstring generation
A frequent operational hazard involves AI models silently refactoring legacy logic while attempting to clarify docstrings. For example, an AI might replace manual dictionary iteration with standard library helpers that alter handling of missing keys. If the original legacy function relied on implicit type coercion, the AI might add restrictive type hints that cause runtime type checking errors in downstream consumer code.
To correct this issue, always prompt the AI with a strict negative constraint. Command the tool to only append docstrings and inline type annotations without altering any executable statements.
Review and validate generated module documentation
Always perform a complete visual diff review before accepting generated documentation changes into source control. Verify that the syntax of added docstrings matches standard Python docstring rules and that parameter descriptions accurately reflect actual module usage.
Run static analysis tools to verify type hints:
mypy legacy_module.py
Run unit tests after applying documentation:
python -m unittest discover
A final human review step ensures that inferred business logic written into the docstring matches the true operational intent of the original author.