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

如何让Sphinx自动文档化TypedDict中的‘items’属性?

如何让Sphinx自动文档化TypedDict中的‘items’属性?

我定义了一个自定义TypedDict类型,代码如下:

custom_type.py

from typing import TypedDict

class CustomDict(TypedDict):
    """A custom type definition"""
    items: list[_AnotherCustomDict]
    """The items."""

我的文档源文件是这样的:

custom_type.rst

.. autoclass:: CustomDict
   :members:
   :member-order: bysource
   :show-inheritance:

但是当我运行make html生成文档后,结果文档并没有包含items属性。

我尝试修改文档源,指定要包含的成员:

custom_type.rst

.. autoclass:: CustomDict
   :members: items
   :member-order: bysource
   :show-inheritance:

但生成的文档里反而列出了items(),描述来自dict的.items()方法。我大概明白原因:因为dict本身就有.items()方法,属性和方法不能使用同一个名称。

我还试过用另一种TypedDict的实例语法来定义类型:

custom_type.py

#: A custom type definition
CustomDict = TypedDict('CustomDict', {
    'items': list[_AnotherCustomDict], #: The items.
})

但items还是没有出现在文档里。

可惜我没办法把items改成别的名字,因为这是我要对接的第三方API规定的字段名,我不想为了文档就修改这个字段名。

所以我该怎么让Sphinx正确自动文档化这个TypedDict里的items属性呢?

我的环境配置:Python 3.13,Sphinx 8.1.3

备注:内容来源于stack exchange,提问作者yuhui

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 15:18:00