Plugins/CheckerPlugins/Pep8/Pep257Checker.py

changeset 2945
0005e6314aea
parent 2944
dcca9f5c701d
child 2948
ea04689ee599
equal deleted inserted replaced
2944:dcca9f5c701d 2945:0005e6314aea
111 "D131", "D132", "D133", "D134", 111 "D131", "D132", "D133", "D134",
112 "D141", "D142", "D143", "D144", "D145", 112 "D141", "D142", "D143", "D144", "D145",
113 113
114 "D203", "D205", 114 "D203", "D205",
115 "D221", "D222", 115 "D221", "D222",
116 "D231", "D234", "D235", "D236", "D237", "D238", 116 "D231", "D234", "D235", "D236", "D237", "D238", "D239",
117 "D242", "D243", "D244", "D245", "D246", "D247", 117 "D242", "D243", "D244", "D245", "D246", "D247",
118 ] 118 ]
119 119
120 Messages = { 120 Messages = {
121 "D101": QT_TRANSLATE_NOOP( 121 "D101": QT_TRANSLATE_NOOP(
179 "Pep257Checker", 179 "Pep257Checker",
180 "trailing quotes of docstring not on separate line"), 180 "trailing quotes of docstring not on separate line"),
181 "D231": QT_TRANSLATE_NOOP( 181 "D231": QT_TRANSLATE_NOOP(
182 "Pep257Checker", "docstring summary does not end with a period"), 182 "Pep257Checker", "docstring summary does not end with a period"),
183 "D234": QT_TRANSLATE_NOOP( 183 "D234": QT_TRANSLATE_NOOP(
184 "Pep257Checker", "docstring does not contain a @return line"), 184 "Pep257Checker",
185 "docstring does not contain a @return line but returns something"),
185 "D235": QT_TRANSLATE_NOOP( 186 "D235": QT_TRANSLATE_NOOP(
186 "Pep257Checker", 187 "Pep257Checker",
188 "docstring contains a @return line but doesn't return anything"),
189 "D236": QT_TRANSLATE_NOOP(
190 "Pep257Checker",
187 "docstring does not contain enough @param/@keyparam lines"), 191 "docstring does not contain enough @param/@keyparam lines"),
188 "D236": QT_TRANSLATE_NOOP( 192 "D237": QT_TRANSLATE_NOOP(
189 "Pep257Checker", 193 "Pep257Checker",
190 "docstring contains too many @param/@keyparam lines"), 194 "docstring contains too many @param/@keyparam lines"),
191 "D237": QT_TRANSLATE_NOOP( 195 "D238": QT_TRANSLATE_NOOP(
192 "Pep257Checker", 196 "Pep257Checker",
193 "keyword only arguments must be documented with @keyparam lines"), 197 "keyword only arguments must be documented with @keyparam lines"),
194 "D238": QT_TRANSLATE_NOOP( 198 "D239": QT_TRANSLATE_NOOP(
195 "Pep257Checker", "order of @param/@keyparam lines does" 199 "Pep257Checker", "order of @param/@keyparam lines does"
196 " not match the function/method signature"), 200 " not match the function/method signature"),
197 "D242": QT_TRANSLATE_NOOP( 201 "D242": QT_TRANSLATE_NOOP(
198 "Pep257Checker", "class docstring is preceded by a blank line"), 202 "Pep257Checker", "class docstring is preceded by a blank line"),
199 "D243": QT_TRANSLATE_NOOP( 203 "D243": QT_TRANSLATE_NOOP(
305 ], 309 ],
306 "defDocstring": [ 310 "defDocstring": [
307 (self.__checkFunctionDocstring, ("D102", "D203")), 311 (self.__checkFunctionDocstring, ("D102", "D203")),
308 (self.__checkImperativeMood, ("D132",)), 312 (self.__checkImperativeMood, ("D132",)),
309 (self.__checkNoSignature, ("D133",)), 313 (self.__checkNoSignature, ("D133",)),
310 (self.__checkEricReturn, ("D234",)), 314 (self.__checkEricReturn, ("D234", "D235")),
311 (self.__checkEricFunctionArguments, 315 (self.__checkEricFunctionArguments,
312 ("D235", "D236", "D237", "D238")), 316 ("D236", "D237", "D238", "D239")),
313 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction, 317 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction,
314 ("D244", "D245")), 318 ("D244", "D245")),
315 ], 319 ],
316 "docstring": [ 320 "docstring": [
317 (self.__checkTripleDoubleQuotes, ("D111",)), 321 (self.__checkTripleDoubleQuotes, ("D111",)),
1099 docstringContext.start() + lineNumber + len(summaryLines) - 1, 1103 docstringContext.start() + lineNumber + len(summaryLines) - 1,
1100 0, "D231") 1104 0, "D231")
1101 1105
1102 def __checkEricReturn(self, docstringContext, context): 1106 def __checkEricReturn(self, docstringContext, context):
1103 """ 1107 """
1104 Private method to check, that docstrings contain an @return line. 1108 Private method to check, that docstrings contain an @return line
1109 if they return anything and don't otherwise.
1105 1110
1106 @param docstringContext docstring context (Pep257Context) 1111 @param docstringContext docstring context (Pep257Context)
1107 @param context context of the docstring (Pep257Context) 1112 @param context context of the docstring (Pep257Context)
1108 """ 1113 """
1109 if docstringContext is None or self.__isScript: 1114 if docstringContext is None or self.__isScript:
1110 return 1115 return
1111 1116
1117 tokens = list(
1118 tokenize.generate_tokens(StringIO(context.ssource()).readline))
1119 return_ = [tokens[i + 1][0] for i, token in enumerate(tokens)
1120 if token[1] == "return"]
1112 if "@return" not in docstringContext.ssource(): 1121 if "@return" not in docstringContext.ssource():
1113 tokens = list(
1114 tokenize.generate_tokens(StringIO(context.ssource()).readline))
1115 return_ = [tokens[i + 1][0] for i, token in enumerate(tokens)
1116 if token[1] == "return"]
1117 if (set(return_) - 1122 if (set(return_) -
1118 set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) != 1123 set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) !=
1119 set([])): 1124 set([])):
1120 self.__error(docstringContext.end(), 0, "D234") 1125 self.__error(docstringContext.end(), 0, "D234")
1126 else:
1127 if (set(return_) -
1128 set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) ==
1129 set([])):
1130 self.__error(docstringContext.end(), 0, "D235")
1121 1131
1122 def __checkEricFunctionArguments(self, docstringContext, context): 1132 def __checkEricFunctionArguments(self, docstringContext, context):
1123 """ 1133 """
1124 Private method to check, that docstrings contain an @param line 1134 Private method to check, that docstrings contain an @param line
1125 for each argument. 1135 for each argument.
1144 argNames.remove("cls") 1154 argNames.remove("cls")
1145 1155
1146 docstring = docstringContext.ssource() 1156 docstring = docstringContext.ssource()
1147 if (docstring.count("@param") + docstring.count("@keyparam") < 1157 if (docstring.count("@param") + docstring.count("@keyparam") <
1148 len(argNames + kwNames)): 1158 len(argNames + kwNames)):
1149 self.__error(docstringContext.end(), 0, "D235") 1159 self.__error(docstringContext.end(), 0, "D236")
1150 elif (docstring.count("@param") + docstring.count("@keyparam") > 1160 elif (docstring.count("@param") + docstring.count("@keyparam") >
1151 len(argNames + kwNames)): 1161 len(argNames + kwNames)):
1152 self.__error(docstringContext.end(), 0, "D236") 1162 self.__error(docstringContext.end(), 0, "D237")
1153 else: 1163 else:
1154 # extract @param and @keyparam from docstring 1164 # extract @param and @keyparam from docstring
1155 args = [] 1165 args = []
1156 kwargs = [] 1166 kwargs = []
1157 for line in docstringContext.source(): 1167 for line in docstringContext.source():
1162 args.append(name) 1172 args.append(name)
1163 1173
1164 # do the checks 1174 # do the checks
1165 for name in kwNames: 1175 for name in kwNames:
1166 if name not in kwargs: 1176 if name not in kwargs:
1167 self.__error(docstringContext.end(), 0, "D237") 1177 self.__error(docstringContext.end(), 0, "D238")
1168 return 1178 return
1169 if argNames + kwNames != args: 1179 if argNames + kwNames != args:
1170 self.__error(docstringContext.end(), 0, "D238") 1180 self.__error(docstringContext.end(), 0, "D239")
1171 1181
1172 def __checkEricBlankAfterSummary(self, docstringContext, context): 1182 def __checkEricBlankAfterSummary(self, docstringContext, context):
1173 """ 1183 """
1174 Private method to check, that docstring summaries are followed 1184 Private method to check, that docstring summaries are followed
1175 by a blank line. 1185 by a blank line.
1184 if len(docstrings) <= 3: 1194 if len(docstrings) <= 3:
1185 # correct/invalid one-liner 1195 # correct/invalid one-liner
1186 return 1196 return
1187 1197
1188 summaryLines, lineNumber = self.__getSummaryLines(docstringContext) 1198 summaryLines, lineNumber = self.__getSummaryLines(docstringContext)
1189 if len(docstrings) > lineNumber + len(summaryLines) - 1: 1199 if len(docstrings) - 2 > lineNumber + len(summaryLines) - 1:
1190 if docstrings[lineNumber + len(summaryLines)].strip(): 1200 if docstrings[lineNumber + len(summaryLines)].strip():
1191 self.__error(docstringContext.start() + lineNumber, 0, "D246") 1201 self.__error(docstringContext.start() + lineNumber, 0, "D246")
1192 1202
1193 def __checkEricNoBlankBeforeAndAfterClassOrFunction( 1203 def __checkEricNoBlankBeforeAndAfterClassOrFunction(
1194 self, docstringContext, context): 1204 self, docstringContext, context):

eric ide

mercurial