2012-01-04 38 views
19

Các công cụ như pep8 có thể kiểm tra kiểu mã nguồn, nhưng chúng không kiểm tra xem docstrings có được định dạng theo pep257, pep287 hay không. Có những công cụ như vậy không?Công cụ để tự động kiểm tra kiểu chuỗi ký tự theo PEP257

Cập nhật

tôi quyết định thực hiện như một công cụ phân tích tĩnh một mình, xem:

https://github.com/GreenSteam/pep257

Ngay bây giờ, hầu hết các pep257 được bảo hiểm. Thiết kế chịu ảnh hưởng nặng nề từ công cụ pep8 được đề cập.

Trả lời

13

Tôi không biết bất kỳ công cụ phân tích tĩnh nào cho chuỗi tài liệu python. Tôi thực sự bắt đầu xây dựng một thời gian ngắn sau khi bắt đầu với PyLint nhưng nhanh chóng bỏ cuộc.

PyLint có hệ thống plugin và plugin chuỗi doc có thể thực hiện được nếu bạn muốn đặt công việc vào để làm cho PEP thực thi.

PyLint "plugins" được gọi là bộ kiểm tra và có hai dạng: những người làm việc với tệp nguồn dưới dạng tài liệu văn bản thô và những người làm việc với tư cách là AST. Tôi đã cố gắng bắt đầu từ AST. Đây có thể là một sai lầm khi nhìn lại.

Dưới đây là những gì tôi có:

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

Như tôi nhớ lại hệ thống này không có tài liệu rất lớn (có thể đã thay đổi trong năm qua). Điều này ít nhất là cung cấp cho bạn một cái gì đó để bắt đầu hack trên/code thực sự đơn giản thay cho tài liệu.

0

Tôi không nghĩ rằng nó xác thực đối với bất kỳ PEP nào, nhưng Epydoc sẽ kiểm tra xem tất cả tham số và đối tượng được tham chiếu trong docstrmap có thông số và đối tượng hợp lệ không.

Các vấn đề liên quan