Python Language Supportยถ
Automatic documentation with Sphinx autodoc
Python Language Supportยถ
Introligo provides seamless integration with Sphinxโs autodoc extension for automatic Python API documentation extraction.
Overviewยถ
Python is Introligoโs native language and has first-class support through Sphinxโs powerful autodoc extension. Documentation is automatically extracted from Python docstrings, supporting multiple docstring formats including Google, NumPy, and standard Sphinx styles.
Featuresยถ
โ Automatic Extraction - Uses Sphinx autodoc to extract from docstrings โ Multiple Formats - Google, NumPy, and Sphinx docstring styles โ Full Integration - Complete Sphinx autodoc feature set โ Source Links - Direct links to source code with viewcode โ Inheritance Tree - Automatic class hierarchy documentation โ Type Hints - Native Python type annotation support
Basic Usageยถ
Minimal Configurationยถ
modules:
my_module:
title: "My Python Module"
module: "mypackage.mymodule"
description: "Core functionality"
Configuration Fieldsยถ
Field |
Description |
Required |
|---|---|---|
module |
Python module path (e.g., mypackage.submodule) |
Yes |
language |
Can be set to python (auto-detected from module) |
No |
description |
Brief module description |
Recommended |
How It Worksยถ
Automatic Detectionยถ
Introligo automatically detects Python modules:
modules:
utils:
module: "myproject.utils" # โ Automatically detected as Python
title: "Utilities"
Autodoc Integrationยถ
Detection: Introligo detects the module field
Configuration: Auto-adds Sphinx autodoc extensions
Template: Generates RST with .. automodule:: directive
Extraction: Sphinx autodoc extracts docstrings at build time
Documentation: Beautiful API docs in your site!
Generated RSTยถ
.. automodule:: myproject.utils
:members:
:undoc-members:
:show-inheritance:
:private-members:
:special-members: __init__
Complete Exampleยถ
sphinx:
project: "My Python Project"
html_theme: "furo"
# Python-specific configuration
add_project_to_path: true # Add project to sys.path
autodoc_default_options:
members: true
undoc-members: true
special-members: "__init__"
show-inheritance: true
modules:
api_reference:
title: "API Reference"
description: "Complete Python API documentation"
utils_module:
parent: "api_reference"
title: "Utilities Module"
module: "myproject.utils"
description: "Utility functions and helpers"
overview: |
The utilities module provides common helper functions
used throughout the project.
usage_examples:
- title: "Basic Usage"
language: "python"
code: |
from myproject.utils import slugify
result = slugify("Hello World!")
print(result) # Output: hello-world
models_module:
parent: "api_reference"
title: "Data Models"
module: "myproject.models"
description: "Core data structures"
Docstring Formatsยถ
Google Style (Recommended)ยถ
def calculate(value: int, multiplier: float = 2.0) -> float:
"""Calculate the result of value * multiplier.
Args:
value: The base value to multiply
multiplier: The multiplication factor (default: 2.0)
Returns:
The calculated result
Raises:
ValueError: If value is negative
Example:
>>> calculate(5, 2.5)
12.5
"""
if value < 0:
raise ValueError("Value must be non-negative")
return value * multiplier
NumPy Styleยถ
def process_data(data, threshold=0.5):
"""Process input data with threshold filtering.
Parameters
----------
data : array_like
Input data to process
threshold : float, optional
Filtering threshold (default is 0.5)
Returns
-------
processed : ndarray
Processed data array
Notes
-----
This function applies threshold filtering to the input data.
Examples
--------
>>> process_data([1, 2, 3], threshold=1.5)
array([2, 3])
"""
return [x for x in data if x > threshold]
Sphinx Styleยถ
def connect(host, port=8080):
"""Connect to a server.
:param str host: Server hostname
:param int port: Server port (default: 8080)
:return: Connection object
:rtype: Connection
:raises ConnectionError: If connection fails
"""
pass
Sphinx Configurationยถ
Auto-Configurationยถ
Introligo automatically adds these extensions when Python modules are detected:
sphinx:
extensions: # Auto-added by Introligo
- "sphinx.ext.autodoc" # API documentation
- "sphinx.ext.napoleon" # Google/NumPy style
- "sphinx.ext.viewcode" # Source code links
Manual Configurationยถ
You can customize autodoc behavior:
sphinx:
# Add project to Python path
add_project_to_path: true
project_root: "."
# Autodoc options
autodoc_default_options:
members: true # Document all members
undoc-members: true # Include undocumented
private-members: false # Exclude private (_name)
special-members: "__init__" # Include __init__
inherited-members: true # Show inherited
show-inheritance: true # Show base classes
# Napoleon (Google/NumPy style)
napoleon_google_docstring: true
napoleon_numpy_docstring: true
napoleon_include_init_with_doc: true
napoleon_include_private_with_doc: false
napoleon_use_param: true
napoleon_use_rtype: true
Advanced Featuresยถ
Type Hintsยถ
Python type annotations are automatically included:
from typing import List, Optional, Dict
def process_items(
items: List[str],
config: Optional[Dict[str, int]] = None
) -> List[str]:
"""Process a list of items with optional configuration.
Args:
items: List of items to process
config: Optional configuration dictionary
Returns:
Processed items
"""
return items
Generated documentation shows: - items (List[str]) โ List of items to process - config (Optional[Dict[str, int]]) โ Optional configuration dictionary - Returns: List[str]
Class Documentationยถ
class DataProcessor:
"""Process data with configurable options.
Args:
threshold: Processing threshold
verbose: Enable verbose output
Attributes:
threshold (float): Current threshold value
count (int): Number of items processed
"""
def __init__(self, threshold: float = 0.5, verbose: bool = False):
self.threshold = threshold
self.verbose = verbose
self.count = 0
def process(self, data: list) -> list:
"""Process the input data.
Args:
data: Input data to process
Returns:
Processed data
"""
self.count += len(data)
return [x for x in data if x > self.threshold]
Multiple Modulesยถ
Document several related modules:
modules:
api:
title: "API Reference"
core_module:
parent: "api"
module: "myproject.core"
title: "Core Module"
utils_module:
parent: "api"
module: "myproject.utils"
title: "Utilities"
models_module:
parent: "api"
module: "myproject.models"
title: "Data Models"
Best Practicesยถ
1. Use Google Style Docstringsยถ
def good_function(param1, param2):
"""One-line summary.
Detailed description here.
Args:
param1: Description of param1
param2: Description of param2
Returns:
Description of return value
"""
pass
2. Add Type Hintsยถ
def typed_function(value: int, name: str) -> dict:
"""Function with type hints."""
return {"value": value, "name": name}
3. Document All Public APIsยถ
class MyClass:
"""Class docstring is required."""
def public_method(self):
"""All public methods need docstrings."""
pass
def _private_method(self):
# Private methods can have regular comments
pass
4. Use Examplesยถ
def slugify(text: str) -> str:
"""Convert text to URL-friendly slug.
Args:
text: Text to convert
Returns:
URL-friendly slug
Example:
>>> slugify("Hello World!")
'hello-world'
>>> slugify("Python 3.10")
'python-3-10'
"""
pass
5. Organize with Parentsยถ
modules:
api:
title: "API Reference"
core:
parent: "api"
module: "myproject.core"
title: "Core"
core_auth:
parent: "core"
module: "myproject.core.auth"
title: "Authentication"
Troubleshootingยถ
Module Not Foundยถ
Problem: WARNING: autodoc: failed to import module โmymoduleโ
Solution:
sphinx:
add_project_to_path: true
project_root: "."
Or set PYTHONPATH before building:
export PYTHONPATH="${PYTHONPATH}:/path/to/project"
sphinx-build -b html docs docs/_build/html
Missing Documentationยถ
Problem: Functions appear but have no documentation
Solution: Add docstrings to your Python code:
def my_function():
"""Add this docstring!"""
pass
Private Members Showingยถ
Problem: Private methods (_method) appear in docs
Solution:
sphinx:
autodoc_default_options:
private-members: false
Import Errorsยถ
Problem: Module imports fail during doc build
Solution: Install dependencies or mock them:
sphinx:
autodoc_mock_imports:
- "numpy"
- "pandas"
- "torch"
Example Project Structureยถ
myproject/
โโโ myproject/
โ โโโ __init__.py
โ โโโ core.py # Core functionality
โ โโโ utils.py # Utilities
โ โโโ models.py # Data models
โโโ docs/
โ โโโ introligo_config.yaml
โ โโโ (generated RST files)
โโโ setup.py
Configuration:
sphinx:
project: "My Project"
add_project_to_path: true
project_root: ".."
modules:
api:
title: "API"
core:
parent: "api"
module: "myproject.core"
title: "Core"
utils:
parent: "api"
module: "myproject.utils"
title: "Utilities"
Comparison with Other Languagesยถ
Feature |
Python |
C/C++ |
Go |
|---|---|---|---|
Source |
Docstrings |
Comments |
Comments |
Tool |
autodoc |
Doxygen |
go doc |
Build Time |
Sphinx |
Doxygen + Sphinx |
go doc + Introligo |
Type Hints |
โ Native |
โ ๏ธ Limited |
โ Native |
Examples |
โ In docstrings |
โ ๏ธ External |
โ In comments |
Inheritance |
โ Automatic |
โ Automatic |
N/A |
See Alsoยถ
Summaryยถ
Introligoโs Python support provides:
โ Zero-configuration autodoc integration โ Multiple docstring styles (Google, NumPy, Sphinx) โ Type hint support for modern Python โ Complete Sphinx features (inheritance, source links, etc.) โ Easy organization with parent-child hierarchy
Python documentation in Introligo is as simple as specifying the module name!