--- a/UtilitiesPython2/Pep257CheckerPy2.py Sat Sep 28 16:46:46 2013 +0200 +++ b/UtilitiesPython2/Pep257CheckerPy2.py Sat Sep 28 17:24:00 2013 +0200 @@ -113,6 +113,7 @@ "D221", "D231", "D234", "D235", "D236", "D237", "D238", "D239", "D242", "D243", "D244", "D245", "D246", "D247", + "D250", "D251", ] def __init__(self, source, filename, select, ignore, expected, repeat, @@ -215,6 +216,7 @@ ("D236", "D237", "D238", "D239")), (self.__checkEricNoBlankBeforeAndAfterClassOrFunction, ("D244", "D245")), + (self.__checkEricException, ("D250", "D251")), ], "docstring": [ (self.__checkTripleDoubleQuotes, ("D111",)), @@ -340,13 +342,13 @@ lines = docstringContext.source() line0 = (lines[0] - .replace('r"""', "", 1) - .replace('u"""', "", 1) - .replace('"""', "") - .replace("r'''", "", 1) - .replace("u'''", "", 1) - .replace("'''", "") - .strip()) + .replace('r"""', "", 1) + .replace('u"""', "", 1) + .replace('"""', "") + .replace("r'''", "", 1) + .replace("u'''", "", 1) + .replace("'''", "") + .strip()) if len(lines) > 1: line1 = lines[1].strip().replace('"""', "").replace("'''", "") else: @@ -448,6 +450,7 @@ Private method to extract a docstring given `def` or `class` source. @param context context data to get the docstring from (Pep257Context) + @param what string denoting what is being parsed (string) @return context of extracted docstring (Pep257Context) """ moduleDocstring = self.__parseModuleDocstring(context.source()) @@ -985,7 +988,7 @@ def __checkEricReturn(self, docstringContext, context): """ - Private method to check, that docstrings contain an @return line + Private method to check, that docstrings contain an @return line if they return anything and don't otherwise. @param docstringContext docstring context (Pep257Context) @@ -1011,8 +1014,8 @@ def __checkEricFunctionArguments(self, docstringContext, context): """ - Private method to check, that docstrings contain an @param line - for each argument. + Private method to check, that docstrings contain an @param and/or + @keyparam line for each argument. @param docstringContext docstring context (Pep257Context) @param context context of the docstring (Pep257Context) @@ -1034,10 +1037,10 @@ argNames.remove("cls") docstring = docstringContext.ssource() - if (docstring.count("@param") + docstring.count("@keyparam") < + if (docstring.count("@param") + docstring.count("@keyparam") < len(argNames + kwNames)): self.__error(docstringContext.end(), 0, "D236") - elif (docstring.count("@param") + docstring.count("@keyparam") > + elif (docstring.count("@param") + docstring.count("@keyparam") > len(argNames + kwNames)): self.__error(docstringContext.end(), 0, "D237") else: @@ -1059,6 +1062,34 @@ if argNames + kwNames != args: self.__error(docstringContext.end(), 0, "D239") + def __checkEricException(self, docstringContext, context): + """ + Private method to check, that docstrings contain an @exception line + if they raise an exception and don't otherwise. + + @param docstringContext docstring context (Pep257Context) + @param context context of the docstring (Pep257Context) + """ + if docstringContext is None or self.__isScript: + return + + tokens = list( + tokenize.generate_tokens(StringIO(context.ssource()).readline)) + exception = [tokens[i + 1][0] for i, token in enumerate(tokens) + if token[1] == "raise"] + if "@exception" not in docstringContext.ssource() and \ + "@throws" not in docstringContext.ssource() and \ + "@raise" not in docstringContext.ssource(): + if (set(exception) - + set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) != + set([])): + self.__error(docstringContext.end(), 0, "D250") + else: + if (set(exception) - + set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) == + set([])): + self.__error(docstringContext.end(), 0, "D251") + def __checkEricBlankAfterSummary(self, docstringContext, context): """ Private method to check, that docstring summaries are followed