FAQ
Show inherited fields/validators
Pydantic models can be subclassed to inherit fields and validators from base
classes. Naturally, autodoc_pydantic should also show these members.
By default, sphinx autodoc does not include any member from base classes, though.
However, sphinx autodoc provides a directive option named :inherited-members:
which allows to include all members from all base classes except object
(see docs here).
Unfortunately, this will also include all members from pydantic.BaseModel
(e.g. copy()
, schema()
etc…) which is most likely not what one wants.
Luckily, :inherited-members:
takes a parameter which allows to exclude base classes.
Hence, when supplying BaseModel
as an argument for :inherited-members:
,
irrelevant members are ignored:
from pydantic import BaseModel
class MyBase(BaseModel):
"""MyBase"""
field_on_base: str
"""Base Field"""
class MySubclass(MyBase):
"""MySubClass"""
field_on_subclass: str
"""Subclass field"""
.. automodule:: target.faq.inherited_members
:inherited-members: BaseModel
- pydantic model MyBase[source]
MyBase
Show JSON schema
{ "title": "MyBase", "description": "MyBase", "type": "object", "properties": { "field_on_base": { "title": "Field On Base", "type": "string" } }, "required": [ "field_on_base" ] }
- Fields
- field field_on_base: str [Required]
Base Field
- pydantic model MySubclass[source]
MySubClass
Show JSON schema
{ "title": "MySubclass", "description": "MySubClass", "type": "object", "properties": { "field_on_base": { "title": "Field On Base", "type": "string" }, "field_on_subclass": { "title": "Field On Subclass", "type": "string" } }, "required": [ "field_on_base", "field_on_subclass" ] }
- field field_on_base: str [Required]
Base Field
- field field_on_subclass: str [Required]
Subclass field
Note
For more, please see the corresponding github issue #32.
Exclude __init__
docstring
If a pydantic model’s documentation rendered by autodoc_pydantic
includes the docstring from the pydantic base class or from the model’s
__init__
method, it may be due to autodoc’s autoclass_content
setting in sphinx’s conf.py
.
The configuration below tells Sphinx to include both the class docstring
and that of __init__
for auto-documented classes:
autoclass_content = "both"
This behavior does also apply to autodoc_pydantic’s
auto-documenters. If you haven’t overwritten the __init__
method in your model, this will look exactly like it has
inherited the Pydantic base class docstring. In order to only
show the class docstring, change this setting back to “class”:
autoclass_content = "class"
Note
For more, please see the corresponding github issue #58.