【问题标题】:Autodoc failing with class that has nested pydantic modelAutodoc 因具有嵌套 pydantic 模型的类而失败
【发布时间】: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 pydanticautodoc_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


    【解决方案1】:

    此问题已在版本 1.7.2 中修复。

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2021-09-06
      • 2022-07-28
      • 2020-10-27
      • 2013-02-19
      • 2017-08-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多