Automated docstring generation with multiple style support, error detection, and inline comments
Try it now: Python Docstring Generator π
Experience the app live without any installation required!
Python Docstring Generator is a powerful, template-based tool that automatically generates professional docstrings for your Python code. It supports multiple docstring styles (Google, NumPy, reST), detects code issues, and adds helpful inline commentsβall through a clean, intuitive web interface.
- π¨ Multiple Docstring Styles - Google, NumPy, and reStructuredText (reST)
- β Docstring Validation - Validates generated docstrings for quality, completeness, and PEP 257 compliance
- π Error Detection - Identifies syntax errors, unused imports, and missing type hints
- π¬ Inline Comments - Automatically adds comments for complex code constructs
- β Smart Detection - Preserves existing docstrings
- π― Selective Generation - Review and accept only the docstrings you want
- β‘ Instant Generation - Template-based, no API calls required
- π¨ Clean UI - Simple 3-step workflow
graph TB
A[User uploads Python file] --> B[Parser Module]
B --> C[AST Analysis]
C --> D[Generator Module]
C --> E[Validator Module]
C --> F[Error Detector]
C --> G[Comment Generator]
D --> H[Docstring Generation]
E --> I[Quality Validation]
F --> J[Issue Detection]
G --> K[Inline Comments]
H --> L[Inserter Module]
I --> L
J --> L
K --> L
L --> M[Enhanced Python File]
M --> N[User Download]
| Module | Purpose | Key Functions |
|---|---|---|
Parser (parser.py) |
Extracts function/class metadata using AST | parse_python_file() |
Generator (generator.py) |
Creates docstrings in multiple styles | generate_function_docstring(), generate_class_docstring() |
Validator (validator.py) |
Validates docstring quality and compliance | validate_docstring(), check_pep257_compliance() |
Error Detector (error_detector.py) |
Identifies code issues | detect_issues() |
Comment Generator (comment_generator.py) |
Adds inline comments | generate_inline_comments() |
Inserter (inserter.py) |
Combines docstrings into source code | insert_docstrings() |
Models (models.py) |
Data structures for metadata | FunctionInfo, ClassInfo, Parameter |
App (app.py) |
Streamlit UI and workflow | Main application |
# Clone the repository
git clone https://github.com/Megesh07/Python-Doctring-Generator.git
cd Python-Docstring-Generator
# Install dependencies
pip install -r requirements.txtstreamlit run app.pyThe app will open in your browser at http://localhost:8501
Upload any Python file (.py) through the web interface.
- View all generated docstrings
- See detected code issues
- Accept or reject individual docstrings
Download your Python file with the accepted docstrings and inline comments.
def calculate_sum(a: int, b: int) -> int:
"""
Calculate Sum function.
Args:
a (int): The a parameter.
b (int): The b parameter.
Returns:
int: Return value.
"""
return a + bdef calculate_sum(a: int, b: int) -> int:
"""
Calculate Sum function.
Parameters
----------
a : int
The a parameter.
b : int
The b parameter.
Returns
-------
int
Return value.
"""
return a + bdef calculate_sum(a: int, b: int) -> int:
"""
Calculate Sum function.
:param a: The a parameter.
:type a: int
:param b: The b parameter.
:type b: int
:returns: Return value.
:rtype: int
"""
return a + b- Purpose: Generates clear function/class descriptions
- Parameters: Documents all parameters with types and defaults
- Returns: Includes return type information
- Filters: Automatically excludes
selfandclsparameters
Detects and reports:
- β Syntax errors
- β Unused imports
- β Missing type hints (parameters)
- β Missing return type hints
Automatically adds comments for:
- List comprehensions
- Dictionary comprehensions
- Lambda functions
- Try-except blocks
- Context managers
- Preserves existing docstrings
- Shows "Already documented" status
- Skips generation for documented items
The validator ensures every generated docstring meets professional standards.
What it validates:
-
β PEP 257 Compliance
- Summary line format and punctuation
- Blank line after summary (multi-line docstrings)
- Proper docstring structure
-
β Format Compliance
- Google style:
Args:andReturns:sections - NumPy style: Dashed section headers
- reST style:
:paramand:returns:directives
- Google style:
-
β Completeness
- All parameters documented
- Return type documented (if applicable)
- No missing information
Quality Scoring (0-100):
- 90-100: β Excellent - Production-ready
- 70-89:
β οΈ Good - Minor improvements suggested - 0-69: β Needs improvement - Review warnings
Example Validation Output:
calculate_area (Line 5)
β
Validation: 100/100 (Excellent)
View docstring:
"""
Calculate Area function.
Args:
length: The length parameter.
width: The width parameter.
"""
Python-Docstring-Generator/
βββ app.py # Main Streamlit application
βββ models.py # Data classes (FunctionInfo, ClassInfo, etc.)
βββ parser.py # AST-based Python code parser
βββ generator.py # Multi-style docstring generator
βββ validator.py # Docstring quality validator
βββ inserter.py # Docstring insertion logic
βββ error_detector.py # Code issue detection
βββ comment_generator.py # Inline comment generation
βββ sample.py # Example Python file for testing
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ architecture_diagram.png # System architecture diagram
βββ .gitignore # Git ignore rules
- Python 3.8+ - Core language
- Streamlit - Web UI framework
- AST (Abstract Syntax Tree) - Code parsing
- Dataclasses - Data structures
-
Template-Based Generation
- No AI/API dependencies
- Instant generation
- Completely offline
- Free to use
-
AST Parsing
- Accurate code analysis
- Extracts metadata reliably
- Handles complex Python syntax
-
Multi-Style Support
- Supports 3 major docstring styles
- Easy to add new styles
- Consistent formatting
def calculate_area(length, width):
return length * width
class DataProcessor:
def process(self):
return [x * 2 for x in self.data]def calculate_area(length, width):
"""
Calculate Area function.
Args:
length: The length parameter.
width: The width parameter.
"""
return length * width
class DataProcessor:
"""
Data Processor class.
Provides methods: process.
"""
def process(self):
"""
Process function.
"""
return [x * 2 for x in self.data] # List comprehension- Code Documentation - Quickly document existing codebases
- Code Review - Ensure all functions are documented
- Learning - Understand proper docstring formatting
- Standardization - Enforce consistent documentation style
- Open Source - Prepare code for public release
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Streamlit
- Inspired by Python documentation best practices
- Supports PEP 257 docstring conventions
For questions or feedback, please open an issue on GitHub.
