跳到内容

过度缩进的节 (D214)

源自 pydocstyle 代码检查器。

修复总是可用的。

作用

检查文档字符串中过度缩进的节。

为什么这不好?

此规则强制实施具有多个节的文档字符串的统一风格。

多行文档字符串通常由摘要行、空行以及一系列节组成,每个节都有一个节标题和一个节正文。 约定是所有节都应使用一致的缩进。 在每个节中,标题应与文档字符串的开头引号的缩进匹配,正文应再缩进一级。

当使用 numpygoogle 约定启用此规则,当使用 pep257 约定禁用此规则。

示例

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

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

    Returns:
        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.

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

    Returns:
        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 (选项)

参考