You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

autodoc_pydantic跨文件显示父模型字段问题求助

解决跨模块Pydantic模型继承的文档生成问题

方案1:指定父类完整模块路径

直接在:inherited-members:参数中填写父类的完整模块限定名,替代仅写类名的方式:

.. currentmodule:: module.child_bar

.. autopydantic_model:: Bar
    :inherited-members: module.parent_foo.Foo
    :member-order: bysource
    :model-show-field-summary: false
    :field-show-constraints: false

sphinx-autodoc-pydantic默认会在当前模块下查找父类,跨模块场景必须提供完整路径才能准确定位到父类。

方案2:全局开启继承字段展示

如果存在大量跨模块继承的模型,可在Sphinx配置文件conf.py中添加全局配置,自动包含所有父类字段:

autodoc_pydantic_model_show_inherited_fields = True

开启后无需手动指定:inherited-members:参数,所有Pydantic模型文档都会自动展示跨模块父类的字段,适合批量处理场景。

方案3:结合原生autoclass指令

若上述方案仍有问题,可使用Sphinx原生autoclass指令配合继承参数:

.. currentmodule:: module.child_bar

.. autoclass:: Bar
    :members:
    :undoc-members:
    :show-inheritance:
    :inherited-members: module.parent_foo.Foo

这种方式保留原生autodoc特性的同时,也能正确识别跨模块父类的字段。

内容的提问来源于stack exchange,提问作者maxim-lixakov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 20:22:35