eric6/Plugins/CheckerPlugins/CodeStyleChecker/Naming/NamingStyleChecker.py

branch
maintenance
changeset 8043
0acf98cd089a
parent 7924
8a96736d465e
parent 7988
c4c17121eff8
child 8273
698ae46f40a4
equal deleted inserted replaced
7991:866adc8c315b 8043:0acf98cd089a
9 9
10 import collections 10 import collections
11 import ast 11 import ast
12 import re 12 import re
13 import os 13 import os
14 import sys
15 14
16 try: 15 try:
17 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__ 16 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__
18 except AttributeError: 17 except AttributeError:
19 ast.AsyncFunctionDef = ast.FunctionDef 18 ast.AsyncFunctionDef = ast.FunctionDef
92 def __visitTree(self, node): 91 def __visitTree(self, node):
93 """ 92 """
94 Private method to scan the given AST tree. 93 Private method to scan the given AST tree.
95 94
96 @param node AST tree node to scan 95 @param node AST tree node to scan
97 @return tuple giving line number, offset within line, code and 96 @yield tuple giving line number, offset within line and error code
98 checker function 97 @ytype tuple of (int, int, str)
99 """ 98 """
100 for error in self.__visitNode(node): 99 for error in self.__visitNode(node):
101 yield error 100 yield error
102 self.__parents.append(node) 101 self.__parents.append(node)
103 for child in ast.iter_child_nodes(node): 102 for child in ast.iter_child_nodes(node):
108 def __visitNode(self, node): 107 def __visitNode(self, node):
109 """ 108 """
110 Private method to inspect the given AST node. 109 Private method to inspect the given AST node.
111 110
112 @param node AST tree node to inspect 111 @param node AST tree node to inspect
113 @return tuple giving line number, offset within line, code and 112 @yield tuple giving line number, offset within line and error code
114 checker function 113 @ytype tuple of (int, int, str)
115 """ 114 """
116 if isinstance(node, ast.ClassDef): 115 if isinstance(node, ast.ClassDef):
117 self.__tagClassFunctions(node) 116 self.__tagClassFunctions(node)
118 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): 117 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
119 self.__findGlobalDefs(node) 118 self.__findGlobalDefs(node)
228 """ 227 """
229 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).
230 229
231 @param node AST note to check 230 @param node AST note to check
232 @param parents list of parent nodes 231 @param parents list of parent nodes
233 @return tuple giving line number, offset within line and error code 232 @yield tuple giving line number, offset within line and error code
234 (integer, integer, string) 233 @ytype tuple of (int, int, str)
235 """ 234 """
236 if isinstance(node, (ast.ClassDef, ast.FunctionDef, 235 if isinstance(node, (ast.ClassDef, ast.FunctionDef,
237 ast.AsyncFunctionDef)): 236 ast.AsyncFunctionDef)):
238 name = node.name 237 name = node.name
239 if self.__isNameToBeAvoided(name): 238 if self.__isNameToBeAvoided(name):
277 Almost without exception, class names use the CapWords convention. 276 Almost without exception, class names use the CapWords convention.
278 Classes for internal use have a leading underscore in addition. 277 Classes for internal use have a leading underscore in addition.
279 278
280 @param node AST note to check 279 @param node AST note to check
281 @param parents list of parent nodes 280 @param parents list of parent nodes
282 @return tuple giving line number, offset within line and error code 281 @yield tuple giving line number, offset within line and error code
283 (integer, integer, string) 282 @ytype tuple of (int, int, str)
284 """ 283 """
285 if not self.CamelcaseRegexp.match(node.name): 284 if not self.CamelcaseRegexp.match(node.name):
286 yield self.__error(node, "N801") 285 yield self.__error(node, "N801")
287 286
288 def __checkFunctionName(self, node, parents): 287 def __checkFunctionName(self, node, parents):
296 only in contexts where that's already the prevailing style 295 only in contexts where that's already the prevailing style
297 (e.g. threading.py), to retain backwards compatibility. 296 (e.g. threading.py), to retain backwards compatibility.
298 297
299 @param node AST note to check 298 @param node AST note to check
300 @param parents list of parent nodes 299 @param parents list of parent nodes
301 @return tuple giving line number, offset within line and error code 300 @yield tuple giving line number, offset within line and error code
302 (integer, integer, string) 301 @ytype tuple of (int, int, str)
303 """ 302 """
304 functionType = getattr(node, "function_type", "function") 303 functionType = getattr(node, "function_type", "function")
305 name = node.name 304 name = node.name
306 if ( 305 if (
307 (functionType == "function" and "__" in (name[:2], name[-2:])) or 306 (functionType == "function" and "__" in (name[:2], name[-2:])) or
318 separated by underscores. A class method should have 'cls' as the 317 separated by underscores. A class method should have 'cls' as the
319 first argument. A method should have 'self' as the first argument. 318 first argument. A method should have 'self' as the first argument.
320 319
321 @param node AST note to check 320 @param node AST note to check
322 @param parents list of parent nodes 321 @param parents list of parent nodes
323 @return tuple giving line number, offset within line and error code 322 @yield tuple giving line number, offset within line and error code
324 (integer, integer, string) 323 @ytype tuple of (int, int, str)
325 """ 324 """
326 if node.args.kwarg is not None: 325 if node.args.kwarg is not None:
327 if sys.version_info >= (3, 4): 326 kwarg = node.args.kwarg.arg
328 kwarg = node.args.kwarg.arg
329 else:
330 kwarg = node.args.kwarg
331 if not self.LowercaseRegex.match(kwarg): 327 if not self.LowercaseRegex.match(kwarg):
332 yield self.__error(node, "N803") 328 yield self.__error(node, "N803")
333 return 329 return
334 330
335 if node.args.vararg is not None: 331 if node.args.vararg is not None:
336 if sys.version_info >= (3, 4): 332 vararg = node.args.vararg.arg
337 vararg = node.args.vararg.arg
338 else:
339 vararg = node.args.vararg
340 if not self.LowercaseRegex.match(vararg): 333 if not self.LowercaseRegex.match(vararg):
341 yield self.__error(node, "N803") 334 yield self.__error(node, "N803")
342 return 335 return
343 336
344 argNames = self.__getArgNames(node) 337 argNames = self.__getArgNames(node)
371 364
372 Local variables in functions should be lowercase. 365 Local variables in functions should be lowercase.
373 366
374 @param node AST note to check 367 @param node AST note to check
375 @param parents list of parent nodes 368 @param parents list of parent nodes
376 @return tuple giving line number, offset within line and error code 369 @yield tuple giving line number, offset within line and error code
377 (integer, integer, string) 370 @ytype tuple of (int, int, str)
378 """ 371 """
379 for parentFunc in reversed(parents): 372 for parentFunc in reversed(parents):
380 if isinstance(parentFunc, ast.ClassDef): 373 if isinstance(parentFunc, ast.ClassDef):
381 return 374 return
382 if isinstance(parentFunc, (ast.FunctionDef, ast.AsyncFunctionDef)): 375 if isinstance(parentFunc, (ast.FunctionDef, ast.AsyncFunctionDef)):
397 390
398 Module and package names should be lowercase. 391 Module and package names should be lowercase.
399 392
400 @param node AST note to check 393 @param node AST note to check
401 @param parents list of parent nodes 394 @param parents list of parent nodes
402 @return tuple giving line number, offset within line and error code 395 @yield tuple giving line number, offset within line and error code
403 (integer, integer, string) 396 @ytype tuple of (int, int, str)
404 """ 397 """
405 if self.__filename: 398 if self.__filename:
406 moduleName = os.path.splitext(os.path.basename(self.__filename))[0] 399 moduleName = os.path.splitext(os.path.basename(self.__filename))[0]
407 if moduleName.lower() != moduleName: 400 if moduleName.lower() != moduleName:
408 yield self.__error(node, "N807") 401 yield self.__error(node, "N807")
420 Private method to check that imports don't change the 413 Private method to check that imports don't change the
421 naming convention (N811, N812, N813, N814). 414 naming convention (N811, N812, N813, N814).
422 415
423 @param node AST note to check 416 @param node AST note to check
424 @param parents list of parent nodes 417 @param parents list of parent nodes
425 @return tuple giving line number, offset within line and error code 418 @yield tuple giving line number, offset within line and error code
426 (integer, integer, string) 419 @ytype tuple of (int, int, str)
427 """ 420 """
428 for name in node.names: 421 for name in node.names:
429 if not name.asname: 422 if not name.asname:
430 continue 423 continue
431 424

eric ide

mercurial