跳到内容

缺少节标题下划线 (D408)

源自 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 (选项)

参考