91 def __visitTree(self, node): |
91 def __visitTree(self, node): |
92 """ |
92 """ |
93 Private method to scan the given AST tree. |
93 Private method to scan the given AST tree. |
94 |
94 |
95 @param node AST tree node to scan |
95 @param node AST tree node to scan |
96 @return tuple giving line number, offset within line, code and |
96 @yield tuple giving line number, offset within line and error code |
97 checker function |
97 @ytype tuple of (int, int, str) |
98 """ |
98 """ |
99 for error in self.__visitNode(node): |
99 for error in self.__visitNode(node): |
100 yield error |
100 yield error |
101 self.__parents.append(node) |
101 self.__parents.append(node) |
102 for child in ast.iter_child_nodes(node): |
102 for child in ast.iter_child_nodes(node): |
107 def __visitNode(self, node): |
107 def __visitNode(self, node): |
108 """ |
108 """ |
109 Private method to inspect the given AST node. |
109 Private method to inspect the given AST node. |
110 |
110 |
111 @param node AST tree node to inspect |
111 @param node AST tree node to inspect |
112 @return tuple giving line number, offset within line, code and |
112 @yield tuple giving line number, offset within line and error code |
113 checker function |
113 @ytype tuple of (int, int, str) |
114 """ |
114 """ |
115 if isinstance(node, ast.ClassDef): |
115 if isinstance(node, ast.ClassDef): |
116 self.__tagClassFunctions(node) |
116 self.__tagClassFunctions(node) |
117 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
117 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
118 self.__findGlobalDefs(node) |
118 self.__findGlobalDefs(node) |
227 """ |
227 """ |
228 Private class to check the given node for a name to be avoided (N831). |
228 Private class to check the given node for a name to be avoided (N831). |
229 |
229 |
230 @param node AST note to check |
230 @param node AST note to check |
231 @param parents list of parent nodes |
231 @param parents list of parent nodes |
232 @return tuple giving line number, offset within line and error code |
232 @yield tuple giving line number, offset within line and error code |
233 (integer, integer, string) |
233 @ytype tuple of (int, int, str) |
234 """ |
234 """ |
235 if isinstance(node, (ast.ClassDef, ast.FunctionDef, |
235 if isinstance(node, (ast.ClassDef, ast.FunctionDef, |
236 ast.AsyncFunctionDef)): |
236 ast.AsyncFunctionDef)): |
237 name = node.name |
237 name = node.name |
238 if self.__isNameToBeAvoided(name): |
238 if self.__isNameToBeAvoided(name): |
276 Almost without exception, class names use the CapWords convention. |
276 Almost without exception, class names use the CapWords convention. |
277 Classes for internal use have a leading underscore in addition. |
277 Classes for internal use have a leading underscore in addition. |
278 |
278 |
279 @param node AST note to check |
279 @param node AST note to check |
280 @param parents list of parent nodes |
280 @param parents list of parent nodes |
281 @return tuple giving line number, offset within line and error code |
281 @yield tuple giving line number, offset within line and error code |
282 (integer, integer, string) |
282 @ytype tuple of (int, int, str) |
283 """ |
283 """ |
284 if not self.CamelcaseRegexp.match(node.name): |
284 if not self.CamelcaseRegexp.match(node.name): |
285 yield self.__error(node, "N801") |
285 yield self.__error(node, "N801") |
286 |
286 |
287 def __checkFunctionName(self, node, parents): |
287 def __checkFunctionName(self, node, parents): |
295 only in contexts where that's already the prevailing style |
295 only in contexts where that's already the prevailing style |
296 (e.g. threading.py), to retain backwards compatibility. |
296 (e.g. threading.py), to retain backwards compatibility. |
297 |
297 |
298 @param node AST note to check |
298 @param node AST note to check |
299 @param parents list of parent nodes |
299 @param parents list of parent nodes |
300 @return tuple giving line number, offset within line and error code |
300 @yield tuple giving line number, offset within line and error code |
301 (integer, integer, string) |
301 @ytype tuple of (int, int, str) |
302 """ |
302 """ |
303 functionType = getattr(node, "function_type", "function") |
303 functionType = getattr(node, "function_type", "function") |
304 name = node.name |
304 name = node.name |
305 if ( |
305 if ( |
306 (functionType == "function" and "__" in (name[:2], name[-2:])) or |
306 (functionType == "function" and "__" in (name[:2], name[-2:])) or |
317 separated by underscores. A class method should have 'cls' as the |
317 separated by underscores. A class method should have 'cls' as the |
318 first argument. A method should have 'self' as the first argument. |
318 first argument. A method should have 'self' as the first argument. |
319 |
319 |
320 @param node AST note to check |
320 @param node AST note to check |
321 @param parents list of parent nodes |
321 @param parents list of parent nodes |
322 @return tuple giving line number, offset within line and error code |
322 @yield tuple giving line number, offset within line and error code |
323 (integer, integer, string) |
323 @ytype tuple of (int, int, str) |
324 """ |
324 """ |
325 if node.args.kwarg is not None: |
325 if node.args.kwarg is not None: |
326 kwarg = node.args.kwarg.arg |
326 kwarg = node.args.kwarg.arg |
327 if not self.LowercaseRegex.match(kwarg): |
327 if not self.LowercaseRegex.match(kwarg): |
328 yield self.__error(node, "N803") |
328 yield self.__error(node, "N803") |
364 |
364 |
365 Local variables in functions should be lowercase. |
365 Local variables in functions should be lowercase. |
366 |
366 |
367 @param node AST note to check |
367 @param node AST note to check |
368 @param parents list of parent nodes |
368 @param parents list of parent nodes |
369 @return tuple giving line number, offset within line and error code |
369 @yield tuple giving line number, offset within line and error code |
370 (integer, integer, string) |
370 @ytype tuple of (int, int, str) |
371 """ |
371 """ |
372 for parentFunc in reversed(parents): |
372 for parentFunc in reversed(parents): |
373 if isinstance(parentFunc, ast.ClassDef): |
373 if isinstance(parentFunc, ast.ClassDef): |
374 return |
374 return |
375 if isinstance(parentFunc, (ast.FunctionDef, ast.AsyncFunctionDef)): |
375 if isinstance(parentFunc, (ast.FunctionDef, ast.AsyncFunctionDef)): |
390 |
390 |
391 Module and package names should be lowercase. |
391 Module and package names should be lowercase. |
392 |
392 |
393 @param node AST note to check |
393 @param node AST note to check |
394 @param parents list of parent nodes |
394 @param parents list of parent nodes |
395 @return tuple giving line number, offset within line and error code |
395 @yield tuple giving line number, offset within line and error code |
396 (integer, integer, string) |
396 @ytype tuple of (int, int, str) |
397 """ |
397 """ |
398 if self.__filename: |
398 if self.__filename: |
399 moduleName = os.path.splitext(os.path.basename(self.__filename))[0] |
399 moduleName = os.path.splitext(os.path.basename(self.__filename))[0] |
400 if moduleName.lower() != moduleName: |
400 if moduleName.lower() != moduleName: |
401 yield self.__error(node, "N807") |
401 yield self.__error(node, "N807") |
413 Private method to check that imports don't change the |
413 Private method to check that imports don't change the |
414 naming convention (N811, N812, N813, N814). |
414 naming convention (N811, N812, N813, N814). |
415 |
415 |
416 @param node AST note to check |
416 @param node AST note to check |
417 @param parents list of parent nodes |
417 @param parents list of parent nodes |
418 @return tuple giving line number, offset within line and error code |
418 @yield tuple giving line number, offset within line and error code |
419 (integer, integer, string) |
419 @ytype tuple of (int, int, str) |
420 """ |
420 """ |
421 for name in node.names: |
421 for name in node.names: |
422 if not name.asname: |
422 if not name.asname: |
423 continue |
423 continue |
424 |
424 |