Skip to main content

A simple integration between autodoc and Pydantic in your Sphinx documentation.

Project description

Ruff Code style: black Imports: isort codecov tests

sphinxcontrib-pydantic

A Sphinx extension for documenting Pydantic models in your documentation.

This project was inspired by autodoc_pydantic, which I have been using for years but has gone unmaintained and is now incompatible with Sphinx 9.0+. Unlike its predecessor, sphinxcontrib-pydantic focuses exclusively on modern versions: Pydantic v2+ and Sphinx 9.0+ (Python 3.11+).

This project was vibe-coded using Claude Code (Opus 4.5); using autodoc_pydantic as inspiration as well as the source code of sphinx and pydantic as documentation.

Installation

pip install sphinxcontrib-pydantic

For pydantic-settings support:

pip install sphinxcontrib-pydantic[settings]

Configuration

Enable the extension in your conf.py:

extensions = [
    "sphinx.ext.autodoc",
    "sphinxcontrib.pydantic",
]

Usage

With autodoc

The extension integrates with sphinx.ext.autodoc. Simply use automodule or autoclass:

.. automodule:: mypackage.models
   :members:

Pydantic models are automatically detected and documented with field summaries and validators.

With autosummary

For larger projects, use autosummary with a custom template:

.. currentmodule:: mypackage.models

.. autosummary::
   :toctree: generated/api
   :template: autosummary/models.rst

   UserConfig
   DatabaseConfig

Create _templates/autosummary/models.rst:

{{ fullname | escape | underline }}

.. currentmodule:: {{ module }}

.. pydantic-model:: {{ fullname }}
   :inherited-members: BaseModel

Standalone directives

Document models directly with the pydantic-model directive:

.. pydantic-model:: mypackage.models.UserConfig
   :show-field-summary:
   :show-validator-summary:

For Pydantic settings:

.. pydantic-settings:: mypackage.settings.AppSettings
   :show-field-summary:

Configuration Options

All options use the sphinxcontrib_pydantic_ prefix in conf.py, e.g. sphinxcontrib_pydantic_model_show_json.

Model Options

Option Default Description
model_show_json False Show JSON schema for the model
model_show_field_summary True Show summary table of fields
model_show_validator_summary True Show summary table of validators
model_show_config False Show model configuration
model_signature_prefix "model" Prefix shown before model name
model_hide_paramlist True Hide __init__ parameter list

Field Options

Option Default Description
field_show_alias True Show field aliases
field_show_default True Show default values
field_show_required True Show required status
field_show_constraints True Show field constraints (e.g., min_length)

Validator Options

Option Default Description
validator_list_fields True List fields affected by each validator

Settings Options

Settings options mirror model options with settings_ prefix (e.g., sphinxcontrib_pydantic_settings_show_json). They default to the same values as their model counterparts.

Example Configuration

# conf.py
sphinxcontrib_pydantic_model_show_json = True
sphinxcontrib_pydantic_model_show_field_summary = True
sphinxcontrib_pydantic_field_show_constraints = True
sphinxcontrib_pydantic_settings_signature_prefix = "config"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxcontrib_pydantic-0.1.0.tar.gz (237.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinxcontrib_pydantic-0.1.0-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file sphinxcontrib_pydantic-0.1.0.tar.gz.

File metadata

  • Download URL: sphinxcontrib_pydantic-0.1.0.tar.gz
  • Upload date:
  • Size: 237.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sphinxcontrib_pydantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b068f2926bbf2e6b4679ba767ed8a1fa113c4e10adc7c06b83a25d4748dd35c3
MD5 4017ebba8273d1c35dbf974049254495
BLAKE2b-256 d86b02c005a8b42de4a99bd95d064f72b9cb4cd1fb2df62a86375844c2038bc1

See more details on using hashes here.

File details

Details for the file sphinxcontrib_pydantic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sphinxcontrib_pydantic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sphinxcontrib_pydantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 502d1efa7c5f259e0ecdc13497d1e4e81d9291af6dee441508b7c3b27dc4183e
MD5 40c12b03daae41ca2d6f454e9f943e62
BLAKE2b-256 905fd27624f28aeed4b55440cd8ee68a99fb55ce54d881891075764773c7f471

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page