狮身人面像两次显示属性

问题描述

我正在尝试使用 sphinx 编写 API 文档 我从模型开始,但无法正确记录属性

这是一个例子:

class Incoterm(AuditedModel(updatable=True),ModelBase,db.Model):
    """Incoterm DeFinition

    Args:
        AuditedModel (class): Audit class
        ModelBase (class): Model base class
        db (class): sqlAlchemy instance

    Attributes:
        abbreviation (String): an abbreviation
        full_name (String): the full name
        group (String): which group it belong

    """

    abbreviation = Column(String(3),nullable=False)
    full_name = Column(String(128),nullable=False)
    group = Column(String(1),nullable=False)

使用 $ sphinx-apidoc -o . ..$ make html 生成的文档显示了两次属性和没有定义的类继承。

我还尝试在 sphinx examples 中描述的 #: str: an string 之类的变量之前插入注释,但得到了相同的结果...

我发现的一个解决方法是在 .rst 文件中注释 :undoc-members:。 有没有更好的方法来实现这一目标?我错过了什么?

enter image description here

解决方法

如果您像这样记录类属性,问题很可能会消失。

class Incoterm(AuditedModel(updatable=True),ModelBase,db.Model):
    """Incoterm Definition

    Args:
        AuditedModel (class): Audit class
        ModelBase (class): Model base class
        db (class): SQLAlchemy instance
    """

    abbreviation = Column(String(3),nullable=False)
    """an abbreviation"""

    full_name = Column(String(128),nullable=False)
    """the full name"""

    group = Column(String(1),nullable=False)
    """which group it belong"""

参考: https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute 向下滚动到 class Foo 示例。