自动检查文档字符串风格的工具,符合PEP257规范

21 投票
2 回答
4094 浏览
提问于 2025-04-17 09:31

pep8 这样的工具可以检查源代码的风格,但它们并不能检查文档字符串是否按照 pep257pep287 的格式来写。有没有这样的工具呢?

更新

我决定自己实现一个这样的静态分析工具,看看这里:

https://github.com/GreenSteam/pep257

现在,大部分的 pep257 内容都已经覆盖了。这个工具的设计受到了之前提到的 pep8 工具的很大影响。

2 个回答

0

我觉得这个并没有按照任何PEP(Python增强提案)来验证,不过Epydoc可以检查文档中的所有引用参数和对象,确保它们都是有效的参数和对象。

13

我不知道有没有专门用于分析Python文档字符串的工具。其实我在刚开始用PyLint的时候,就想自己做一个,但很快就放弃了。

PyLint有一个插件系统,如果你愿意花时间去做,可以开发一个文档字符串的插件,让PEP(Python增强提案)可以执行。

PyLint的“插件”叫做检查器,分为两种形式:一种是直接处理源文件的原始文本,另一种是处理源文件的抽象语法树(AST)。我当时是从AST入手的,事后看来这可能是个错误。

我当时的代码是这样的:

class DocStringChecker(BaseChecker):
    """
    PyLint AST based checker to eval compliance with PEP 257-ish conventions.
    """
    __implements__ = IASTNGChecker

    name = 'doc_string_checker'
    priority = -1
    msgs = {'W9001': ('One line doc string on >1 lines',
                     ('Used when a short doc string is on multiple lines')),
            'W9002': ('Doc string does not end with "." period',
                     ('Used when a doc string does not end with a period')),
            'W9003': ('Not all args mentioned in doc string',
                     ('Used when not all arguments are in the doc string ')),
            'W9004': ('triple quotes',
                     ('Used when doc string does not use """')),
           }
    options = ()

    def visit_function(self, node):
        if node.doc: self._check_doc_string(node)

    def visit_module(self, node):
        if node.doc: self._check_doc_string(node)

    def visit_class(self, node):
        if node.doc: self._check_doc_string(node)

    def _check_doc_string(self, node):
        self.one_line_one_one_line(node)
        self.has_period(node)
        self.all_args_in_doc(node)

    def one_line_one_one_line(self,node):
        """One line docs (len < 80) are on one line"""
        doc = node.doc
        if len(doc) > 80: return True
        elif sum(doc.find(nl) for nl in ('\n', '\r', '\n\r')) == -3: return True
        else:
            self.add_message('W9001', node=node, line=node.tolineno)

    def has_period(self,node):
        """Doc ends in a period"""
        if not node.doc.strip().endswith('.'):
            self.add_message('W9002', node=node, line=node.tolineno)

    def all_args_in_doc(self,node):
        """All function arguments are mentioned in doc"""
        if not hasattr(node, 'argnames'): return True
        for arg in node.argnames:
            if arg != 'self' and arg in node.doc: continue
            else: break
        else: return True
        self.add_message('W9003', node=node, line=node.tolineno)

    def triple_quotes(self,node): #This would need a raw checker to work b/c the AST doesn't use """
        """Doc string uses tripple quotes"""
        doc = node.doc.strip()
        if doc.endswith('"""') and doc.startswith('"""'): return True
        else: self.add_message('W9004', node=node, line=node.tolineno)


def register(linter):
    """required method to auto register this checker"""
    linter.register_checker(DocStringChecker(linter))

我记得这个系统的文档不是很好(可能在过去一年里有所改善)。不过至少给你提供了一些可以开始动手的简单代码,替代文档。

撰写回答