src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Naming/NamingStyleChecker.py

branch
eric7
changeset 10437
2f70ca07f0af
parent 10069
435cc5875135
child 10439
21c28b0f9e41
equal deleted inserted replaced
10436:f6881d10e995 10437:2f70ca07f0af
48 def __init__(self, tree, filename, options): 48 def __init__(self, tree, filename, options):
49 """ 49 """
50 Constructor (according to 'extended' pycodestyle.py API) 50 Constructor (according to 'extended' pycodestyle.py API)
51 51
52 @param tree AST tree of the source file 52 @param tree AST tree of the source file
53 @param filename name of the source file (string) 53 @type ast.AST
54 @param filename name of the source file
55 @type str
54 @param options options as parsed by pycodestyle.StyleGuide 56 @param options options as parsed by pycodestyle.StyleGuide
57 @type optparse.Option
55 """ 58 """
56 self.__parents = collections.deque() 59 self.__parents = collections.deque()
57 self.__tree = tree 60 self.__tree = tree
58 self.__filename = filename 61 self.__filename = filename
59 62
108 """ 111 """
109 Public method run by the pycodestyle.py checker. 112 Public method run by the pycodestyle.py checker.
110 113
111 @return tuple giving line number, offset within line, code and 114 @return tuple giving line number, offset within line, code and
112 checker function 115 checker function
116 @rtype tuple of (int, int, str, function)
113 """ 117 """
114 if self.__tree and self.__checkers: 118 if self.__tree and self.__checkers:
115 return self.__visitTree(self.__tree) 119 return self.__visitTree(self.__tree)
116 else: 120 else:
117 return () 121 return ()
119 def __visitTree(self, node): 123 def __visitTree(self, node):
120 """ 124 """
121 Private method to scan the given AST tree. 125 Private method to scan the given AST tree.
122 126
123 @param node AST tree node to scan 127 @param node AST tree node to scan
128 @type ast.AST
124 @yield tuple giving line number, offset within line and error code 129 @yield tuple giving line number, offset within line and error code
125 @ytype tuple of (int, int, str) 130 @ytype tuple of (int, int, str)
126 """ 131 """
127 yield from self.__visitNode(node) 132 yield from self.__visitNode(node)
128 self.__parents.append(node) 133 self.__parents.append(node)
133 def __visitNode(self, node): 138 def __visitNode(self, node):
134 """ 139 """
135 Private method to inspect the given AST node. 140 Private method to inspect the given AST node.
136 141
137 @param node AST tree node to inspect 142 @param node AST tree node to inspect
143 @type ast.AST
138 @yield tuple giving line number, offset within line and error code 144 @yield tuple giving line number, offset within line and error code
139 @ytype tuple of (int, int, str) 145 @ytype tuple of (int, int, str)
140 """ 146 """
141 if isinstance(node, ast.ClassDef): 147 if isinstance(node, ast.ClassDef):
142 self.__tagClassFunctions(node) 148 self.__tagClassFunctions(node)
153 """ 159 """
154 Private method to tag functions if they are methods, class methods or 160 Private method to tag functions if they are methods, class methods or
155 static methods. 161 static methods.
156 162
157 @param classNode AST tree node to tag 163 @param classNode AST tree node to tag
164 @type ast.ClassDef
158 """ 165 """
159 # try to find all 'old style decorators' 166 # try to find all 'old style decorators'
160 # like m = staticmethod(m) 167 # like m = staticmethod(m)
161 lateDecoration = {} 168 lateDecoration = {}
162 for node in ast.iter_child_nodes(classNode): 169 for node in ast.iter_child_nodes(classNode):
196 def __findGlobalDefs(self, functionNode): 203 def __findGlobalDefs(self, functionNode):
197 """ 204 """
198 Private method amend a node with global definitions information. 205 Private method amend a node with global definitions information.
199 206
200 @param functionNode AST tree node to amend 207 @param functionNode AST tree node to amend
208 @type ast.FunctionDef or ast.AsyncFunctionDef
201 """ 209 """
202 globalNames = set() 210 globalNames = set()
203 nodesToCheck = collections.deque(ast.iter_child_nodes(functionNode)) 211 nodesToCheck = collections.deque(ast.iter_child_nodes(functionNode))
204 while nodesToCheck: 212 while nodesToCheck:
205 node = nodesToCheck.pop() 213 node = nodesToCheck.pop()
215 def __getArgNames(self, node): 223 def __getArgNames(self, node):
216 """ 224 """
217 Private method to get the argument names of a function node. 225 Private method to get the argument names of a function node.
218 226
219 @param node AST node to extract arguments names from 227 @param node AST node to extract arguments names from
220 @return list of argument names (list of string) 228 @type ast.FunctionDef or ast.AsyncFunctionDef
229 @return list of argument names
230 @rtype list of str
221 """ 231 """
222 posArgs = [arg.arg for arg in node.args.args] 232 posArgs = [arg.arg for arg in node.args.args]
223 kwOnly = [arg.arg for arg in node.args.kwonlyargs] 233 kwOnly = [arg.arg for arg in node.args.kwonlyargs]
224 return posArgs + kwOnly 234 return posArgs + kwOnly
225 235
226 def __error(self, node, code): 236 def __error(self, node, code):
227 """ 237 """
228 Private method to build the error information. 238 Private method to build the error information.
229 239
230 @param node AST node to report an error for 240 @param node AST node to report an error for
231 @param code error code to report (string) 241 @type ast.AST
242 @param code error code to report
243 @type str
232 @return tuple giving line number, offset within line and error code 244 @return tuple giving line number, offset within line and error code
233 (integer, integer, string) 245 @rtype tuple of (int, int, str)
234 """ 246 """
235 if isinstance(node, ast.Module): 247 if isinstance(node, ast.Module):
236 lineno = 0 248 lineno = 0
237 offset = 0 249 offset = 0
238 else: 250 else:
248 260
249 def __isNameToBeAvoided(self, name): 261 def __isNameToBeAvoided(self, name):
250 """ 262 """
251 Private method to check, if the given name should be avoided. 263 Private method to check, if the given name should be avoided.
252 264
253 @param name name to be checked (string) 265 @param name name to be checked
254 @return flag indicating to avoid it (boolen) 266 @type str
267 @return flag indicating to avoid it
268 @rtype bool
255 """ 269 """
256 return name in ("l", "O", "I") 270 return name in ("l", "O", "I")
257 271
258 def __checkNameToBeAvoided(self, node, parents): # noqa: U100 272 def __checkNameToBeAvoided(self, node, parents): # noqa: U100
259 """ 273 """
260 Private class to check the given node for a name to be avoided (N831). 274 Private class to check the given node for a name to be avoided (N831).
261 275
262 @param node AST note to check 276 @param node AST note to check
263 @param parents list of parent nodes 277 @type ast.Ast
278 @param parents list of parent nodes
279 @type list of ast.AST
264 @yield tuple giving line number, offset within line and error code 280 @yield tuple giving line number, offset within line and error code
265 @ytype tuple of (int, int, str) 281 @ytype tuple of (int, int, str)
266 """ 282 """
267 if isinstance(node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)): 283 if isinstance(node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
268 name = node.name 284 name = node.name
298 Private method to extract the class definition. 314 Private method to extract the class definition.
299 315
300 @param name name of the class 316 @param name name of the class
301 @type str 317 @type str
302 @param parents list of parent nodes 318 @param parents list of parent nodes
303 @type ast.AST 319 @type list of ast.AST
304 @return node containing the class definition 320 @return node containing the class definition
305 @rtype ast.ClassDef 321 @rtype ast.ClassDef
306 """ 322 """
307 for parent in parents: 323 for parent in parents:
308 for node in parent.body: 324 for node in parent.body:
316 Private method to extract the names of all super classes. 332 Private method to extract the names of all super classes.
317 333
318 @param name name of the class 334 @param name name of the class
319 @type str 335 @type str
320 @param parents list of parent nodes 336 @param parents list of parent nodes
321 @type ast.AST 337 @type list of ast.AST
322 @param names set of collected class names (defaults to None) 338 @param names set of collected class names (defaults to None)
323 @type set of str (optional) 339 @type set of str (optional)
324 @return set of class names 340 @return set of class names
325 @rtype set of str 341 @rtype set of str
326 """ 342 """
345 361
346 Almost without exception, class names use the CapWords convention. 362 Almost without exception, class names use the CapWords convention.
347 Classes for internal use have a leading underscore in addition. 363 Classes for internal use have a leading underscore in addition.
348 364
349 @param node AST note to check 365 @param node AST note to check
350 @param parents list of parent nodes 366 @type ast.ClassDef
367 @param parents list of parent nodes
368 @type list of ast.AST
351 @yield tuple giving line number, offset within line and error code 369 @yield tuple giving line number, offset within line and error code
352 @ytype tuple of (int, int, str) 370 @ytype tuple of (int, int, str)
353 """ 371 """
354 name = node.name 372 name = node.name
355 strippedName = name.strip("_") 373 strippedName = name.strip("_")
370 methods '__' in front and back are not allowed. Mixed case is allowed 388 methods '__' in front and back are not allowed. Mixed case is allowed
371 only in contexts where that's already the prevailing style 389 only in contexts where that's already the prevailing style
372 (e.g. threading.py), to retain backwards compatibility. 390 (e.g. threading.py), to retain backwards compatibility.
373 391
374 @param node AST note to check 392 @param node AST note to check
375 @param parents list of parent nodes 393 @type ast.FunctionDef or ast.AsynFunctionDef
394 @param parents list of parent nodes
395 @type list of ast.AST
376 @yield tuple giving line number, offset within line and error code 396 @yield tuple giving line number, offset within line and error code
377 @ytype tuple of (int, int, str) 397 @ytype tuple of (int, int, str)
378 """ 398 """
379 functionType = getattr(node, "function_type", "function") 399 functionType = getattr(node, "function_type", "function")
380 name = node.name 400 name = node.name
395 The argument names of a function should be lowercase, with words 415 The argument names of a function should be lowercase, with words
396 separated by underscores. A class method should have 'cls' as the 416 separated by underscores. A class method should have 'cls' as the
397 first argument. A method should have 'self' as the first argument. 417 first argument. A method should have 'self' as the first argument.
398 418
399 @param node AST note to check 419 @param node AST note to check
400 @param parents list of parent nodes 420 @type ast.FunctionDef or ast.AsynFunctionDef
421 @param parents list of parent nodes
422 @type list of ast.AST
401 @yield tuple giving line number, offset within line and error code 423 @yield tuple giving line number, offset within line and error code
402 @ytype tuple of (int, int, str) 424 @ytype tuple of (int, int, str)
403 """ 425 """
404 if node.args.kwarg is not None: 426 if node.args.kwarg is not None:
405 kwarg = node.args.kwarg.arg 427 kwarg = node.args.kwarg.arg
438 (N821, N822, N823). 460 (N821, N822, N823).
439 461
440 Local variables in functions should be lowercase. 462 Local variables in functions should be lowercase.
441 463
442 @param node AST note to check 464 @param node AST note to check
443 @param parents list of parent nodes 465 @type ast.AST
466 @param parents list of parent nodes
467 @type list of ast.AST
444 @yield tuple giving line number, offset within line and error code 468 @yield tuple giving line number, offset within line and error code
445 @ytype tuple of (int, int, str) 469 @ytype tuple of (int, int, str)
446 """ 470 """
447 nodeType = type(node) 471 nodeType = type(node)
448 if nodeType is ast.Assign: 472 if nodeType is ast.Assign:
599 """ 623 """
600 Private method to check module naming conventions (N807, N808). 624 Private method to check module naming conventions (N807, N808).
601 625
602 Module and package names should be lowercase. 626 Module and package names should be lowercase.
603 627
604 @param node AST note to check 628 @param node AST node to check
605 @param parents list of parent nodes 629 @type ast.AST
630 @param parents list of parent nodes
631 @type list of ast.AST
606 @yield tuple giving line number, offset within line and error code 632 @yield tuple giving line number, offset within line and error code
607 @ytype tuple of (int, int, str) 633 @ytype tuple of (int, int, str)
608 """ 634 """
609 if self.__filename: 635 if self.__filename:
610 moduleName = os.path.splitext(os.path.basename(self.__filename))[0] 636 moduleName = os.path.splitext(os.path.basename(self.__filename))[0]
620 def __checkImportAs(self, node, parents): # noqa: U100 646 def __checkImportAs(self, node, parents): # noqa: U100
621 """ 647 """
622 Private method to check that imports don't change the 648 Private method to check that imports don't change the
623 naming convention (N811, N812, N813, N814, N815). 649 naming convention (N811, N812, N813, N814, N815).
624 650
625 @param node AST note to check 651 @param node AST node to check
626 @param parents list of parent nodes 652 @type ast.Import
653 @param parents list of parent nodes
654 @type list of ast.AST
627 @yield tuple giving line number, offset within line and error code 655 @yield tuple giving line number, offset within line and error code
628 @ytype tuple of (int, int, str) 656 @ytype tuple of (int, int, str)
629 """ 657 """
630 for name in node.names: 658 for name in node.names:
631 asname = name.asname 659 asname = name.asname

eric ide

mercurial