跳到内容

过度缩进的章节下划线 (D215)

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

参考