eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyle/DocStyleChecker.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8176
31965986ecd1
parent 8259
2bbec88047dd
diff -r fb0ef164f536 -r 698ae46f40a4 eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyle/DocStyleChecker.py
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyle/DocStyleChecker.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyle/DocStyleChecker.py	Sat May 01 14:27:20 2021 +0200
@@ -14,8 +14,8 @@
 
 import tokenize
 import ast
-import sys
 from io import StringIO
+import contextlib
 
 try:
     ast.AsyncFunctionDef    # __IGNORE_EXCEPTION__
@@ -23,7 +23,7 @@
     ast.AsyncFunctionDef = ast.FunctionDef
 
 
-class DocStyleContext(object):
+class DocStyleContext:
     """
     Class implementing the source context.
     """
@@ -117,7 +117,7 @@
         return self.__special
 
 
-class DocStyleChecker(object):
+class DocStyleChecker:
     """
     Class implementing a checker for documentation string conventions.
     """
@@ -135,8 +135,6 @@
         "D242", "D243", "D244", "D245", "D246", "D247",
         "D250", "D251", "D252", "D253",
         "D260", "D261", "D262", "D263",
-        
-        "D901",
     ]
 
     def __init__(self, source, filename, select, ignore, expected, repeat,
@@ -307,20 +305,6 @@
                 }
             )
     
-    def __reportInvalidSyntax(self):
-        """
-        Private method to report a syntax error.
-        """
-        exc_type, exc = sys.exc_info()[:2]
-        if len(exc.args) > 1:
-            offset = exc.args[1]
-            if len(offset) > 2:
-                offset = offset[1:3]
-        else:
-            offset = (1, 0)
-        self.__error(offset[0] - 1, offset[1] or 0,
-                     'D901', exc_type.__name__, exc.args[0])
-    
     def __resetReadline(self):
         """
         Private method to reset the internal readline function.
@@ -351,12 +335,6 @@
             # don't do anything, if no codes were selected
             return
         
-        try:
-            ast.parse("".join(self.__source), self.__filename)
-        except (SyntaxError, TypeError):
-            self.__reportInvalidSyntax()
-            return
-        
         for keyword in self.__keywords:
             if keyword in self.__checkers:
                 for check in self.__checkers[keyword]:
@@ -405,14 +383,16 @@
                  .replace("u'''", "", 1)
                  .replace("'''", "")
                  .strip())
-        if len(lines) > 1:
-            line1 = lines[1].strip().replace('"""', "").replace("'''", "")
-        else:
-            line1 = ""
-        if len(lines) > 2:
-            line2 = lines[2].strip().replace('"""', "").replace("'''", "")
-        else:
-            line2 = ""
+        line1 = (
+            lines[1].strip().replace('"""', "").replace("'''", "")
+            if len(lines) > 1 else
+            ""
+        )
+        line2 = (
+            lines[2].strip().replace('"""', "").replace("'''", "")
+            if len(lines) > 2 else
+            ""
+        )
         if line0:
             lineno = 0
             summaries.append(line0)
@@ -487,7 +467,7 @@
         
         tokenGenerator = tokenize.generate_tokens(
             StringIO(context.ssource()).readline)
-        try:
+        with contextlib.suppress(StopIteration):
             kind = None
             while kind != tokenize.INDENT:
                 kind, _, _, _, _ = next(tokenGenerator)
@@ -495,8 +475,6 @@
             if kind == tokenize.STRING:  # STRING after INDENT is a docstring
                 return DocStyleContext(
                     value, context.start() + line - 1, "docstring")
-        except StopIteration:
-            pass
         
         return None
     
@@ -582,7 +560,7 @@
                 tokenGenerator = tokenize.generate_tokens(
                     StringIO(classContext.ssource()).readline)
                 kind, value, char = None, None, None
-                try:
+                with contextlib.suppress(StopIteration):
                     while True:
                         start, end = None, None
                         while not (kind == tokenize.NAME and value == 'def'):
@@ -611,8 +589,6 @@
                             ):
                                 context.setSpecial("classmethod")
                         contexts.append(context)
-                except StopIteration:
-                    pass
             self.__methodsCache = contexts
         
         return self.__methodsCache
@@ -819,10 +795,11 @@
             return
         
         indent = min(len(line) - len(line.strip()) for line in nonEmptyLines)
-        if context.contextType() == "module":
-            expectedIndent = 0
-        else:
-            expectedIndent = len(context.indent()) + 4
+        expectedIndent = (
+            0
+            if context.contextType() == "module" else
+            len(context.indent()) + 4
+        )
         if indent != expectedIndent:
             self.__error(docstringContext.start(), 0, "D122")
     
@@ -886,7 +863,7 @@
         summary, lineNumber = self.__getSummaryLine(docstringContext)
         if (
             functionName + "(" in summary.replace(" ", "") and
-            not functionName + "()" in summary.replace(" ", "")
+            functionName + "()" not in summary.replace(" ", "")
         ):
             # report only, if it is not an abbreviated form (i.e. function() )
             self.__error(docstringContext.start() + lineNumber, 0, "D133")
@@ -995,9 +972,11 @@
             return
         
         summary, lineNumber = self.__getSummaryLine(docstringContext)
-        if len(docstrings) > 2:
-            if docstrings[lineNumber + 1].strip():
-                self.__error(docstringContext.start() + lineNumber, 0, "D144")
+        if (
+            len(docstrings) > 2 and
+            docstrings[lineNumber + 1].strip()
+        ):
+            self.__error(docstringContext.start() + lineNumber, 0, "D144")
     
     def __checkBlankAfterLastParagraph(self, docstringContext, context):
         """
@@ -1057,7 +1036,7 @@
             if (
                 summary and
                 not summary.endswith(".") and
-                not summary.split(None, 1)[0].lower() == "constructor"
+                summary.split(None, 1)[0].lower() != "constructor"
             ):
                 self.__error(
                     docstringContext.start() + lineNumber +
@@ -1308,9 +1287,11 @@
             return
         
         summaryLines, lineNumber = self.__getSummaryLines(docstringContext)
-        if len(docstrings) - 2 > lineNumber + len(summaryLines) - 1:
-            if docstrings[lineNumber + len(summaryLines)].strip():
-                self.__error(docstringContext.start() + lineNumber, 0, "D246")
+        if (
+            len(docstrings) - 2 > lineNumber + len(summaryLines) - 1 and
+            docstrings[lineNumber + len(summaryLines)].strip()
+        ):
+            self.__error(docstringContext.start() + lineNumber, 0, "D246")
     
     def __checkEricNoBlankBeforeAndAfterClassOrFunction(
             self, docstringContext, context):

eric ide

mercurial