Plugins/CheckerPlugins/CodeStyleChecker/NamingStyleChecker.py

Wed, 08 Mar 2017 10:49:20 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 08 Mar 2017 10:49:20 +0100
changeset 5589
ad8f37c6a306
parent 5585
dab20c39f08c
child 5597
3d88d53f8c2b
permissions
-rw-r--r--

Fixed code style checker.

2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
5389
9b1c800daff3 Updated copyright for 2017.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5147
diff changeset
3 # Copyright (c) 2013 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
2984
031cceaa8b01 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2982
diff changeset
7 Module implementing a checker for naming conventions.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import collections
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import ast
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import re
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import os
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
14 import sys
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
16 try:
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
17 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
18 except AttributeError:
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
19 ast.AsyncFunctionDef = ast.FunctionDef
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
20
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
22 class NamingStyleChecker(object):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
2984
031cceaa8b01 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2982
diff changeset
24 Class implementing a checker for naming conventions.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 LowercaseRegex = re.compile(r"[_a-z][_a-z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 UppercaseRegexp = re.compile(r"[_A-Z][_A-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 CamelcaseRegexp = re.compile(r"_?[A-Z][a-zA-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 MixedcaseRegexp = re.compile(r"_?[a-z][a-zA-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Codes = [
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808",
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 "N811", "N812", "N813", "N814", "N821", "N831"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 ]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
36 def __init__(self, tree, filename, options):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
5147
d39dd5cee0c8 Renamed pep8.py to pycodestyle.py in order to track the upstream renaming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
38 Constructor (according to 'extended' pycodestyle.py API)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 @param tree AST tree of the source file
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 @param filename name of the source file (string)
5147
d39dd5cee0c8 Renamed pep8.py to pycodestyle.py in order to track the upstream renaming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
42 @param options options as parsed by pycodestyle.StyleGuide
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__parents = collections.deque()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__tree = tree
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.__filename = filename
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
48 self.__checkersWithCodes = {
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
49 "classdef": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
50 (self.__checkClassName, ("N801",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
51 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
52 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
53 "functiondef": [
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
54 (self.__checkFunctionName, ("N802",)),
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
55 (self.__checkFunctionArgumentNames,
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
56 ("N803", "N804", "N805", "N806")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
57 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
58 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
59 "assign": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
60 (self.__checkVariablesInFunction, ("N821",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
61 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
62 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
63 "importfrom": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
64 (self.__checkImportAs, ("N811", "N812", "N813", "N814")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
65 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
66 "module": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
67 (self.__checkModule, ("N807", "N808")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
68 ],
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 }
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
70
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
71 self.__checkers = {}
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
72 for key, checkers in self.__checkersWithCodes.items():
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
73 for checker, codes in checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
74 if any(not (code and options.ignore_code(code))
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
75 for code in codes):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
76 if key not in self.__checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
77 self.__checkers[key] = []
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
78 self.__checkers[key].append(checker)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def run(self):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
5147
d39dd5cee0c8 Renamed pep8.py to pycodestyle.py in order to track the upstream renaming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
82 Public method run by the pycodestyle.py checker.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
2975
fb336783a649 A little optimization to the naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2962
diff changeset
87 if self.__tree and self.__checkers:
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 return self.__visitTree(self.__tree)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 return ()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 def __visitTree(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 Private method to scan the given AST tree.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 @param node AST tree node to scan
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 for error in self.__visitNode(node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.__parents.append(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 for child in ast.iter_child_nodes(node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 for error in self.__visitTree(child):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.__parents.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 def __visitNode(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 Private method to inspect the given AST node.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 @param node AST tree node to inspect
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 self.__tagClassFunctions(node)
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
118 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 self.__findGlobalDefs(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 checkerName = node.__class__.__name__.lower()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 if checkerName in self.__checkers:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 for checker in self.__checkers[checkerName]:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 for error in checker(node, self.__parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 yield error + (self.__checkers[checkerName],)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 def __tagClassFunctions(self, classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 Private method to tag functions if they are methods, class methods or
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 static methods.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 @param classNode AST tree node to tag
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 # try to find all 'old style decorators' like
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 # m = staticmethod(m)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 lateDecoration = {}
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 for node in ast.iter_child_nodes(classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 if not (isinstance(node, ast.Assign) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 isinstance(node.value, ast.Call) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 isinstance(node.value.func, ast.Name)):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 funcName = node.value.func.id
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 if funcName in ("classmethod", "staticmethod"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 meth = (len(node.value.args) == 1 and node.value.args[0])
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 if isinstance(meth, ast.Name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 lateDecoration[meth.id] = funcName
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 # iterate over all functions and tag them
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 for node in ast.iter_child_nodes(classNode):
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
150 if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 node.function_type = 'method'
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 if node.name == "__new__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 node.function_type = "classmethod"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 if node.name in lateDecoration:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 node.function_type = lateDecoration[node.name]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 elif node.decorator_list:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 names = [d.id for d in node.decorator_list
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 if isinstance(d, ast.Name) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 d.id in ("classmethod", "staticmethod")]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 if names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 node.function_type = names[0]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 def __findGlobalDefs(self, functionNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 Private method amend a node with global definitions information.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 @param functionNode AST tree node to amend
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 globalNames = set()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 nodesToCheck = collections.deque(ast.iter_child_nodes(functionNode))
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 while nodesToCheck:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 node = nodesToCheck.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 if isinstance(node, ast.Global):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 globalNames.update(node.names)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
179 if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef,
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
180 ast.ClassDef)):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 nodesToCheck.extend(ast.iter_child_nodes(node))
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 functionNode.global_names = globalNames
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 def __getArgNames(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 Private method to get the argument names of a function node.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 @param node AST node to extract arguments names from
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @return list of argument names (list of string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
191 if sys.version_info[0] == 3:
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
192 posArgs = [arg.arg for arg in node.args.args]
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
193 kwOnly = [arg.arg for arg in node.args.kwonlyargs]
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
194 return posArgs + kwOnly
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
195 else:
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
196 def unpackArgs(args):
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
197 """
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
198 Local helper function to unpack function argument names.
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
199
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
200 @param args list of AST node arguments
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
201 @return list of argument names (list of string)
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
202 """
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
203 ret = []
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
204 for arg in args:
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
205 if isinstance(arg, ast.Tuple):
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
206 ret.extend(unpackArgs(arg.elts))
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
207 else:
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
208 ret.append(arg.id)
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
209 return ret
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
210
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
211 return unpackArgs(node.args.args)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 def __error(self, node, code):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
215 Private method to build the error information.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 @param node AST node to report an error for
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 @param code error code to report (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 if isinstance(node, ast.Module):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 lineno = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 offset = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 lineno = node.lineno
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 offset = node.col_offset
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 offset += 6
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
231 elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 offset += 4
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 return (lineno, offset, code)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def __isNameToBeAvoided(self, name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 Private method to check, if the given name should be avoided.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 @param name name to be checked (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 @return flag indicating to avoid it (boolen)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 return name in ("l", "O", "I")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
245 def __checkNameToBeAvoided(self, node, parents):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
246 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
247 Private class to check the given node for a name to be avoided (N831).
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
248
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
249 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
250 @param parents list of parent nodes
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
251 @return tuple giving line number, offset within line and error code
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
252 (integer, integer, string)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
253 """
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
254 if isinstance(node, (ast.ClassDef, ast.FunctionDef,
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
255 ast.AsyncFunctionDef)):
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
256 name = node.name
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
257 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
258 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
259 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
260
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
261 if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
262 argNames = self.__getArgNames(node)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
263 for arg in argNames:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
264 if self.__isNameToBeAvoided(arg):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
265 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
266 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
267
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
268 if isinstance(node, ast.Assign):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
269 for target in node.targets:
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
270 if isinstance(target, ast.Name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
271 name = target.id
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
272 if not name:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
273 return
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
274
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
275 if self.__isNameToBeAvoided(name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
276 yield self.__error(node, "N831")
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
277 return
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
278
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
279 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
280 for element in target.elts:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
281 if isinstance(element, ast.Name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
282 name = element.id
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
283 if not name:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
284 return
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
285
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
286 if self.__isNameToBeAvoided(name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
287 yield self.__error(node, "N831")
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
288 return
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
289
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 def __checkClassName(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 Private class to check the given node for class name
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
293 conventions (N801).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 Almost without exception, class names use the CapWords convention.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 Classes for internal use have a leading underscore in addition.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
299 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 if not self.CamelcaseRegexp.match(node.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 yield self.__error(node, "N801")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
306 def __checkFunctionName(self, node, parents):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 Private class to check the given node for function name
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
309 conventions (N802).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 Function names should be lowercase, with words separated by underscores
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 as necessary to improve readability. Functions <b>not</b> being
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 methods '__' in front and back are not allowed. Mixed case is allowed
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 only in contexts where that's already the prevailing style
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 (e.g. threading.py), to retain backwards compatibility.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
318 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 functionType = getattr(node, "function_type", "function")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 name = node.name
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 not self.LowercaseRegex.match(name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 yield self.__error(node, "N802")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 def __checkFunctionArgumentNames(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 Private class to check the argument names of functions
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
331 (N803, N804, N805, N806).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 The argument names of a function should be lowercase, with words
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 separated by underscores. A class method should have 'cls' as the
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 first argument. A method should have 'self' as the first argument.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
338 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 if node.args.kwarg is not None:
3646
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
343 if sys.version_info >= (3, 4):
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
344 kwarg = node.args.kwarg.arg
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
345 else:
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
346 kwarg = node.args.kwarg
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
347 if not self.LowercaseRegex.match(kwarg):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 if node.args.vararg is not None:
3646
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
352 if sys.version_info >= (3, 4):
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
353 vararg = node.args.vararg.arg
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
354 else:
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
355 vararg = node.args.vararg
cfbb47b6d885 Fixed an issue in the naming style checker caused by AST changes in Python 3.4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3456
diff changeset
356 if not self.LowercaseRegex.match(vararg):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 argNames = self.__getArgNames(node)
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
361 functionType = getattr(node, "function_type", "function")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
362
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 if not argNames:
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
364 if functionType == "method":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
365 yield self.__error(node, "N805")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
366 elif functionType == "classmethod":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
367 yield self.__error(node, "N804")
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 if functionType == "method":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 if argNames[0] != "self":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 yield self.__error(node, "N805")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 elif functionType == "classmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 if argNames[0] != "cls":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 yield self.__error(node, "N804")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 elif functionType == "staticmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 if argNames[0] in ("cls", "self"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 yield self.__error(node, "N806")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 for arg in argNames:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 if not self.LowercaseRegex.match(arg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 def __checkVariablesInFunction(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
386 Private method to check local variables in functions (N821).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 Local variables in functions should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
391 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 for parentFunc in reversed(parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 if isinstance(parentFunc, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 return
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4021
diff changeset
398 if isinstance(parentFunc, (ast.FunctionDef, ast.AsyncFunctionDef)):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 break
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 for target in node.targets:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 name = isinstance(target, ast.Name) and target.id
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 if not name or name in parentFunc.global_names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 if not self.LowercaseRegex.match(name) and name[:1] != '_':
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 yield self.__error(target, "N821")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 def __checkModule(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 Private method to check module naming conventions (N807, N808).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 Module and package names should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
417 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 if self.__filename:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 moduleName = os.path.splitext(os.path.basename(self.__filename))[0]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 if moduleName.lower() != moduleName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 yield self.__error(node, "N807")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 if moduleName == "__init__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 # we got a package
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 packageName = \
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 os.path.split(os.path.dirname(self.__filename))[1]
3762
ac51ccb60e35 Fixed an issue in the naming style checker for check N808.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3646
diff changeset
430 if packageName.lower() != packageName:
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 yield self.__error(node, "N808")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 def __checkImportAs(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 Private method to check that imports don't change the
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 naming convention (N811, N812, N813, N814).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
439 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 for name in node.names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 if not name.asname:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 if self.UppercaseRegexp.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 if not self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 yield self.__error(node, "N811")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 elif self.LowercaseRegex.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 if not self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 yield self.__error(node, "N812")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 elif self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 yield self.__error(node, "N813")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 elif self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 yield self.__error(node, "N814")
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
457
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
458 #
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
459 # eflag: noqa = M702

eric ide

mercurial