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

Vla*_*hev 20 python static-analysis docstring pep8 pep

pep8这样的工具可以查看源代码样式,但是它们不会根据pep257,pep287来检查docstrings是否已经过滤.有这样的工具吗?

更新

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

https://github.com/GreenSteam/pep257

现在,大多数pep257都被覆盖了.设计受到提到的pep8工具的严重影响.

Fin*_*inn 13

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

PyLint有一个插件系统和一个doc字符串插件是可行的,如果你想把工作放在一起使PEP可执行.

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))
Run Code Online (Sandbox Code Playgroud)

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