跳到内容

缺少分段标题下方的虚线 (D407)

源自 pydocstyle 代码检查器。

修复总是可用的。

作用

检查文档字符串中的分段标题是否后跟下划线。

为什么这不好?

此规则强制多行 numpy 样式文档字符串采用一致的风格,并有助于防止使用 reStructuredText 的文档字符串中出现不正确的语法。

多行 numpy 样式文档字符串通常由摘要行、一个空行以及一系列分段组成。每个分段都有一个分段标题和一个分段正文,并且标题后应在下一行跟一系列下划线字符。

此规则强制具有分段的多行 numpy 样式文档字符串采用一致的风格。如果你的文档字符串使用 reStructuredText,该规则还有助于防止不正确的 reStructuredText 语法,如果你尝试使用诸如 Sphinx 之类的工具从文档字符串生成文档,则会导致错误。

当使用 numpy 约定启用此规则,当使用 googlepep257 约定禁用此规则。

示例

def calculate_speed(distance: float, time: float) -> float:
    """Calculate speed as distance divided by time.

    Parameters

    distance : float
        Distance traveled.
    time : float
        Time spent traveling.

    Returns

    float
        Speed as distance divided by time.

    Raises

    FasterThanLightError
        If speed is greater than the speed of light.
    """
    try:
        return distance / time
    except ZeroDivisionError as exc:
        raise FasterThanLightError from exc

建议改为

def calculate_speed(distance: float, time: float) -> float:
    """Calculate speed as distance divided by time.

    Parameters
    ----------
    distance : float
        Distance traveled.
    time : float
        Time spent traveling.

    Returns
    -------
    float
        Speed as distance divided by time.

    Raises
    ------
    FasterThanLightError
        If speed is greater than the speed of light.
    """
    try:
        return distance / time
    except ZeroDivisionError as exc:
        raise FasterThanLightError from exc

Options (选项)

参考