eric6/Plugins/CheckerPlugins/CodeStyleChecker/NamingStyleChecker.py

Sun, 04 Oct 2020 15:25:17 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 04 Oct 2020 15:25:17 +0200
changeset 7749
285855999b4e
parent 7639
422fd05e9c91
permissions
-rw-r--r--

Configuration Dialog: optimized the display of the various configuration pages.

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
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3 # Copyright (c) 2013 - 2020 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 """
7249
0bf517e60f54 Fixed some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
134 # try to find all 'old style decorators'
0bf517e60f54 Fixed some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
135 # like m = staticmethod(m)
2894
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 """
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
191 posArgs = [arg.arg for arg in node.args.args]
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
192 kwOnly = [arg.arg for arg in node.args.kwonlyargs]
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
193 return posArgs + kwOnly
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 def __error(self, node, code):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 """
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
197 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
198
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 @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
200 @param code error code to report (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 @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
202 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 if isinstance(node, ast.Module):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 lineno = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 offset = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 lineno = node.lineno
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 offset = node.col_offset
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 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
213 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
214 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 offset += 4
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 return (lineno, offset, code)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 def __isNameToBeAvoided(self, name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 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
221
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 @param name name to be checked (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 @return flag indicating to avoid it (boolen)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 return name in ("l", "O", "I")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
227 def __checkNameToBeAvoided(self, node, parents):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
228 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
229 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
230
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
231 @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
232 @param parents list of parent nodes
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
233 @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
234 (integer, integer, string)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
235 """
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
236 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
237 ast.AsyncFunctionDef)):
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
238 name = node.name
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
239 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
240 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
241 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
242
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
243 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
244 argNames = self.__getArgNames(node)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
245 for arg in argNames:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
246 if self.__isNameToBeAvoided(arg):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
247 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
248 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
249
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
250 if isinstance(node, ast.Assign):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
251 for target in node.targets:
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
252 if isinstance(target, ast.Name):
5597
3d88d53f8c2b Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5589
diff changeset
253 name = target.id
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
254 if not name:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
255 return
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
256
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
257 if self.__isNameToBeAvoided(name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
258 yield self.__error(node, "N831")
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
259 return
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
260
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
261 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
262 for element in target.elts:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
263 if isinstance(element, ast.Name):
5597
3d88d53f8c2b Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5589
diff changeset
264 name = element.id
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
265 if not name:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
266 return
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
267
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
268 if self.__isNameToBeAvoided(name):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
269 yield self.__error(node, "N831")
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
270 return
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
271
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 def __checkClassName(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 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
275 conventions (N801).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 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
278 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
279
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 @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
281 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 @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
283 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 if not self.CamelcaseRegexp.match(node.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 yield self.__error(node, "N801")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
288 def __checkFunctionName(self, node, parents):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 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
291 conventions (N802).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 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
294 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
295 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
296 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
297 (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
298
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 @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
300 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 @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
302 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 functionType = getattr(node, "function_type", "function")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 name = node.name
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
306 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
307 (functionType == "function" and "__" in (name[:2], name[-2:])) or
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
308 not self.LowercaseRegex.match(name)
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
309 ):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 yield self.__error(node, "N802")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 def __checkFunctionArgumentNames(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 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
315 (N803, N804, N805, N806).
2894
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 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
318 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
319 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
320
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 @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
322 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 @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
324 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 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
327 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
328 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
329 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
330 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
331 if not self.LowercaseRegex.match(kwarg):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 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
336 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
337 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
338 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
339 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
340 if not self.LowercaseRegex.match(vararg):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 argNames = self.__getArgNames(node)
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
345 functionType = getattr(node, "function_type", "function")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
346
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 if not argNames:
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
348 if functionType == "method":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
349 yield self.__error(node, "N805")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
350 elif functionType == "classmethod":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
351 yield self.__error(node, "N804")
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 if functionType == "method":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 if argNames[0] != "self":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 yield self.__error(node, "N805")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 elif functionType == "classmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 if argNames[0] != "cls":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 yield self.__error(node, "N804")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 elif functionType == "staticmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 if argNames[0] in ("cls", "self"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 yield self.__error(node, "N806")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 for arg in argNames:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 if not self.LowercaseRegex.match(arg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 def __checkVariablesInFunction(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
370 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
371
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 Local variables in functions should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 @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
375 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 @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
377 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 for parentFunc in reversed(parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 if isinstance(parentFunc, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 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
382 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
383 break
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 for target in node.targets:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 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
388 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
389 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 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
392 yield self.__error(target, "N821")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 def __checkModule(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 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
397
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 Module and package names should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 @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
401 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 @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
403 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 if self.__filename:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 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
407 if moduleName.lower() != moduleName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 yield self.__error(node, "N807")
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 if moduleName == "__init__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 # we got a package
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
412 packageName = (
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 os.path.split(os.path.dirname(self.__filename))[1]
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
414 )
3762
ac51ccb60e35 Fixed an issue in the naming style checker for check N808.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3646
diff changeset
415 if packageName.lower() != packageName:
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 yield self.__error(node, "N808")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 def __checkImportAs(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 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
421 naming convention (N811, N812, N813, N814).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 @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
424 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 @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
426 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 for name in node.names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 if not name.asname:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 if self.UppercaseRegexp.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 if not self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 yield self.__error(node, "N811")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 elif self.LowercaseRegex.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 if not self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 yield self.__error(node, "N812")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 elif self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 yield self.__error(node, "N813")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 elif self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 yield self.__error(node, "N814")

eric ide

mercurial