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

branch
maintenance
changeset 8043
0acf98cd089a
parent 7924
8a96736d465e
parent 7998
cd41c844862f
child 8176
31965986ecd1
equal deleted inserted replaced
7991:866adc8c315b 8043:0acf98cd089a
126 "D111", "D112", 126 "D111", "D112",
127 "D121", "D122", 127 "D121", "D122",
128 "D130", "D131", "D132", "D133", "D134", 128 "D130", "D131", "D132", "D133", "D134",
129 "D141", "D142", "D143", "D144", "D145", 129 "D141", "D142", "D143", "D144", "D145",
130 130
131 "D203", "D205", "D206", 131 "D201", "D202.1", "D202.2", "D203", "D205", "D206",
132 "D221", "D222", 132 "D221", "D222",
133 "D231", "D232", "D234", "D235", "D236", "D237", "D238", "D239", 133 "D231", "D232", "D234r", "D234y", "D235r", "D235y", "D236", "D237",
134 "D238", "D239",
134 "D242", "D243", "D244", "D245", "D246", "D247", 135 "D242", "D243", "D244", "D245", "D246", "D247",
135 "D250", "D251", "D252", "D253", 136 "D250", "D251", "D252", "D253",
136 "D260", "D261", "D262", "D263", 137 "D260", "D261", "D262", "D263",
137 138
138 "D901", 139 "D901",
148 @param select list of selected codes (list of string) 149 @param select list of selected codes (list of string)
149 @param ignore list of codes to be ignored (list of string) 150 @param ignore list of codes to be ignored (list of string)
150 @param expected list of expected codes (list of string) 151 @param expected list of expected codes (list of string)
151 @param repeat flag indicating to report each occurrence of a code 152 @param repeat flag indicating to report each occurrence of a code
152 (boolean) 153 (boolean)
153 @keyparam maxLineLength allowed line length (integer) 154 @param maxLineLength allowed line length (integer)
154 @keyparam docType type of the documentation strings 155 @param docType type of the documentation strings
155 (string, one of 'eric' or 'pep257') 156 (string, one of 'eric' or 'pep257')
156 """ 157 """
157 self.__select = tuple(select) 158 self.__select = tuple(select)
158 self.__ignore = ('',) if select else tuple(ignore) 159 self.__ignore = ('',) if select else tuple(ignore)
159 self.__expected = expected[:] 160 self.__expected = expected[:]
213 ], 214 ],
214 } 215 }
215 elif self.__docType == "eric": 216 elif self.__docType == "eric":
216 checkersWithCodes = { 217 checkersWithCodes = {
217 "moduleDocstring": [ 218 "moduleDocstring": [
218 (self.__checkModulesDocstrings, ("D101",)), 219 (self.__checkModulesDocstrings, ("D101", "D201")),
219 ], 220 ],
220 "functionDocstring": [ 221 "functionDocstring": [
221 ], 222 ],
222 "classDocstring": [ 223 "classDocstring": [
223 (self.__checkClassDocstring, ("D104", "D205", "D206")), 224 (self.__checkClassDocstring, ("D104", "D205", "D206")),
227 ], 228 ],
228 "methodDocstring": [ 229 "methodDocstring": [
229 (self.__checkEricSummary, ("D232")), 230 (self.__checkEricSummary, ("D232")),
230 ], 231 ],
231 "defDocstring": [ 232 "defDocstring": [
232 (self.__checkFunctionDocstring, ("D102", "D203")), 233 (self.__checkFunctionDocstring,
234 ("D102", "D202.1", "D202.2", "D203")),
233 (self.__checkImperativeMood, ("D132",)), 235 (self.__checkImperativeMood, ("D132",)),
234 (self.__checkNoSignature, ("D133",)), 236 (self.__checkNoSignature, ("D133",)),
235 (self.__checkEricReturn, ("D234", "D235")), 237 (self.__checkEricReturn, ("D234r", "D235r")),
238 (self.__checkEricYield, ("D234y", "D235y")),
236 (self.__checkEricFunctionArguments, 239 (self.__checkEricFunctionArguments,
237 ("D236", "D237", "D238", "D239")), 240 ("D236", "D237", "D238", "D239")),
238 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction, 241 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction,
239 ("D244", "D245")), 242 ("D244", "D245")),
240 (self.__checkEricException, 243 (self.__checkEricException,
437 and one for keyword arguments (tuple of list of string) 440 and one for keyword arguments (tuple of list of string)
438 """ 441 """
439 arguments = [] 442 arguments = []
440 arguments.extend([arg.arg for arg in node.args.args]) 443 arguments.extend([arg.arg for arg in node.args.args])
441 if node.args.vararg is not None: 444 if node.args.vararg is not None:
442 if sys.version_info < (3, 4, 0): 445 arguments.append(node.args.vararg.arg)
443 arguments.append(node.args.vararg)
444 else:
445 arguments.append(node.args.vararg.arg)
446 446
447 kwarguments = [] 447 kwarguments = []
448 kwarguments.extend([arg.arg for arg in node.args.kwonlyargs]) 448 kwarguments.extend([arg.arg for arg in node.args.kwonlyargs])
449 if node.args.kwarg is not None: 449 if node.args.kwarg is not None:
450 if sys.version_info < (3, 4, 0): 450 kwarguments.append(node.args.kwarg.arg)
451 kwarguments.append(node.args.kwarg)
452 else:
453 kwarguments.append(node.args.kwarg.arg)
454 return arguments, kwarguments 451 return arguments, kwarguments
455 452
456 ################################################################## 453 ##################################################################
457 ## Parsing functionality below 454 ## Parsing functionality below
458 ################################################################## 455 ##################################################################
697 docstring = docstringContext.ssource() 694 docstring = docstringContext.ssource()
698 if (not docstring or not docstring.strip() or 695 if (not docstring or not docstring.strip() or
699 not docstring.strip('\'"')): 696 not docstring.strip('\'"')):
700 self.__error(context.start(), 0, code) 697 self.__error(context.start(), 0, code)
701 698
702 if ( 699 if self.__docType == "eric":
703 self.__docType == "eric" and 700 if (
704 docstring.strip('\'"').strip() == 701 docstring.strip('\'"').strip() ==
705 "Function documentation goes here." 702 "Function documentation goes here."
706 ): 703 ):
707 self.__error(docstringContext.end(), 0, "D202") 704 self.__error(docstringContext.end(), 0, "D202.1")
708 return 705 return
706
707 if (
708 "DESCRIPTION" in docstring or
709 "TYPE" in docstring
710 ):
711 self.__error(docstringContext.end(), 0, "D202.2")
712 return
709 713
710 def __checkClassDocstring(self, docstringContext, context): 714 def __checkClassDocstring(self, docstringContext, context):
711 """ 715 """
712 Private method to check, that all public functions and methods 716 Private method to check, that all public functions and methods
713 have a docstring. 717 have a docstring.
1073 return 1077 return
1074 1078
1075 tokens = list( 1079 tokens = list(
1076 tokenize.generate_tokens(StringIO(context.ssource()).readline)) 1080 tokenize.generate_tokens(StringIO(context.ssource()).readline))
1077 return_ = [tokens[i + 1][0] for i, token in enumerate(tokens) 1081 return_ = [tokens[i + 1][0] for i, token in enumerate(tokens)
1078 if token[1] in ("return", "yield")] 1082 if token[1] == "return"]
1079 if "@return" not in docstringContext.ssource(): 1083 if "@return" not in docstringContext.ssource():
1080 if (set(return_) - 1084 if (set(return_) -
1081 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} != 1085 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} !=
1082 set()): 1086 set()):
1083 self.__error(docstringContext.end(), 0, "D234") 1087 self.__error(docstringContext.end(), 0, "D234r")
1084 else: 1088 else:
1085 if (set(return_) - 1089 if (set(return_) -
1086 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} == 1090 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} ==
1087 set()): 1091 set()):
1088 self.__error(docstringContext.end(), 0, "D235") 1092 self.__error(docstringContext.end(), 0, "D235r")
1093
1094 def __checkEricYield(self, docstringContext, context):
1095 """
1096 Private method to check, that docstrings contain an &#64;yield line
1097 if they return anything and don't otherwise.
1098
1099 @param docstringContext docstring context (DocStyleContext)
1100 @param context context of the docstring (DocStyleContext)
1101 """
1102 if docstringContext is None:
1103 return
1104
1105 tokens = list(
1106 tokenize.generate_tokens(StringIO(context.ssource()).readline))
1107 yield_ = [tokens[i + 1][0] for i, token in enumerate(tokens)
1108 if token[1] == "yield"]
1109 if "@yield" not in docstringContext.ssource():
1110 if (set(yield_) -
1111 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} !=
1112 set()):
1113 self.__error(docstringContext.end(), 0, "D234y")
1114 else:
1115 if (set(yield_) -
1116 {tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE} ==
1117 set()):
1118 self.__error(docstringContext.end(), 0, "D235y")
1089 1119
1090 def __checkEricFunctionArguments(self, docstringContext, context): 1120 def __checkEricFunctionArguments(self, docstringContext, context):
1091 """ 1121 """
1092 Private method to check, that docstrings contain an &#64;param and/or 1122 Private method to check, that docstrings contain an &#64;param and/or
1093 &#64;keyparam line for each argument. 1123 &#64;keyparam line for each argument.

eric ide

mercurial