2012-01-04 23 views
19

Narzędzia takie jak pep8 mogą sprawdzać styl kodu źródłowego, ale nie sprawdzają, czy docstrukcje są pobierane zgodnie z pep257, pep287. Czy są takie narzędzia?Narzędzie do automatycznego sprawdzania stylu docplingu zgodnie z PEP257

Aktualizacja

I zdecydowała się na wdrożenie takiego narzędzia do analizy statycznej na własną rękę, patrz:

https://github.com/GreenSteam/pep257

Teraz większość pep257 jest objęty. Projekt był pod silnym wpływem wspomnianego narzędzia pep8.

Odpowiedz

13

Nie znam żadnego narzędzia do analizy statycznej dla ciągów docs doc. Zacząłem ją budować wkrótce po rozpoczęciu pracy z PyLint, ale szybko się poddałem.

PyLint ma system wtyczek i wtyczka do dokumentu jest wykonalna, jeśli chcesz umieścić pracę w celu wykonania plików PEP.

Wtyczki PyLint nazywane są kontrolerami i występują w dwóch formach: te, które pracują z plikiem źródłowym jako dokument tekstowy i osoby pracujące z nim jako AST. Podjąłem próbę wyjścia z AST. To może być błąd z perspektywy czasu.

Oto co miałem:

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

ile pamiętam ten system nie ma wielkich docs (które mogły ulec zmianie w ubiegłym roku). To daje przynajmniej coś do rozpoczęcia hackowania na/naprawdę prosty kod zamiast dokumentów.

0

Nie sądzę, że to sprawdzanie poprawności w stosunku do jakichkolwiek PEP, ale Epydoc sprawdzi, czy wszystkie przywołane parametry i obiekty w docstrmap do prawidłowych parametrów i obiektów.

Powiązane problemy