【发布时间】:2022-06-24 09:47:19
【问题描述】:
作为我的 MRE,我有以下文件:
blah.py
'''Blah module'''
import pydantic
class Foo:
'''Foo class'''
class Bar(pydantic.BaseModel):
'''Bar class'''
x: str = pydantic.Field(description='The x.')
@pydantic.validator('x')
def do_nothing(cls, value: str) -> str:
return value
我正在尝试使用 Sphinx 为该模块生成文档。在我的 conf.py 中,我有
extensions = [
'sphinx.ext.autodoc',
'sphinxcontrib.autodoc_pydantic',
]
我的 blah.rst 是
Blah
====
.. automodule:: blah.blah
:members:
我已经安装了 pip pydantic 和 autodoc_pydantic。
但是,当我make html 时,我得到了
Exception occurred:
File "/home/user/Projects/Workspace/env/lib/python3.10/site-packages/sphinxcontrib/autodoc_pydantic/inspection.py", line 311, in __init__
self.attribute: Dict = self.model.Config
AttributeError: type object 'Foo' has no attribute 'Config'
似乎autodoc_pydantic 认为Foo 继承自pydantic.BaseModel,而实际上Bar 确实如此。如果我从extensions 中删除'sphinxcontrib.autodoc_pydantic',错误就会消失。
更有趣的是,如果我删除验证器,错误也会消失。
autodoc_pydantic 是 1.6.1 版。
【问题讨论】:
标签: python python-sphinx pydantic autodoc autodoc-pydantic