跳到内容

未记录的公共方法 (D102)

源自 pydocstyle 代码检查器。

作用

检查未记录的公共方法定义。

为什么这不好?

公共方法应该通过文档字符串来记录,以概述其目的和行为。

通常,方法文档字符串应该描述方法的行为、参数、副作用、异常、返回值,以及任何可能与用户相关的其他信息。

如果代码库遵循方法文档字符串的标准格式,请遵循该格式以保持一致性。

示例

class Cat(Animal):
    def greet(self, happy: bool = True):
        if happy:
            print("Meow!")
        else:
            raise ValueError("Tried to greet an unhappy cat.")

改为使用(NumPy 文档字符串格式)

class Cat(Animal):
    def greet(self, happy: bool = True):
        """Print a greeting from the cat.

        Parameters
        ----------
        happy : bool, optional
            Whether the cat is happy, is True by default.

        Raises
        ------
        ValueError
            If the cat is not happy.
        """
        if happy:
            print("Meow!")
        else:
            raise ValueError("Tried to greet an unhappy cat.")

或(Google 文档字符串格式)

class Cat(Animal):
    def greet(self, happy: bool = True):
        """Print a greeting from the cat.

        Args:
            happy: Whether the cat is happy, is True by default.

        Raises:
            ValueError: If the cat is not happy.
        """
        if happy:
            print("Meow!")
        else:
            raise ValueError("Tried to greet an unhappy cat.")

Options (选项)

参考