CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-sphinx-autoapi

Sphinx extension for automatically generating complete API documentation from source code without requiring the project to be loaded, run, or imported

Pending
Overview
Eval results
Files

generation.mddocs/

Documentation Generation and Mapping

Core mapping functionality that converts parsed source code into comprehensive documentation structure. The Mapper handles file discovery, content organization, and output generation using Jinja2 templates and reStructuredText format.

Core Mapper Class

Mapper

class Mapper:
    """
    Maps source code to documentation objects and handles output generation.
    
    The Mapper coordinates the entire documentation generation pipeline:
    - Discovers and loads source files
    - Parses code into object representations  
    - Maps objects to documentation structure
    - Renders output using templates
    
    Attributes:
        objects (list[PythonObject]): Top-level documentation objects
        paths (list[str]): Paths to generated documentation files
    """
    
    def load(self, patterns: list[str], dirs: list[str], ignore: list[str]) -> bool:
        """
        Load and parse source files from specified directories.
        
        Args:
            patterns (list[str]): File patterns to include (e.g., ['*.py', '*.pyi'])
            dirs (list[str]): Source directories to scan
            ignore (list[str]): Patterns to ignore (e.g., ['*tests*'])
            
        Returns:
            bool: True if any source files were found and loaded
            
        Recursively discovers Python files, parses them using astroid,
        and builds the complete object hierarchy.
        """
    
    def map(self, options: list[str]) -> None:
        """
        Map loaded objects to documentation structure.
        
        Args:
            options (list[str]): AutoAPI options controlling content inclusion
            
        Processes the loaded objects according to configuration options:
        - Filters objects based on inclusion options
        - Resolves inheritance relationships
        - Builds cross-reference mappings
        - Organizes objects into documentation hierarchy
        """
    
    def output_rst(self, source_suffix: str = '.rst') -> None:
        """
        Generate reStructuredText documentation files.
        
        Args:
            source_suffix (str): File extension for generated files
            
        Renders all mapped objects to .rst files using Jinja2 templates:
        - Creates individual files for modules, classes, etc.
        - Generates index files with navigation
        - Applies custom templates if configured
        - Writes files to autoapi_root directory
        """

From autoapi._mapper:

from autoapi._mapper import Mapper

File Discovery and Loading

Source File Discovery

# File pattern matching
"""The Mapper discovers source files using glob patterns:
- Default patterns: ['*.py', '*.pyi']
- Custom patterns via autoapi_file_patterns
- Recursive directory scanning
- Ignore pattern filtering
"""

# Directory structure handling
"""Supports complex project structures:
- Single-file modules
- Package hierarchies with __init__.py
- Namespace packages (PEP 420)
- Mixed source and binary distributions
"""

Loading Process

def load(self, patterns, dirs, ignore):
    """
    Multi-stage loading process:
    
    1. File Discovery:
       - Scan directories recursively
       - Apply include/exclude patterns
       - Build file path lists
    
    2. Parsing:
       - Parse each file with astroid
       - Handle syntax errors gracefully
       - Extract object hierarchies
    
    3. Organization:
       - Build package/module structure
       - Resolve import relationships
       - Create cross-reference mappings
    
    Returns True if any files were successfully loaded.
    """

Object Mapping and Organization

Content Filtering

def map(self, options):
    """
    Apply AutoAPI options to control content inclusion:
    
    Options processed:
    - 'members': Include documented members
    - 'undoc-members': Include undocumented members  
    - 'private-members': Include private members (_name)
    - 'special-members': Include special methods (__name__)
    - 'show-inheritance': Display inheritance relationships
    - 'show-module-summary': Generate module summary tables
    - 'imported-members': Include imported objects
    """

Inheritance Resolution

# Inheritance processing
"""The mapping stage resolves complex inheritance:
- Method Resolution Order (MRO) calculation
- Inherited member identification
- Abstract method detection
- Multiple inheritance support
- Mixin class handling
"""

# Cross-reference building
"""Creates comprehensive cross-reference data:
- Type annotation resolution
- Import alias tracking
- Inter-module references
- External package references
"""

Template-Based Rendering

Template System

def output_rst(self, source_suffix='.rst'):
    """
    Template-based rendering system:
    
    1. Template Selection:
       - Object type determines template (class.rst, function.rst)
       - Custom templates override defaults
       - Template inheritance supported
    
    2. Context Preparation:
       - Object properties and metadata
       - Configuration values
       - Cross-reference data
       - Sphinx-specific context
    
    3. Rendering:
       - Jinja2 template processing
       - reStructuredText generation
       - File output with proper encoding
    """

Template Hierarchy

# Default templates (autoapi/templates/python/):
"""
├── base.rst              # Base template with common elements
├── index.rst             # Main API index page
├── package.rst           # Package documentation
├── module.rst            # Module documentation
├── class.rst             # Class documentation
├── function.rst          # Function documentation
├── method.rst            # Method documentation
├── property.rst          # Property documentation
├── attribute.rst         # Attribute documentation
├── data.rst              # Data/variable documentation
└── exception.rst         # Exception class documentation
"""

# Template customization
"""Custom templates can be provided via:
- autoapi_template_dir configuration
- Template inheritance from defaults
- Per-object-type template overrides
"""

Configuration Integration

Sphinx Configuration

# Mapper uses Sphinx configuration values:
"""
- autoapi_root: Output directory for generated files
- autoapi_own_page_level: Level for separate pages
- autoapi_member_order: Member ordering preference
- autoapi_include_summaries: Summary table generation
- autoapi_template_dir: Custom template directory
- autoapi_prepare_jinja_env: Environment customization
"""

Output Organization

# Generated file structure:
"""
autoapi/                  # Default autoapi_root
├── index.rst            # Main API index
├── package_name/        # Package directory
│   ├── index.rst       # Package index
│   ├── module.rst      # Module documentation
│   └── subpackage/     # Nested packages
└── _templates/         # Template overrides (if any)
"""

# Page level control:
"""
autoapi_own_page_level determines file organization:
- 'module': Each module gets own file
- 'class': Each class gets own file  
- 'function': Each function gets own file
"""

Error Handling and Robustness

Graceful Degradation

# Error handling strategies:
"""
1. Parse Errors:
   - Log syntax errors with file/line info
   - Continue processing other files
   - Generate partial documentation

2. Import Resolution:
   - Handle missing dependencies gracefully
   - Use string representations for unresolved types
   - Maintain cross-references where possible

3. Template Errors:
   - Fall back to simpler templates
   - Log rendering issues
   - Generate basic documentation over none

4. File System:
   - Handle permission issues
   - Create directories as needed
   - Atomic file writing where possible
"""

Advanced Features

Custom Jinja Environment

def prepare_jinja_env(jinja_env):
    """
    Customize Jinja2 environment for advanced template features.
    
    Args:
        jinja_env: Jinja2 environment instance
        
    Can be configured via autoapi_prepare_jinja_env:
    - Add custom filters
    - Register template functions
    - Configure autoescape settings
    - Add template globals
    """

Integration Hooks

# Sphinx event integration:
"""
The Mapper integrates with Sphinx events:
- autoapi-skip-member: Filter objects during mapping
- build-finished: Clean up generated files
- source-read: Populate cross-references
"""

Usage Example

from autoapi._mapper import Mapper

# Create and configure mapper
mapper = Mapper()

# Load source files
success = mapper.load(
    patterns=['*.py', '*.pyi'],
    dirs=['src', 'mypackage'],
    ignore=['*tests*', '*migrations*']
)

if success:
    # Map objects with options
    mapper.map([
        'members',
        'undoc-members',
        'show-inheritance',
        'show-module-summary'
    ])
    
    # Generate documentation files
    mapper.output_rst('.rst')
    
    # Access generated objects
    for obj in mapper.objects:
        print(f"Generated docs for: {obj.name}")

The Mapper provides the core engine for converting Python source code into comprehensive, navigable documentation that integrates seamlessly with Sphinx documentation systems.

Install with Tessl CLI

npx tessl i tessl/pypi-sphinx-autoapi

docs

directives.md

extension.md

generation.md

index.md

objects.md

parsing.md

tile.json