25 Codes = [ |
25 Codes = [ |
26 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808", |
26 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808", |
27 "N811", "N812", "N813", "N814", "N821", "N831" |
27 "N811", "N812", "N813", "N814", "N821", "N831" |
28 ] |
28 ] |
29 |
29 |
30 def __init__(self, tree, filename): |
30 def __init__(self, tree, filename, options): |
31 """ |
31 """ |
32 Constructor (according to pep8.py API) |
32 Constructor (according to 'extended' pep8.py API) |
33 |
33 |
34 @param tree AST tree of the source file |
34 @param tree AST tree of the source file |
35 @param filename name of the source file (string) |
35 @param filename name of the source file (string) |
|
36 @param options options as parsed by pep8.StyleGuide |
36 """ |
37 """ |
37 self.__parents = collections.deque() |
38 self.__parents = collections.deque() |
38 self.__tree = tree |
39 self.__tree = tree |
39 self.__filename = filename |
40 self.__filename = filename |
40 |
41 |
41 self.__checkers = { |
42 self.__checkersWithCodes = { |
42 "classdef": [self.__checkClassName], |
43 "classdef": [ |
43 "functiondef": [self.__checkFuntionName, |
44 (self.__checkClassName, ("N801",)), |
44 self.__checkFunctionArgumentNames, |
45 (self.__checkNameToBeAvoided, ("N831",)), |
45 ], |
46 ], |
46 "assign": [self.__checkVariablesInFunction], |
47 "functiondef": [ |
47 "importfrom": [self.__checkImportAs], |
48 (self.__checkFuntionName, ("N802",)), |
48 "module": [self.__checkModule], |
49 (self.__checkFunctionArgumentNames, |
|
50 ("N803", "N804", "N805", "N806")), |
|
51 (self.__checkNameToBeAvoided, ("N831",)), |
|
52 ], |
|
53 "assign": [ |
|
54 (self.__checkVariablesInFunction, ("N821",)), |
|
55 (self.__checkNameToBeAvoided, ("N831",)), |
|
56 ], |
|
57 "importfrom": [ |
|
58 (self.__checkImportAs, ("N811", "N812", "N813", "N814")), |
|
59 ], |
|
60 "module": [ |
|
61 (self.__checkModule, ("N807", "N808")), |
|
62 ], |
49 } |
63 } |
|
64 |
|
65 self.__checkers = {} |
|
66 for key, checkers in self.__checkersWithCodes.items(): |
|
67 for checker, codes in checkers: |
|
68 if any(not (code and options.ignore_code(code)) |
|
69 for code in codes): |
|
70 if key not in self.__checkers: |
|
71 self.__checkers[key] = [] |
|
72 self.__checkers[key].append(checker) |
50 |
73 |
51 def run(self): |
74 def run(self): |
52 """ |
75 """ |
53 Public method run by the pep8.py checker. |
76 Public method run by the pep8.py checker. |
54 |
77 |
205 @param name name to be checked (string) |
228 @param name name to be checked (string) |
206 @return flag indicating to avoid it (boolen) |
229 @return flag indicating to avoid it (boolen) |
207 """ |
230 """ |
208 return name in ("l", "O", "I") |
231 return name in ("l", "O", "I") |
209 |
232 |
|
233 def __checkNameToBeAvoided(self, node, parents): |
|
234 """ |
|
235 Private class to check the given node for a name to be avoided (N831). |
|
236 |
|
237 @param node AST note to check |
|
238 @return tuple giving line number, offset within line and error code |
|
239 (integer, integer, string) |
|
240 """ |
|
241 if isinstance(node, (ast.ClassDef, ast.FunctionDef)): |
|
242 name = node.name |
|
243 if self.__isNameToBeAvoided(name): |
|
244 yield self.__error(node, "N831") |
|
245 return |
|
246 |
|
247 if isinstance(node, ast.FunctionDef): |
|
248 argNames = self.__getArgNames(node) |
|
249 for arg in argNames: |
|
250 if self.__isNameToBeAvoided(arg): |
|
251 yield self.__error(node, "N831") |
|
252 return |
|
253 |
|
254 if isinstance(node, ast.Assign): |
|
255 for target in node.targets: |
|
256 name = isinstance(target, ast.Name) and target.id |
|
257 if not name: |
|
258 return |
|
259 |
|
260 if self.__isNameToBeAvoided(name): |
|
261 yield self.__error(node, "N831") |
|
262 return |
|
263 |
210 def __checkClassName(self, node, parents): |
264 def __checkClassName(self, node, parents): |
211 """ |
265 """ |
212 Private class to check the given node for class name |
266 Private class to check the given node for class name |
213 conventions (N801, N831). |
267 conventions (N801). |
214 |
268 |
215 Almost without exception, class names use the CapWords convention. |
269 Almost without exception, class names use the CapWords convention. |
216 Classes for internal use have a leading underscore in addition. |
270 Classes for internal use have a leading underscore in addition. |
217 |
271 |
218 @param node AST note to check |
272 @param node AST note to check |
219 @return tuple giving line number, offset within line and error code |
273 @return tuple giving line number, offset within line and error code |
220 (integer, integer, string) |
274 (integer, integer, string) |
221 """ |
275 """ |
222 if self.__isNameToBeAvoided(node.name): |
|
223 yield self.__error(node, "N831") |
|
224 return |
|
225 |
|
226 if not self.CamelcaseRegexp.match(node.name): |
276 if not self.CamelcaseRegexp.match(node.name): |
227 yield self.__error(node, "N801") |
277 yield self.__error(node, "N801") |
228 |
278 |
229 def __checkFuntionName(self, node, parents): |
279 def __checkFuntionName(self, node, parents): |
230 """ |
280 """ |
231 Private class to check the given node for function name |
281 Private class to check the given node for function name |
232 conventions (N802, N831). |
282 conventions (N802). |
233 |
283 |
234 Function names should be lowercase, with words separated by underscores |
284 Function names should be lowercase, with words separated by underscores |
235 as necessary to improve readability. Functions <b>not</b> being |
285 as necessary to improve readability. Functions <b>not</b> being |
236 methods '__' in front and back are not allowed. Mixed case is allowed |
286 methods '__' in front and back are not allowed. Mixed case is allowed |
237 only in contexts where that's already the prevailing style |
287 only in contexts where that's already the prevailing style |
241 @return tuple giving line number, offset within line and error code |
291 @return tuple giving line number, offset within line and error code |
242 (integer, integer, string) |
292 (integer, integer, string) |
243 """ |
293 """ |
244 functionType = getattr(node, "function_type", "function") |
294 functionType = getattr(node, "function_type", "function") |
245 name = node.name |
295 name = node.name |
246 if self.__isNameToBeAvoided(name): |
|
247 yield self.__error(node, "N831") |
|
248 return |
|
249 |
|
250 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \ |
296 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \ |
251 not self.LowercaseRegex.match(name): |
297 not self.LowercaseRegex.match(name): |
252 yield self.__error(node, "N802") |
298 yield self.__error(node, "N802") |
253 |
299 |
254 def __checkFunctionArgumentNames(self, node, parents): |
300 def __checkFunctionArgumentNames(self, node, parents): |
255 """ |
301 """ |
256 Private class to check the argument names of functions |
302 Private class to check the argument names of functions |
257 (N803, N804, N805, N806, N831). |
303 (N803, N804, N805, N806). |
258 |
304 |
259 The argument names of a function should be lowercase, with words |
305 The argument names of a function should be lowercase, with words |
260 separated by underscores. A class method should have 'cls' as the |
306 separated by underscores. A class method should have 'cls' as the |
261 first argument. A method should have 'self' as the first argument. |
307 first argument. A method should have 'self' as the first argument. |
262 |
308 |
292 yield self.__error(node, "N804") |
338 yield self.__error(node, "N804") |
293 elif functionType == "staticmethod": |
339 elif functionType == "staticmethod": |
294 if argNames[0] in ("cls", "self"): |
340 if argNames[0] in ("cls", "self"): |
295 yield self.__error(node, "N806") |
341 yield self.__error(node, "N806") |
296 for arg in argNames: |
342 for arg in argNames: |
297 if self.__isNameToBeAvoided(arg): |
|
298 yield self.__error(node, "N831") |
|
299 return |
|
300 |
|
301 if not self.LowercaseRegex.match(arg): |
343 if not self.LowercaseRegex.match(arg): |
302 yield self.__error(node, "N803") |
344 yield self.__error(node, "N803") |
303 return |
345 return |
304 |
346 |
305 def __checkVariablesInFunction(self, node, parents): |
347 def __checkVariablesInFunction(self, node, parents): |
306 """ |
348 """ |
307 Private method to check local variables in functions (N821, N831). |
349 Private method to check local variables in functions (N821). |
308 |
350 |
309 Local variables in functions should be lowercase. |
351 Local variables in functions should be lowercase. |
310 |
352 |
311 @param node AST note to check |
353 @param node AST note to check |
312 @return tuple giving line number, offset within line and error code |
354 @return tuple giving line number, offset within line and error code |
322 for target in node.targets: |
364 for target in node.targets: |
323 name = isinstance(target, ast.Name) and target.id |
365 name = isinstance(target, ast.Name) and target.id |
324 if not name or name in parentFunc.global_names: |
366 if not name or name in parentFunc.global_names: |
325 return |
367 return |
326 |
368 |
327 if self.__isNameToBeAvoided(name): |
|
328 yield self.__error(node, "N831") |
|
329 return |
|
330 |
|
331 if not self.LowercaseRegex.match(name) and name[:1] != '_': |
369 if not self.LowercaseRegex.match(name) and name[:1] != '_': |
332 yield self.__error(target, "N821") |
370 yield self.__error(target, "N821") |
333 |
371 |
334 def __checkModule(self, node, parents): |
372 def __checkModule(self, node, parents): |
335 """ |
373 """ |