autodoc-pydantic

2.2.0 · active · verified Wed Apr 15

autodoc-pydantic is a Sphinx extension that seamlessly integrates Pydantic models into Sphinx documentation. It automatically generates documentation for model fields, validators, and configurations, enhancing the standard Sphinx autodoc capabilities for data models. The current version is 2.2.0, with an active development cycle and regular updates.

Common errors

Warnings

Install

Imports

Quickstart

To integrate autodoc-pydantic, first create a Sphinx project. Add 'sphinx.ext.autodoc' and 'autodoc_pydantic' to your `extensions` list in `conf.py`. Define your Pydantic models in Python modules. Then, use Sphinx's `automodule` combined with autodoc-pydantic's specific directives (e.g., `autodoc_pydantic_model`, `autodoc_pydantic_field`) in your documentation files to generate detailed API documentation for your Pydantic models.

# 1. Install dependencies: pip install autodoc-pydantic sphinx pydantic

# 2. Initialize a Sphinx project (e.g., using 'sphinx-quickstart')

# 3. Modify conf.py:
#    Add 'sphinx.ext.autodoc' and 'autodoc_pydantic' to the extensions list.
#    Example conf.py snippet:
#    extensions = [
#        'sphinx.ext.autodoc',
#        'sphinx.ext.napoleon', # For Google/NumPy style docstrings
#        'autodoc_pydantic'
#    ]

# 4. Create a Python file (e.g., 'my_models.py'):
#    from pydantic import BaseModel, Field
#
#    class User(BaseModel):
#        """Represents a user in the system."""
#        name: str = Field(..., description="The user's full name")
#        age: int = Field(default=18, ge=0, description="The user's age")

# 5. Create an RST or Markdown file (e.g., 'models.rst'):
#    .. automodule:: my_models
#       :members:
#
#    .. autodoc_pydantic_model:: my_models.User
#       :model-show-json: False
#       :model-show-config-summary: False

# 6. Build your documentation: make html (or sphinx-build -b html . _build)

view raw JSON →