用于根据PEP257自动检查文档字符串样式的工具

时间:2012-01-04 09:51:27

标签: python static-analysis docstring pep8 pep

pep8这样的工具可以检查源代码样式,但是它们不会根据pep257pep287检查文档字符串是否已被冻结。有没有这样的工具?

更新

我决定自己实现这样一个静态分析工具,参见:

https://github.com/GreenSteam/pep257

目前,大多数pep257都已被覆盖。 设计受提到的pep8工具的影响很大。

2 个答案:

答案 0 :(得分:13)

我不知道任何用于python doc字符串的静态分析工具。我开始使用PyLint之后不久就开始构建一个,但很快就放弃了。

PyLint有一个插件系统,如果你想把工作放在PEPs的可执行文件中,你就可以使用doc string插件。

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))

我记得这个系统没有很好的文档(过去一年可能已经改变了)。这至少可以让你开始黑客攻击/非常简单的代码代替文档。

答案 1 :(得分:0)

我认为它不会对任何PEP进行验证,但Epydoc会检查docstrmap中所有引用的参数和对象是否为有效参数和对象。