跳到内容

missing-new-line-after-section-name (D406)

源自 pydocstyle 代码检查器。

修复总是可用的。

作用

检查文档字符串中的节标题后是否跟有非换行符字符。

为什么这不好?

此规则强制执行多行 numpy 样式文档字符串的一致风格。

多行 numpy 样式文档字符串通常由摘要行、空行和一系列节组成。每个节都有一个节标题和一个节主体。节标题后应跟一个换行符,而不是其他字符(如冒号)。

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

示例

# The `Parameters`, `Returns` and `Raises` section headers are all followed
# by a colon in this function's docstring:
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 (选项)

参考