eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

Sat, 06 Jun 2020 19:42:15 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 06 Jun 2020 19:42:15 +0200
changeset 7610
df7025fe26a3
parent 7360
9190402e4505
child 7637
c878e8255972
permissions
-rw-r--r--

Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.

4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7360
9190402e4505 Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7289
diff changeset
3 # Copyright (c) 2015 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a checker for miscellaneous checks.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import sys
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import ast
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import re
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
13 import itertools
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
14 from string import Formatter
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
15 from collections import defaultdict
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
16 import tokenize
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
18 import AstUtilities
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
19
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
21 def composeCallPath(node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
22 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
23 Generator function to assemble the call path of a given node.
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
24
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
25 @param node node to assemble call path for
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
26 @type ast.Node
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
27 @return call path components
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
28 @rtype str
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
29 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
30 if isinstance(node, ast.Attribute):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
31 for v in composeCallPath(node.value):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
32 yield v
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
33 yield node.attr
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
34 elif isinstance(node, ast.Name):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
35 yield node.id
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
36
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
37
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 class MiscellaneousChecker(object):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 Class implementing a checker for miscellaneous checks.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 Codes = [
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
43 ## Coding line
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 "M101", "M102",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
45
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
46 ## Copyright
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 "M111", "M112",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
48
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
49 ## Shadowed Builtins
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
50 "M131", "M132",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
51
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
52 ## Comprehensions
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
53 "M181", "M182", "M183", "M184",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
54 "M185", "M186", "M187",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
55 "M191", "M192", "M193",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
56 "M195", "M196", "M197", "M198",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
57
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
58 ## Dictionaries with sorted keys
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
59 "M201",
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
60
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
61 ## Naive datetime usage
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
62 "M301", "M302", "M303", "M304", "M305", "M306", "M307", "M308",
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
63 "M311", "M312", "M313", "M314", "M315",
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
64 "M321",
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
65
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
66 ## sys.version and sys.version_info usage
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
67 "M401", "M402", "M403",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
68 "M411", "M412", "M413", "M414",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
69 "M421", "M422", "M423",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
70
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
71 ## Bugbear
7021
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
72 "M501", "M502", "M503", "M504", "M505", "M506", "M507", "M508",
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
73 "M509",
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
74 "M511", "M512", "M513",
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
75 "M521", "M522", "M523", "M524",
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
76
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
77 ## Format Strings
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
78 "M601",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
79 "M611", "M612", "M613",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
80 "M621", "M622", "M623", "M624", "M625",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
81 "M631", "M632",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
82
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
83 ## Logging
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
84 "M651", "M652", "M653", "M654", "M655",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
85
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
86 ## Future statements
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
87 "M701", "M702",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
88
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
89 ## Gettext
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
90 "M711",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
91
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
92 ## print
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 "M801",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
94
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
95 ## one element tuple
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
96 "M811",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
97
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
98 ## Mutable Defaults
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
99 "M821", "M822",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
100
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
101 ## return statements
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
102 "M831", "M832", "M833", "M834",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
104 ## line continuation
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
105 "M841",
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
106
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
107 ## commented code
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
108 "M891",
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
109
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
110 ## syntax error
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 "M901",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 ]
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
113
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
114 Formatter = Formatter()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
115 FormatFieldRegex = re.compile(r'^((?:\s|.)*?)(\..*|\[.*\])?$')
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
116
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
117 BuiltinsWhiteList = [
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
118 "__name__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
119 "__doc__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
120 "credits",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
121 ]
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 def __init__(self, source, filename, select, ignore, expected, repeat,
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 args):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 Constructor
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
128 @param source source code to be checked
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
129 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
130 @param filename name of the source file
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
131 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
132 @param select list of selected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
133 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
134 @param ignore list of codes to be ignored
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
135 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
136 @param expected list of expected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
137 @type list of str
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @param repeat flag indicating to report each occurrence of a code
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
139 @type bool
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
140 @param args dictionary of arguments for the miscellaneous checks
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
141 @type dict
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 self.__select = tuple(select)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 self.__ignore = ('',) if select else tuple(ignore)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 self.__expected = expected[:]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 self.__repeat = repeat
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__filename = filename
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.__source = source[:]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.__args = args
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.__pep3101FormatRegex = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 r'^(?:[^\'"]*[\'"][^\'"]*[\'"])*\s*%|^\s*%')
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
153
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
154 if sys.version_info >= (3, 0):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
155 import builtins
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
156 self.__builtins = [b for b in dir(builtins)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
157 if b not in self.BuiltinsWhiteList]
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
158 else:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
159 import __builtin__
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
160 self.__builtins = [b for b in dir(__builtin__)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
161 if b not in self.BuiltinsWhiteList]
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 # statistics counters
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.counters = {}
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 # collection of detected errors
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 self.errors = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 checkersWithCodes = [
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 (self.__checkCoding, ("M101", "M102")),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 (self.__checkCopyright, ("M111", "M112")),
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
172 (self.__checkBuiltins, ("M131", "M132")),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
173 (self.__checkComprehensions, ("M181", "M182", "M183", "M184",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
174 "M185", "M186", "M187",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
175 "M191", "M192", "M193",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
176 "M195", "M196", "M197", "M198")),
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
177 (self.__checkDictWithSortedKeys, ("M201",)),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
178 (self.__checkDateTime, ("M301", "M302", "M303", "M304", "M305",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
179 "M306", "M307", "M308", "M311", "M312",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
180 "M313", "M314", "M315", "M321")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
181 (self.__checkSysVersion, ("M401", "M402", "M403",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
182 "M411", "M412", "M413", "M414",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
183 "M421", "M422", "M423")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
184 (self.__checkBugBear, ("M501", "M502", "M503", "M504", "M505",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
185 "M506", "M507", "M508", "M509",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
186 "M511", "M512", "M513",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
187 "M521", "M522", "M523", "M524")),
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
188 (self.__checkPep3101, ("M601",)),
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
189 (self.__checkFormatString, ("M611", "M612", "M613",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
190 "M621", "M622", "M623", "M624", "M625",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
191 "M631", "M632")),
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
192 (self.__checkLogging, ("M651", "M652", "M653", "M654", "M655")),
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
193 (self.__checkFuture, ("M701", "M702")),
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
194 (self.__checkGettext, ("M711",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 (self.__checkPrintStatements, ("M801",)),
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
196 (self.__checkTuple, ("M811",)),
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
197 (self.__checkMutableDefault, ("M821", "M822")),
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
198 (self.__checkReturn, ("M831", "M832", "M833", "M834")),
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
199 (self.__checkLineContinuation, ("M841",)),
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
200 (self.__checkCommentedCode, ("M891",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 ]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 self.__defaultArgs = {
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
204 "BuiltinsChecker": {
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
205 "chr": ["unichr", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
206 "str": ["unicode", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
207 },
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 "CodingChecker": 'latin-1, utf-8',
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 "CopyrightChecker": {
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
210 "Author": "",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 "MinFilesize": 0,
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 },
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
213 "CommentedCodeChecker": {
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
214 "Aggressive": False,
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
215 }
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 }
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 self.__checkers = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 for checker, codes in checkersWithCodes:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 if any(not (code and self.__ignoreCode(code))
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 for code in codes):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 self.__checkers.append(checker)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 def __ignoreCode(self, code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 Private method to check if the message code should be ignored.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
228 @param code message code to check for
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
229 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
230 @return flag indicating to ignore the given code
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
231 @rtype bool
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 return (code.startswith(self.__ignore) and
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 not code.startswith(self.__select))
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def __error(self, lineNumber, offset, code, *args):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 Private method to record an issue.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
240 @param lineNumber line number of the issue
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
241 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
242 @param offset position within line of the issue
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
243 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
244 @param code message code
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
245 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
246 @param args arguments for the message
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
247 @type list
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 if self.__ignoreCode(code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 if code in self.counters:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 self.counters[code] += 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 self.counters[code] = 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 # Don't care about expected codes
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 if code in self.__expected:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 if code and (self.counters[code] == 1 or self.__repeat):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 # record the issue with one based line number
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.errors.append(
7610
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
264 {
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
265 "file": self.__filename,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
266 "line": lineNumber + 1,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
267 "offset": offset,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
268 "code": code,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
269 "args": args,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
270 }
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
271 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 def __reportInvalidSyntax(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 Private method to report a syntax error.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 exc_type, exc = sys.exc_info()[:2]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 if len(exc.args) > 1:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 offset = exc.args[1]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 if len(offset) > 2:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 offset = offset[1:3]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 offset = (1, 0)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 self.__error(offset[0] - 1, offset[1] or 0,
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 'M901', exc_type.__name__, exc.args[0])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
287 def __generateTree(self):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
288 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
289 Private method to generate an AST for our source.
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
290
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
291 @return generated AST
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
292 @rtype ast.AST
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
293 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
294 source = "".join(self.__source)
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
295 # Check type for py2: if not str it's unicode
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
296 if sys.version_info[0] == 2:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
297 try:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
298 source = source.encode('utf-8')
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
299 except UnicodeError:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
300 pass
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
301
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
302 return compile(source, self.__filename, 'exec', ast.PyCF_ONLY_AST)
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
303
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 def run(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 Public method to check the given source against miscellaneous
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 conditions.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 if not self.__filename:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 # don't do anything, if essential data is missing
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 if not self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 # don't do anything, if no codes were selected
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 try:
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
318 self.__tree = self.__generateTree()
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 except (SyntaxError, TypeError):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 self.__reportInvalidSyntax()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 for check in self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 check()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
326 def __getCoding(self):
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
327 """
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
328 Private method to get the defined coding of the source.
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
329
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
330 @return tuple containing the line number and the coding
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
331 @rtype tuple of int and str
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
332 """
6106
921d070cec82 Finished implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6100
diff changeset
333 for lineno, line in enumerate(self.__source[:5]):
6247
5c677a7f7d51 Corrected some code style issues detected by the new pycodestyle version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6188
diff changeset
334 matched = re.search(r'coding[:=]\s*([-\w_.]+)',
5c677a7f7d51 Corrected some code style issues detected by the new pycodestyle version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6188
diff changeset
335 line, re.IGNORECASE)
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
336 if matched:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
337 return lineno, matched.group(1)
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
338 else:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
339 return 0, ""
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
340
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 def __checkCoding(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 Private method to check the presence of a coding line and valid
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 encodings.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 if len(self.__source) == 0:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 encodings = [e.lower().strip()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 for e in self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 "CodingChecker", self.__defaultArgs["CodingChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 .split(",")]
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
353 lineno, coding = self.__getCoding()
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
354 if coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
355 if coding.lower() not in encodings:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
356 self.__error(lineno, 0, "M102", coding)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 self.__error(0, 0, "M101")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 def __checkCopyright(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 Private method to check the presence of a copyright statement.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 source = "".join(self.__source)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 copyrightArgs = self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 "CopyrightChecker", self.__defaultArgs["CopyrightChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 copyrightMinFileSize = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 "MinFilesize",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 self.__defaultArgs["CopyrightChecker"]["MinFilesize"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 copyrightAuthor = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 "Author",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 self.__defaultArgs["CopyrightChecker"]["Author"])
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
373 copyrightRegexStr = (
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 r"Copyright\s+(\(C\)\s+)?(\d{{4}}\s+-\s+)?\d{{4}}\s+{author}"
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
375 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 tocheck = max(1024, copyrightMinFileSize)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 topOfSource = source[:tocheck]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 if len(topOfSource) < copyrightMinFileSize:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 copyrightRe = re.compile(copyrightRegexStr.format(author=r".*"),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 if not copyrightRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 self.__error(0, 0, "M111")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 if copyrightAuthor:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 copyrightAuthorRe = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 copyrightRegexStr.format(author=copyrightAuthor),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if not copyrightAuthorRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 self.__error(0, 0, "M112")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
395 def __checkCommentedCode(self):
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
396 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
397 Private method to check for commented code.
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
398 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
399 from eradicate import commented_out_code_line_numbers
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
400
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
401 source = "".join(self.__source)
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
402 commentedCodeCheckerArgs = self.__args.get(
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
403 "CommentedCodeChecker", self.__defaultArgs["CommentedCodeChecker"])
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
404 aggressive = commentedCodeCheckerArgs.get(
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
405 "Aggressive",
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
406 self.__defaultArgs["CommentedCodeChecker"]["Aggressive"])
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
407 for markedLine in commented_out_code_line_numbers(
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
408 source, aggressive=aggressive):
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
409 self.__error(markedLine - 1, 0, "M891")
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
410
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
411 def __checkLineContinuation(self):
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
412 """
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
413 Private method to check line continuation using backslash.
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
414 """
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
415 # generate source lines without comments
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
416 linesIterator = iter(self.__source)
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
417 tokens = tokenize.generate_tokens(lambda: next(linesIterator))
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
418 comments = [token for token in tokens if token[0] == tokenize.COMMENT]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
419 stripped = self.__source[:]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
420 for comment in comments:
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
421 lineno = comment[3][0]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
422 start = comment[2][1]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
423 stop = comment[3][1]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
424 content = stripped[lineno - 1]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
425 withoutComment = content[:start] + content[stop:]
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
426 stripped[lineno - 1] = withoutComment.rstrip()
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
427
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
428 # perform check with 'cleaned' source
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
429 for lineIndex, line in enumerate(stripped):
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
430 strippedLine = line.strip()
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
431 if (strippedLine.endswith('\\') and
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
432 not strippedLine.startswith(('assert', 'with'))):
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
433 self.__error(lineIndex, len(line), "M841")
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
434
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 def __checkPrintStatements(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 Private method to check for print statements.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 for node in ast.walk(self.__tree):
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
440 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
441 (isinstance(node, ast.Call) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
442 getattr(node.func, 'id', None) == 'print') or
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
443 (hasattr(ast, 'Print') and isinstance(node, ast.Print))
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
444 ):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 self.__error(node.lineno - 1, node.col_offset, "M801")
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
446
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
447 def __checkTuple(self):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
448 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
449 Private method to check for one element tuples.
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
450 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
451 for node in ast.walk(self.__tree):
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
452 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
453 isinstance(node, ast.Tuple) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
454 len(node.elts) == 1
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
455 ):
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
456 self.__error(node.lineno - 1, node.col_offset, "M811")
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
457
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
458 def __checkFuture(self):
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
459 """
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
460 Private method to check the __future__ imports.
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
461 """
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
462 expectedImports = {
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
463 i.strip()
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
464 for i in self.__args.get("FutureChecker", "").split(",")
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
465 if bool(i.strip())}
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
466 if len(expectedImports) == 0:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
467 # nothing to check for; disabling the check
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
468 return
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
469
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
470 imports = set()
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
471 node = None
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
472 hasCode = False
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
473
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
474 for node in ast.walk(self.__tree):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
475 if (isinstance(node, ast.ImportFrom) and
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
476 node.module == '__future__'):
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
477 imports |= {name.name for name in node.names}
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
478 elif isinstance(node, ast.Expr):
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
479 if not AstUtilities.isString(node.value):
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
480 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
481 break
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
482 elif not (
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
483 AstUtilities.isString(node) or
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
484 isinstance(node, ast.Module)
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
485 ):
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
486 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
487 break
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
488
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
489 if isinstance(node, ast.Module) or not hasCode:
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
490 return
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
491
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
492 if not (imports >= expectedImports):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
493 if imports:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
494 self.__error(node.lineno - 1, node.col_offset, "M701",
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
495 ", ".join(expectedImports), ", ".join(imports))
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
496 else:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
497 self.__error(node.lineno - 1, node.col_offset, "M702",
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
498 ", ".join(expectedImports))
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
499
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
500 def __checkPep3101(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
501 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
502 Private method to check for old style string formatting.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
503 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
504 for lineno, line in enumerate(self.__source):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
505 match = self.__pep3101FormatRegex.search(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
506 if match:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
507 lineLen = len(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
508 pos = line.find('%')
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
509 formatPos = pos
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
510 formatter = '%'
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
511 if line[pos + 1] == "(":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
512 pos = line.find(")", pos)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
513 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
514 while c not in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
515 pos += 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
516 if pos >= lineLen:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
517 break
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
518 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
519 if c in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
520 formatter += c
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
521 self.__error(lineno, formatPos, "M601", formatter)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
522
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
523 def __checkFormatString(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
524 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
525 Private method to check string format strings.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
526 """
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
527 coding = self.__getCoding()[1]
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
528 if not coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
529 # default to utf-8
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
530 coding = "utf-8"
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
531
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
532 visitor = TextVisitor()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
533 visitor.visit(self.__tree)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
534 for node in visitor.nodes:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
535 text = node.s
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
536 if sys.version_info[0] > 2 and isinstance(text, bytes):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
537 try:
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
538 text = text.decode(coding)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
539 except UnicodeDecodeError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
540 continue
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
541 fields, implicit, explicit = self.__getFields(text)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
542 if implicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
543 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
544 self.__error(node.lineno - 1, node.col_offset, "M611")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
545 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
546 if node.is_docstring:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
547 self.__error(node.lineno - 1, node.col_offset, "M612")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
548 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
549 self.__error(node.lineno - 1, node.col_offset, "M613")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
550
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
551 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
552 call, strArgs = visitor.calls[node]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
553
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
554 numbers = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
555 names = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
556 # Determine which fields require a keyword and which an arg
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
557 for name in fields:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
558 fieldMatch = self.FormatFieldRegex.match(name)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
559 try:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
560 number = int(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
561 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
562 number = -1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
563 # negative numbers are considered keywords
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
564 if number >= 0:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
565 numbers.add(number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
566 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
567 names.add(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
568
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
569 keywords = {keyword.arg for keyword in call.keywords}
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
570 numArgs = len(call.args)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
571 if strArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
572 numArgs -= 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
573 if sys.version_info < (3, 5):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
574 hasKwArgs = bool(call.kwargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
575 hasStarArgs = bool(call.starargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
576 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
577 hasKwArgs = any(kw.arg is None for kw in call.keywords)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
578 hasStarArgs = sum(1 for arg in call.args
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
579 if isinstance(arg, ast.Starred))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
580
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
581 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
582 keywords.discard(None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
583 if hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
584 numArgs -= 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
585
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
586 # if starargs or kwargs is not None, it can't count the
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
587 # parameters but at least check if the args are used
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
588 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
589 if not names:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
590 # No names but kwargs
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
591 self.__error(call.lineno - 1, call.col_offset, "M623")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
592 if hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
593 if not numbers:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
594 # No numbers but args
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
595 self.__error(call.lineno - 1, call.col_offset, "M624")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
596
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
597 if not hasKwArgs and not hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
598 # can actually verify numbers and names
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
599 for number in sorted(numbers):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
600 if number >= numArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
601 self.__error(call.lineno - 1, call.col_offset,
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
602 "M621", number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
603
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
604 for name in sorted(names):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
605 if name not in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
606 self.__error(call.lineno - 1, call.col_offset,
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
607 "M622", name)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
608
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
609 for arg in range(numArgs):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
610 if arg not in numbers:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
611 self.__error(call.lineno - 1, call.col_offset, "M631",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
612 arg)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
613
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
614 for keyword in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
615 if keyword not in names:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
616 self.__error(call.lineno - 1, call.col_offset, "M632",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
617 keyword)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
618
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
619 if implicit and explicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
620 self.__error(call.lineno - 1, call.col_offset, "M625")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
621
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
622 def __getFields(self, string):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
623 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
624 Private method to extract the format field information.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
625
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
626 @param string format string to be parsed
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
627 @type str
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
628 @return format field information as a tuple with fields, implicit
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
629 field definitions present and explicit field definitions present
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
630 @rtype tuple of set of str, bool, bool
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
631 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
632 fields = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
633 cnt = itertools.count()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
634 implicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
635 explicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
636 try:
6188
5a6ae3be31e6 Fixed some loop related coding issues detected by the extended code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6184
diff changeset
637 for _literal, field, spec, conv in self.Formatter.parse(string):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
638 if field is not None and (conv is None or conv in 'rsa'):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
639 if not field:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
640 field = str(next(cnt))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
641 implicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
642 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
643 explicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
644 fields.add(field)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
645 fields.update(parsedSpec[1]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
646 for parsedSpec in self.Formatter.parse(spec)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
647 if parsedSpec[1] is not None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
648 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
649 return set(), False, False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
650 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
651 return fields, implicit, explicit
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
652
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
653 def __checkBuiltins(self):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
654 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
655 Private method to check, if built-ins are shadowed.
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
656 """
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
657 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
658 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
659 functionDefs.append(ast.AsyncFunctionDef)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
660 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
661 pass
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
662
5619
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
663 ignoreBuiltinAssignments = self.__args.get(
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
664 "BuiltinsChecker", self.__defaultArgs["BuiltinsChecker"])
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
665
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
666 for node in ast.walk(self.__tree):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
667 if isinstance(node, ast.Assign):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
668 # assign statement
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
669 for element in node.targets:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
670 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
671 isinstance(element, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
672 element.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
673 ):
5617
1b9e3d94864a Removed obsolete checker for M121 (blind except) because that is already checked by pycodestyle.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5589
diff changeset
674 value = node.value
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
675 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
676 isinstance(value, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
677 element.id in ignoreBuiltinAssignments and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
678 value.id in ignoreBuiltinAssignments[element.id]
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
679 ):
5617
1b9e3d94864a Removed obsolete checker for M121 (blind except) because that is already checked by pycodestyle.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5589
diff changeset
680 # ignore compatibility assignments
1b9e3d94864a Removed obsolete checker for M121 (blind except) because that is already checked by pycodestyle.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5589
diff changeset
681 continue
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
682 self.__error(element.lineno - 1, element.col_offset,
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
683 "M131", element.id)
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
684 elif isinstance(element, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
685 for tupleElement in element.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
686 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
687 isinstance(tupleElement, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
688 tupleElement.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
689 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
690 self.__error(tupleElement.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
691 tupleElement.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
692 "M131", tupleElement.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
693 elif isinstance(node, ast.For):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
694 # for loop
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
695 target = node.target
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
696 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
697 isinstance(target, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
698 target.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
699 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
700 self.__error(target.lineno - 1, target.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
701 "M131", target.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
702 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
703 for element in target.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
704 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
705 isinstance(element, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
706 element.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
707 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
708 self.__error(element.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
709 element.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
710 "M131", element.id)
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
711 elif any(isinstance(node, functionDef)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
712 for functionDef in functionDefs):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
713 # (asynchronous) function definition
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
714 if sys.version_info >= (3, 0):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
715 for arg in node.args.args:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
716 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
717 isinstance(arg, ast.arg) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
718 arg.arg in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
719 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
720 self.__error(arg.lineno - 1, arg.col_offset,
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
721 "M132", arg.arg)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
722 else:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
723 for arg in node.args.args:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
724 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
725 isinstance(arg, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
726 arg.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
727 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
728 self.__error(arg.lineno - 1, arg.col_offset,
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
729 "M132", arg.id)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
730
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
731 def __checkComprehensions(self):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
732 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
733 Private method to check some comprehension related things.
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
734 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
735 for node in ast.walk(self.__tree):
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
736 if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
737 nArgs = len(node.args)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
738
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
739 if (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
740 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
741 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
742 node.func.id in ('list', 'set')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
743 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
744 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
745 "list": "M181",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
746 "set": "M182",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
747 }[node.func.id]
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
748 self.__error(node.lineno - 1, node.col_offset, errorCode)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
749
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
750 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
751 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
752 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
753 isinstance(node.args[0].elt, ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
754 len(node.args[0].elt.elts) == 2 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
755 node.func.id == "dict"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
756 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
757 self.__error(node.lineno - 1, node.col_offset, "M183")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
758
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
759 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
760 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
761 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
762 node.func.id in ('list', 'set', 'dict')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
763 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
764 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
765 'list': 'M195',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
766 'dict': 'M185',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
767 'set': 'M184',
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
768 }[node.func.id]
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
769 self.__error(node.lineno - 1, node.col_offset, errorCode)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
770
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
771 elif nArgs == 1 and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
772 isinstance(node.args[0], ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
773 node.func.id == "tuple" or
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
774 isinstance(node.args[0], ast.List) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
775 node.func.id == "list"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
776 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
777 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
778 'tuple': 'M197',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
779 'list': 'M198',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
780 }[node.func.id]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
781 self.__error(node.lineno - 1, node.col_offset, errorCode,
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
782 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
783 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
784
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
785 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
786 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
787 isinstance(node.args[0], (ast.Tuple, ast.List)) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
788 node.func.id in ("tuple", "list", "set", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
789 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
790 errorCode = {
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
791 "tuple": "M192",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
792 "list": "M193",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
793 "set": "M191",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
794 "dict": "M191",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
795 }[node.func.id]
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
796 self.__error(node.lineno - 1, node.col_offset, errorCode,
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
797 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
798 node.func.id)
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
799
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
800 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
801 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
802 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
803 node.func.id in ('all', 'any', 'enumerate', 'frozenset',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
804 'max', 'min', 'sorted', 'sum', 'tuple',)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
805 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
806 self.__error(node.lineno - 1, node.col_offset, "M187",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
807 node.func.id)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
808
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
809 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
810 nArgs == 0 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
811 not any(isinstance(a, ast.Starred) for a in node.args) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
812 not any(k.arg is None for k in node.keywords) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
813 node.func.id in ("tuple", "list", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
814 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
815 self.__error(node.lineno - 1, node.col_offset, "M186",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
816 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
817
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
818 elif isinstance(node, ast.Compare) and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
819 len(node.ops) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
820 isinstance(node.ops[0], ast.In) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
821 len(node.comparators) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
822 isinstance(node.comparators[0], ast.ListComp)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
823 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
824 self.__error(node.lineno - 1, node.col_offset, "M196")
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
825
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
826 def __checkMutableDefault(self):
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
827 """
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
828 Private method to check for use of mutable types as default arguments.
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
829 """
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
830 mutableTypes = (
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
831 ast.Call,
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
832 ast.Dict,
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
833 ast.List,
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
834 ast.Set,
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
835 )
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
836 mutableCalls = (
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
837 "Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
838 "OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
839 "collections.Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
840 "collections.OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
841 "collections.defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
842 "collections.deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
843 "defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
844 "deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
845 "dict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
846 "list",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
847 "set",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
848 )
7021
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
849 immutableCalls = (
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
850 "tuple",
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
851 "frozenset",
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
852 )
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
853 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
854 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
855 functionDefs.append(ast.AsyncFunctionDef)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
856 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
857 pass
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
858
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
859 for node in ast.walk(self.__tree):
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
860 if any(isinstance(node, functionDef)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
861 for functionDef in functionDefs):
7057
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
862 defaults = node.args.defaults[:]
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
863 try:
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
864 defaults += node.args.kw_defaults[:]
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
865 except AttributeError:
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
866 pass
0e8d3b0c4889 MiscellaneousChecker: changed code to cope with some strange situation where the arguments of a function definition don't have the kw_defaults attribute (AST).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7045
diff changeset
867 for default in defaults:
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
868 if any(isinstance(default, mutableType)
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
869 for mutableType in mutableTypes):
5703
7650c11cc5f2 MiscChecker, ast: No special case for Python 2 needed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5649
diff changeset
870 typeName = type(default).__name__
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
871 if isinstance(default, ast.Call):
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
872 callPath = '.'.join(composeCallPath(default.func))
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
873 if callPath in mutableCalls:
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
874 self.__error(default.lineno - 1,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
875 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
876 "M823", callPath + "()")
7021
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
877 elif callPath not in immutableCalls:
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
878 self.__error(default.lineno - 1,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
879 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
880 "M822", typeName)
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
881 else:
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
882 self.__error(default.lineno - 1,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
883 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
884 "M821", typeName)
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
885
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
886 def __dictShouldBeChecked(self, node):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
887 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
888 Private function to test, if the node should be checked.
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
889
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
890 @param node reference to the AST node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
891 @return flag indicating to check the node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
892 @rtype bool
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
893 """
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
894 if not all(AstUtilities.isString(key) for key in node.keys):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
895 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
896
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
897 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
898 "__IGNORE_WARNING__" in self.__source[node.lineno - 1] or
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
899 "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1]
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
900 ):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
901 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
902
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
903 lineNumbers = [key.lineno for key in node.keys]
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
904 return len(lineNumbers) == len(set(lineNumbers))
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
905
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
906 def __checkDictWithSortedKeys(self):
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
907 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
908 Private method to check, if dictionary keys appear in sorted order.
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
909 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
910 for node in ast.walk(self.__tree):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
911 if isinstance(node, ast.Dict) and self.__dictShouldBeChecked(node):
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
912 for key1, key2 in zip(node.keys, node.keys[1:]):
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
913 if key2.s < key1.s:
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
914 self.__error(key2.lineno - 1, key2.col_offset,
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
915 "M201", key2.s, key1.s)
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
916
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
917 def __checkLogging(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
918 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
919 Private method to check logging statements.
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
920 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
921 visitor = LoggingVisitor()
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
922 visitor.visit(self.__tree)
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
923 for node, reason in visitor.violations:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
924 self.__error(node.lineno - 1, node.col_offset, reason)
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
925
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
926 def __checkGettext(self):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
927 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
928 Private method to check the 'gettext' import statement.
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
929 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
930 for node in ast.walk(self.__tree):
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
931 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
932 isinstance(node, ast.ImportFrom) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
933 any(name.asname == '_' for name in node.names)
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
934 ):
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
935 self.__error(node.lineno - 1, node.col_offset, "M711",
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
936 node.names[0].name)
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
937
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
938 def __checkBugBear(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
939 """
7021
2894aa889a4e MiscellaneousChecker: added checks for use of 'gettattr' and 'setattr', bas super() calls and 'assert False' statements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
940 Private method for bugbear checks.
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
941 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
942 visitor = BugBearVisitor()
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
943 visitor.visit(self.__tree)
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
944 for violation in visitor.violations:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
945 node = violation[0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
946 reason = violation[1]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
947 params = violation[2:]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
948 self.__error(node.lineno - 1, node.col_offset, reason, *params)