Plugins/CheckerPlugins/Pep8/Pep8NamingChecker.py

changeset 2910
cdc56e9d9f12
parent 2905
a1ae4b297bc0
child 2962
d6c9d1ca2da4
child 3056
9986ec0e559a
equal deleted inserted replaced
2909:c3fb85fe41f9 2910:cdc56e9d9f12
58 "variable in function should be lowercase"), 58 "variable in function should be lowercase"),
59 "N831": QT_TRANSLATE_NOOP("Pep8NamingChecker", 59 "N831": QT_TRANSLATE_NOOP("Pep8NamingChecker",
60 "names 'l', 'O' and 'I' should be avoided"), 60 "names 'l', 'O' and 'I' should be avoided"),
61 } 61 }
62 62
63 def __init__(self, tree, filename): 63 def __init__(self, tree, filename, options):
64 """ 64 """
65 Constructor (according to pep8.py API) 65 Constructor (according to 'extended' pep8.py API)
66 66
67 @param tree AST tree of the source file 67 @param tree AST tree of the source file
68 @param filename name of the source file (string) 68 @param filename name of the source file (string)
69 @param options options as parsed by pep8.StyleGuide
69 """ 70 """
70 self.__parents = collections.deque() 71 self.__parents = collections.deque()
71 self.__tree = tree 72 self.__tree = tree
72 self.__filename = filename 73 self.__filename = filename
73 74
74 self.__checkers = { 75 self.__checkersWithCodes = {
75 "classdef": [self.__checkClassName], 76 "classdef": [
76 "functiondef": [self.__checkFuntionName, 77 (self.__checkClassName, ("N801",)),
77 self.__checkFunctionArgumentNames, 78 (self.__checkNameToBeAvoided, ("N831",)),
78 ], 79 ],
79 "assign": [self.__checkVariablesInFunction], 80 "functiondef": [
80 "importfrom": [self.__checkImportAs], 81 (self.__checkFuntionName, ("N802",)),
81 "module": [self.__checkModule], 82 (self.__checkFunctionArgumentNames,
83 ("N803", "N804", "N805", "N806")),
84 (self.__checkNameToBeAvoided, ("N831",)),
85 ],
86 "assign": [
87 (self.__checkVariablesInFunction, ("N821",)),
88 (self.__checkNameToBeAvoided, ("N831",)),
89 ],
90 "importfrom": [
91 (self.__checkImportAs, ("N811", "N812", "N813", "N814")),
92 ],
93 "module": [
94 (self.__checkModule, ("N807", "N808")),
95 ],
82 } 96 }
97
98 self.__checkers = {}
99 for key, checkers in self.__checkersWithCodes.items():
100 for checker, codes in checkers:
101 if any(not (code and options.ignore_code(code))
102 for code in codes):
103 if key not in self.__checkers:
104 self.__checkers[key] = []
105 self.__checkers[key].append(checker)
83 106
84 def run(self): 107 def run(self):
85 """ 108 """
86 Public method run by the pep8.py checker. 109 Public method run by the pep8.py checker.
87 110
242 @param name name to be checked (string) 265 @param name name to be checked (string)
243 @return flag indicating to avoid it (boolen) 266 @return flag indicating to avoid it (boolen)
244 """ 267 """
245 return name in ("l", "O", "I") 268 return name in ("l", "O", "I")
246 269
270 def __checkNameToBeAvoided(self, node, parents):
271 """
272 Private class to check the given node for a name to be avoided (N831).
273
274 @param node AST note to check
275 @return tuple giving line number, offset within line and error code
276 (integer, integer, string)
277 """
278 if isinstance(node, (ast.ClassDef, ast.FunctionDef)):
279 name = node.name
280 if self.__isNameToBeAvoided(name):
281 yield self.__error(node, "N831")
282 return
283
284 if isinstance(node, ast.FunctionDef):
285 argNames = self.__getArgNames(node)
286 for arg in argNames:
287 if self.__isNameToBeAvoided(arg):
288 yield self.__error(node, "N831")
289 return
290
291 if isinstance(node, ast.Assign):
292 for target in node.targets:
293 name = isinstance(target, ast.Name) and target.id
294 if not name:
295 return
296
297 if self.__isNameToBeAvoided(name):
298 yield self.__error(node, "N831")
299 return
300
247 def __checkClassName(self, node, parents): 301 def __checkClassName(self, node, parents):
248 """ 302 """
249 Private class to check the given node for class name 303 Private class to check the given node for class name
250 conventions (N801, N831). 304 conventions (N801).
251 305
252 Almost without exception, class names use the CapWords convention. 306 Almost without exception, class names use the CapWords convention.
253 Classes for internal use have a leading underscore in addition. 307 Classes for internal use have a leading underscore in addition.
254 308
255 @param node AST note to check 309 @param node AST note to check
256 @return tuple giving line number, offset within line and error code 310 @return tuple giving line number, offset within line and error code
257 (integer, integer, string) 311 (integer, integer, string)
258 """ 312 """
259 if self.__isNameToBeAvoided(node.name):
260 yield self.__error(node, "N831")
261 return
262
263 if not self.CamelcaseRegexp.match(node.name): 313 if not self.CamelcaseRegexp.match(node.name):
264 yield self.__error(node, "N801") 314 yield self.__error(node, "N801")
265 315
266 def __checkFuntionName(self, node, parents): 316 def __checkFuntionName(self, node, parents):
267 """ 317 """
268 Private class to check the given node for function name 318 Private class to check the given node for function name
269 conventions (N802, N831). 319 conventions (N802).
270 320
271 Function names should be lowercase, with words separated by underscores 321 Function names should be lowercase, with words separated by underscores
272 as necessary to improve readability. Functions <b>not</b> being 322 as necessary to improve readability. Functions <b>not</b> being
273 methods '__' in front and back are not allowed. Mixed case is allowed 323 methods '__' in front and back are not allowed. Mixed case is allowed
274 only in contexts where that's already the prevailing style 324 only in contexts where that's already the prevailing style
278 @return tuple giving line number, offset within line and error code 328 @return tuple giving line number, offset within line and error code
279 (integer, integer, string) 329 (integer, integer, string)
280 """ 330 """
281 functionType = getattr(node, "function_type", "function") 331 functionType = getattr(node, "function_type", "function")
282 name = node.name 332 name = node.name
283 if self.__isNameToBeAvoided(name):
284 yield self.__error(node, "N831")
285 return
286
287 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \ 333 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \
288 not self.LowercaseRegex.match(name): 334 not self.LowercaseRegex.match(name):
289 yield self.__error(node, "N802") 335 yield self.__error(node, "N802")
290 336
291 def __checkFunctionArgumentNames(self, node, parents): 337 def __checkFunctionArgumentNames(self, node, parents):
292 """ 338 """
293 Private class to check the argument names of functions 339 Private class to check the argument names of functions
294 (N803, N804, N805, N806, N831). 340 (N803, N804, N805, N806).
295 341
296 The argument names of a function should be lowercase, with words 342 The argument names of a function should be lowercase, with words
297 separated by underscores. A class method should have 'cls' as the 343 separated by underscores. A class method should have 'cls' as the
298 first argument. A method should have 'self' as the first argument. 344 first argument. A method should have 'self' as the first argument.
299 345
329 yield self.__error(node, "N804") 375 yield self.__error(node, "N804")
330 elif functionType == "staticmethod": 376 elif functionType == "staticmethod":
331 if argNames[0] in ("cls", "self"): 377 if argNames[0] in ("cls", "self"):
332 yield self.__error(node, "N806") 378 yield self.__error(node, "N806")
333 for arg in argNames: 379 for arg in argNames:
334 if self.__isNameToBeAvoided(arg):
335 yield self.__error(node, "N831")
336 return
337
338 if not self.LowercaseRegex.match(arg): 380 if not self.LowercaseRegex.match(arg):
339 yield self.__error(node, "N803") 381 yield self.__error(node, "N803")
340 return 382 return
341 383
342 def __checkVariablesInFunction(self, node, parents): 384 def __checkVariablesInFunction(self, node, parents):
343 """ 385 """
344 Private method to check local variables in functions (N821, N831). 386 Private method to check local variables in functions (N821).
345 387
346 Local variables in functions should be lowercase. 388 Local variables in functions should be lowercase.
347 389
348 @param node AST note to check 390 @param node AST note to check
349 @return tuple giving line number, offset within line and error code 391 @return tuple giving line number, offset within line and error code
359 for target in node.targets: 401 for target in node.targets:
360 name = isinstance(target, ast.Name) and target.id 402 name = isinstance(target, ast.Name) and target.id
361 if not name or name in parentFunc.global_names: 403 if not name or name in parentFunc.global_names:
362 return 404 return
363 405
364 if self.__isNameToBeAvoided(name):
365 yield self.__error(node, "N831")
366 return
367
368 if not self.LowercaseRegex.match(name) and name[:1] != '_': 406 if not self.LowercaseRegex.match(name) and name[:1] != '_':
369 yield self.__error(target, "N821") 407 yield self.__error(target, "N821")
370 408
371 def __checkModule(self, node, parents): 409 def __checkModule(self, node, parents):
372 """ 410 """

eric ide

mercurial