Automatic documentation from sources, for MkDocs.
npx @tessl/cli install tessl/pypi-mkdocstrings@0.30.0A language-agnostic documentation generation system for MkDocs that automatically extracts and renders documentation from source code. mkdocstrings enables developers to embed API documentation directly into their Markdown files using a simple ::: identifier syntax, supports multiple programming languages through extensible handlers, and offers cross-referencing capabilities both within projects and across external libraries.
pip install mkdocstringsimport mkdocstringsFor MkDocs plugin configuration:
# mkdocs.yml
plugins:
- mkdocstringsFor direct extension usage:
from mkdocstrings import MkdocstringsExtension, BaseHandler, Handlerspip install mkdocstringsmkdocs.yml:plugins:
- mkdocstrings:
handlers:
python:
options:
docstring_style: google# My API Documentation
::: my_module.MyClass
options:
show_source: false
::: my_module.my_functionfrom markdown import Markdown
from mkdocstrings import MkdocstringsExtension, Handlers
# Create handlers instance
handlers = Handlers(
theme="material",
default="python",
inventory_project="my-project"
)
# Create and configure Markdown instance
md = Markdown(extensions=[
MkdocstringsExtension(handlers=handlers, autorefs=None)
])
# Process markdown with autodoc blocks
result = md.convert("""
::: my_module.MyClass
options:
show_source: false
""")mkdocstrings follows a modular architecture centered around handlers and processors:
MkdocstringsPlugin integrates with MkDocs build processMkdocstringsExtension and AutoDocProcessor process ::: identifier blocks in MarkdownBaseHandler and Handlers provide language-specific documentation extractionInventory and InventoryItem enable cross-project documentation linkingThis modular design enables extensibility through custom handlers while maintaining consistent processing and rendering across different programming languages.
Core MkDocs plugin functionality for seamless integration with MkDocs sites, including configuration management, build process integration, and theme support.
class MkdocstringsPlugin(BasePlugin[PluginConfig]):
def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None: ...
def on_post_build(self, config: MkDocsConfig, **kwargs: Any) -> None: ...
def get_handler(self, handler_name: str) -> BaseHandler: ...
class PluginConfig(Config):
handlers = opt.Type(dict, default={})
default_handler = opt.Type(str, default="python")
custom_templates = opt.Optional(opt.Dir(exists=True))
enable_inventory = opt.Optional(opt.Type(bool))
enabled = opt.Type(bool, default=True)
locale = opt.Optional(opt.Type(str))Extensible handler system for processing different programming languages, with base classes for creating custom handlers and a manager for handler lifecycle.
class BaseHandler:
name: ClassVar[str] = ""
domain: ClassVar[str] = ""
enable_inventory: ClassVar[bool] = False
def collect(self, identifier: str, options: HandlerOptions) -> CollectorItem: ...
def render(self, data: CollectorItem, options: HandlerOptions, *, locale: str | None = None) -> str: ...
class Handlers:
def __init__(self, *, theme: str, default: str, inventory_project: str, ...): ...
def get_handler(self, name: str, handler_config: dict | None = None) -> BaseHandler: ...Markdown extension and processors for parsing and rendering ::: identifier autodoc blocks within Markdown documents.
class MkdocstringsExtension(Extension):
def __init__(self, handlers: Handlers, autorefs: AutorefsPlugin, **kwargs: Any) -> None: ...
def extendMarkdown(self, md: Markdown) -> None: ...
class AutoDocProcessor(BlockProcessor):
def test(self, parent: Element, block: str) -> bool: ...
def run(self, parent: Element, blocks: MutableSequence[str]) -> None: ...Object inventory system for managing and cross-referencing documentation objects within and across projects.
class Inventory(dict):
def __init__(self, items: list[InventoryItem] | None = None, project: str = "project", version: str = "0.0.0") -> None: ...
def register(self, name: str, domain: str, role: str, uri: str, priority: int = 1, dispname: str | None = None) -> None: ...
def format_sphinx(self) -> bytes: ...
class InventoryItem:
def __init__(self, name: str, domain: str, role: str, uri: str, priority: int = 1, dispname: str | None = None) -> None: ...HTML rendering components including syntax highlighting, heading management, and output formatting processors.
class Highlighter(Highlight):
def highlight(self, src: str, language: str | None = None, *, inline: bool = False, dedent: bool = True, linenums: bool | None = None, **kwargs: Any) -> str: ...
class HeadingShiftingTreeprocessor(Treeprocessor):
def __init__(self, md: Markdown, shift_by: int) -> None: ...
def run(self, root: Element) -> None: ...Specialized logging system designed for template environments and debugging mkdocstrings processing.
class LoggerAdapter(logging.LoggerAdapter):
def __init__(self, prefix: str, logger: logging.Logger) -> None: ...
class TemplateLogger:
def __init__(self, logger: LoggerAdapter) -> None: ...
def get_logger(name: str) -> LoggerAdapter: ...
def get_template_logger(handler_name: str | None = None) -> TemplateLogger: ...CollectorItem = Any # Type of item returned by handler's collect method
HandlerConfig = Any # Type for handler configuration
HandlerOptions = Any # Type for options passed to handlersclass CollectionError(Exception):
"""Raised when data collection fails."""
class ThemeNotSupported(Exception):
"""Raised when a theme is not supported."""TEMPLATES_DIRS: Sequence[Path] # Directories where handler templates are locateddef do_any(seq: Sequence, attribute: str | None = None) -> bool:
"""Check if any item in sequence evaluates to True (Jinja filter)."""
def get_template_path(context: Context) -> str:
"""Return path to template using given context."""
def get_template_logger_function(logger_func: Callable) -> Callable:
"""Create wrapper function for template logging."""