autodoc-pydantic
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
-
ModuleNotFoundError: No module named 'autodoc_pydantic'
cause This error occurs when the 'autodoc_pydantic' package is not installed in the Python environment.fixInstall the package using pip: 'pip install autodoc_pydantic'. -
AttributeError: type object 'Foo' has no attribute 'Config'
cause This error occurs when autodoc_pydantic misinterprets a non-Pydantic class as a Pydantic model, leading it to expect a 'Config' attribute that doesn't exist.fixEnsure that only classes inheriting from 'pydantic.BaseModel' are processed by autodoc_pydantic, and update to version 1.7.2 or later where this issue is resolved. -
AttributeError: module 'pydantic' has no attribute 'Field'
cause This error occurs when there's a version mismatch between Pydantic and autodoc_pydantic, leading to compatibility issues.fixEnsure that both Pydantic and autodoc_pydantic are updated to compatible versions; for Pydantic v2, use autodoc_pydantic >= 2.0.0. -
Unknown directive type
cause The autodoc-pydantic Sphinx extension has not been properly enabled in your Sphinx project's configuration.fixAdd 'sphinxcontrib.autodoc_pydantic' to the 'extensions' list in your `conf.py` file. -
WARNING: py:obj reference target not found: typing.Annotated[...]
cause Sphinx's autodoc, which autodoc-pydantic relies on, struggles to resolve complex type hints like `typing.Annotated` within Pydantic models, possibly due to version incompatibilities or inherent limitations in Sphinx's type resolution.fixEnsure Sphinx, Pydantic, and autodoc-pydantic are updated to compatible versions. Consider simplifying complex type hints if possible, or investigate specific Sphinx configuration options for handling annotations.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes in configuration options to align with Pydantic v2 and refine features. Many `autodoc_pydantic_*` settings were renamed, removed, or had their default behavior altered.
- gotcha For autodoc-pydantic's directives to function correctly, `sphinx.ext.autodoc` must be present in your `extensions` list in `conf.py`. Without it, Pydantic model documentation will fail or appear incomplete.
- gotcha autodoc-pydantic automatically uses the `description` argument of Pydantic's `Field` as the field's docstring. This might lead to redundant or undesired output if you're also providing explicit docstrings or using other methods.
- gotcha When using Pydantic v1 vs. Pydantic v2, autodoc-pydantic adapts its behavior. If you switch Pydantic versions, especially upgrading from v1 to v2, your documentation output might change or require configuration adjustments.
Install
-
pip install autodoc-pydantic sphinx pydantic
Imports
- autodoc_pydantic (Sphinx Extension)
extensions = ['autodoc_pydantic.plugin']
# In conf.py, inside the project's source directory: extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', # Or other docstring parsing extensions 'autodoc_pydantic' ]
Quickstart
# 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)