跳到内容

带文档字符串的重载 (D418)

源自 pydocstyle 代码检查器。

作用

检查包含文档字符串的 @overload 函数定义。

为什么这不好?

@overload 装饰器用于为给定的函数定义多个兼容的签名,以支持类型检查。一系列 @overload 定义之后应跟一个包含函数实现的非装饰定义。

@overload 函数定义不应包含文档字符串;相反,文档字符串应放在包含实现的非装饰定义上。

示例

from typing import overload


@overload
def factorial(n: int) -> int:
    """Return the factorial of n."""


@overload
def factorial(n: float) -> float:
    """Return the factorial of n."""


def factorial(n):
    """Return the factorial of n."""


factorial.__doc__  # "Return the factorial of n."

建议改为

from typing import overload


@overload
def factorial(n: int) -> int: ...


@overload
def factorial(n: float) -> float: ...


def factorial(n):
    """Return the factorial of n."""


factorial.__doc__  # "Return the factorial of n."

参考