Sphinx Autodoc Typehints

3.9.11 · active · verified Thu Apr 09

sphinx-autodoc-typehints is a Sphinx extension that provides full support for type hints (PEP 484) in the documentation generated by `sphinx.ext.autodoc`. It parses type annotations from Python code and renders them beautifully in reStructuredText or Markdown output. The current version is 3.9.11, and it follows a minor release cadence driven by bug fixes and compatibility with new Sphinx or Python versions.

Warnings

Install

Imports

Quickstart

To quickly integrate `sphinx-autodoc-typehints` into your project, ensure `sphinx.ext.autodoc` is enabled in your `conf.py` first, then add `'sphinx_autodoc_typehints'` to the `extensions` list. This snippet shows a minimal `conf.py` setup, including common configuration options. Create a Python module with type-hinted functions/classes, then use `automodule` directives in your `.rst` files to generate documentation.

import os

# conf.py example

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath.
import sys
sys.path.insert(0, os.path.abspath('.'))

project = 'My Project'
copyright = '2024, Author Name'
author = 'Author Name'
release = '0.1'

extensions = [
    'sphinx.ext.autodoc',
    'sphinx_autodoc_typehints' # Add this extension
]

# Optional: Configure the extension
typehints_fully_qualified = False # This option was removed in v3.0.0 (example of a common mistake for older configs)
typehints_document_rtype = True # Explicitly document return types

# Example Python module (my_module.py)
# def greet(name: str, age: int = 30) -> str:
#     """Greets a person.
#
#     :param name: The name of the person.
#     :param age: The age of the person.
#     :return: A greeting message.
#     """
#     return f"Hello, {name}! You are {age} years old."

view raw JSON →