eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

Sat, 27 Jun 2020 12:08:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Jun 2020 12:08:12 +0200
changeset 7639
422fd05e9c91
parent 7637
c878e8255972
child 7651
ca87b7490449
permissions
-rw-r--r--

Removed some more Python2 related code.

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
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
154 import builtins
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
155 self.__builtins = [b for b in dir(builtins)
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
156 if b not in self.BuiltinsWhiteList]
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 # statistics counters
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 self.counters = {}
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 # collection of detected errors
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.errors = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 checkersWithCodes = [
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 (self.__checkCoding, ("M101", "M102")),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 (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
167 (self.__checkBuiltins, ("M131", "M132")),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
168 (self.__checkComprehensions, ("M181", "M182", "M183", "M184",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
169 "M185", "M186", "M187",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
170 "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
171 "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
172 (self.__checkDictWithSortedKeys, ("M201",)),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
173 (self.__checkDateTime, ("M301", "M302", "M303", "M304", "M305",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
174 "M306", "M307", "M308", "M311", "M312",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
175 "M313", "M314", "M315", "M321")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
176 (self.__checkSysVersion, ("M401", "M402", "M403",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
177 "M411", "M412", "M413", "M414",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
178 "M421", "M422", "M423")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
179 (self.__checkBugBear, ("M501", "M502", "M503", "M504", "M505",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
180 "M506", "M507", "M508", "M509",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
181 "M511", "M512", "M513",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
182 "M521", "M522", "M523", "M524")),
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
183 (self.__checkPep3101, ("M601",)),
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
184 (self.__checkFormatString, ("M611", "M612", "M613",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
185 "M621", "M622", "M623", "M624", "M625",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
186 "M631", "M632")),
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
187 (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
188 (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
189 (self.__checkGettext, ("M711",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 (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
191 (self.__checkTuple, ("M811",)),
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
192 (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
193 (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
194 (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
195 (self.__checkCommentedCode, ("M891",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 ]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 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
199 "BuiltinsChecker": {
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
200 "chr": ["unichr", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
201 "str": ["unicode", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
202 },
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 "CodingChecker": 'latin-1, utf-8',
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 "CopyrightChecker": {
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
205 "Author": "",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 "MinFilesize": 0,
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 },
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
208 "CommentedCodeChecker": {
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
209 "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
210 }
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 }
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 self.__checkers = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 for checker, codes in checkersWithCodes:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 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
216 for code in codes):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 self.__checkers.append(checker)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 def __ignoreCode(self, code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 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
222
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
223 @param code message code to check for
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
224 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
225 @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
226 @rtype bool
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 return (code.startswith(self.__ignore) and
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 not code.startswith(self.__select))
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 def __error(self, lineNumber, offset, code, *args):
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 Private method to record an issue.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
235 @param lineNumber line number of the issue
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
236 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
237 @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
238 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
239 @param code message code
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
240 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
241 @param args arguments for the message
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
242 @type list
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 if self.__ignoreCode(code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 if code in self.counters:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 self.counters[code] += 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 self.counters[code] = 1
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 # Don't care about expected codes
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 if code in self.__expected:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 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
257 # 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
258 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
259 {
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
260 "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
261 "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
262 "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
263 "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
264 "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
265 }
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 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 def __reportInvalidSyntax(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 Private method to report a syntax error.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 exc_type, exc = sys.exc_info()[:2]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 if len(exc.args) > 1:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 offset = exc.args[1]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 if len(offset) > 2:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 offset = offset[1:3]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 offset = (1, 0)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 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
280 'M901', exc_type.__name__, exc.args[0])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
282 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
283 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
284 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
285
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
286 @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
287 @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
288 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
289 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
290 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
291
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 def run(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 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
295 conditions.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 if not self.__filename:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 # 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
299 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 if not self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 # 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
303 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 try:
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
306 self.__tree = self.__generateTree()
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 except (SyntaxError, TypeError):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 self.__reportInvalidSyntax()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 for check in self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 check()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
314 def __getCoding(self):
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
315 """
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
316 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
317
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
318 @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
319 @rtype tuple of int and str
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
320 """
6106
921d070cec82 Finished implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6100
diff changeset
321 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
322 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
323 line, re.IGNORECASE)
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
324 if matched:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
325 return lineno, matched.group(1)
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
326 else:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
327 return 0, ""
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
328
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 def __checkCoding(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 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
332 encodings.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 if len(self.__source) == 0:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 encodings = [e.lower().strip()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 for e in self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 "CodingChecker", self.__defaultArgs["CodingChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 .split(",")]
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
341 lineno, coding = self.__getCoding()
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
342 if coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
343 if coding.lower() not in encodings:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
344 self.__error(lineno, 0, "M102", coding)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 self.__error(0, 0, "M101")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 def __checkCopyright(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 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
351 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 source = "".join(self.__source)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 copyrightArgs = self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 "CopyrightChecker", self.__defaultArgs["CopyrightChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 copyrightMinFileSize = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 "MinFilesize",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 self.__defaultArgs["CopyrightChecker"]["MinFilesize"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 copyrightAuthor = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 "Author",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 self.__defaultArgs["CopyrightChecker"]["Author"])
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
361 copyrightRegexStr = (
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 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
363 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 tocheck = max(1024, copyrightMinFileSize)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 topOfSource = source[:tocheck]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 if len(topOfSource) < copyrightMinFileSize:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 copyrightRe = re.compile(copyrightRegexStr.format(author=r".*"),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 if not copyrightRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 self.__error(0, 0, "M111")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 if copyrightAuthor:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 copyrightAuthorRe = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 copyrightRegexStr.format(author=copyrightAuthor),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 if not copyrightAuthorRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 self.__error(0, 0, "M112")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
383 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
384 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
385 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
386 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
387 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
388
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
389 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
390 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
391 "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
392 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
393 "Aggressive",
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
394 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
395 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
396 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
397 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
398
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
399 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
400 """
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
401 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
402 """
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
403 # 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
404 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
405 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415
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 # 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
417 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
418 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
419 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
420 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
421 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
422
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 def __checkPrintStatements(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 Private method to check for print statements.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 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
428 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
429 (isinstance(node, ast.Call) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
430 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
431 (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
432 ):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 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
434
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
435 def __checkTuple(self):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
436 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
437 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
438 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
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.Tuple) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
442 len(node.elts) == 1
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
443 ):
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
444 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
445
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
446 def __checkFuture(self):
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
447 """
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
448 Private method to check the __future__ imports.
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
449 """
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
450 expectedImports = {
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
451 i.strip()
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
452 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
453 if bool(i.strip())}
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
454 if len(expectedImports) == 0:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
455 # 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
456 return
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
457
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
458 imports = set()
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
459 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
460 hasCode = False
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
461
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
462 for node in ast.walk(self.__tree):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
463 if (isinstance(node, ast.ImportFrom) and
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
464 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
465 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
466 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
467 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
468 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
469 break
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
470 elif not (
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
471 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
472 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
473 ):
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
474 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
475 break
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
476
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
477 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
478 return
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
479
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
480 if not (imports >= expectedImports):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
481 if imports:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
482 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
483 ", ".join(expectedImports), ", ".join(imports))
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
484 else:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
485 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
486 ", ".join(expectedImports))
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
487
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
488 def __checkPep3101(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
489 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
490 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
491 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
492 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
493 match = self.__pep3101FormatRegex.search(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
494 if match:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
495 lineLen = len(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
496 pos = line.find('%')
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
497 formatPos = pos
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
498 formatter = '%'
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
499 if line[pos + 1] == "(":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
500 pos = line.find(")", pos)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
501 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
502 while c not in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
503 pos += 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
504 if pos >= lineLen:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
505 break
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
506 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
507 if c in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
508 formatter += c
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
509 self.__error(lineno, formatPos, "M601", formatter)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
510
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
511 def __checkFormatString(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
512 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
513 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
514 """
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
515 coding = self.__getCoding()[1]
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
516 if not coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
517 # default to utf-8
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
518 coding = "utf-8"
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
519
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
520 visitor = TextVisitor()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
521 visitor.visit(self.__tree)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
522 for node in visitor.nodes:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
523 text = node.s
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
524 if isinstance(text, bytes):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
525 try:
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
526 text = text.decode(coding)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
527 except UnicodeDecodeError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
528 continue
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
529 fields, implicit, explicit = self.__getFields(text)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
530 if implicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
531 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
532 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
533 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
534 if node.is_docstring:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
535 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
536 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
537 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
538
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
539 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
540 call, strArgs = visitor.calls[node]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
541
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
542 numbers = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
543 names = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
544 # 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
545 for name in fields:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
546 fieldMatch = self.FormatFieldRegex.match(name)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
547 try:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
548 number = int(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
549 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
550 number = -1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
551 # negative numbers are considered keywords
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
552 if number >= 0:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
553 numbers.add(number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
554 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
555 names.add(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
556
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
557 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
558 numArgs = len(call.args)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
559 if strArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
560 numArgs -= 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
561 if sys.version_info < (3, 5):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
562 hasKwArgs = bool(call.kwargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
563 hasStarArgs = bool(call.starargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
564 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
565 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
566 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
567 if isinstance(arg, ast.Starred))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
568
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
569 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
570 keywords.discard(None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
571 if hasStarArgs:
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
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
574 # 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
575 # 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
576 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
577 if not names:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
578 # No names but kwargs
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
579 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
580 if hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
581 if not numbers:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
582 # No numbers but args
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
583 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
584
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
585 if not hasKwArgs and not hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
586 # can actually verify numbers and names
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
587 for number in sorted(numbers):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
588 if number >= numArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
589 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
590 "M621", number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
591
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
592 for name in sorted(names):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
593 if name not in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
594 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
595 "M622", name)
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 for arg in range(numArgs):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
598 if arg not in numbers:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
599 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
600 arg)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
601
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
602 for keyword in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
603 if keyword not in names:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
604 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
605 keyword)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
606
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
607 if implicit and explicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
608 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
609
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
610 def __getFields(self, string):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
611 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
612 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
613
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
614 @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
615 @type str
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
616 @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
617 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
618 @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
619 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
620 fields = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
621 cnt = itertools.count()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
622 implicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
623 explicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
624 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
625 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
626 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
627 if not field:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
628 field = str(next(cnt))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
629 implicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
630 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
631 explicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
632 fields.add(field)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
633 fields.update(parsedSpec[1]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
634 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
635 if parsedSpec[1] is not None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
636 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
637 return set(), False, False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
638 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
639 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
640
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
641 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
642 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
643 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
644 """
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
645 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
646 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
647 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
648 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
649 pass
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
650
5619
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
651 ignoreBuiltinAssignments = self.__args.get(
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
652 "BuiltinsChecker", self.__defaultArgs["BuiltinsChecker"])
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
653
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
654 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
655 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
656 # assign statement
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
657 for element in node.targets:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
658 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
659 isinstance(element, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
660 element.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
661 ):
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
662 value = node.value
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
663 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
664 isinstance(value, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
665 element.id in ignoreBuiltinAssignments and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
666 value.id in ignoreBuiltinAssignments[element.id]
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
667 ):
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
668 # 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
669 continue
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
670 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
671 "M131", element.id)
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
672 elif isinstance(element, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
673 for tupleElement in element.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
674 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
675 isinstance(tupleElement, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
676 tupleElement.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
677 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
678 self.__error(tupleElement.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
679 tupleElement.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
680 "M131", tupleElement.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
681 elif isinstance(node, ast.For):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
682 # for loop
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
683 target = node.target
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
684 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
685 isinstance(target, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
686 target.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
687 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
688 self.__error(target.lineno - 1, target.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
689 "M131", target.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
690 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
691 for element in target.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
692 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
693 isinstance(element, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
694 element.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
695 ):
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
696 self.__error(element.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
697 element.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
698 "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
699 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
700 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
701 # (asynchronous) function definition
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
702 for arg in node.args.args:
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
703 if (
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
704 isinstance(arg, ast.arg) and
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
705 arg.arg in self.__builtins
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
706 ):
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
707 self.__error(arg.lineno - 1, arg.col_offset,
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
708 "M132", arg.arg)
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
709
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
710 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
711 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
712 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
713 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
714 for node in ast.walk(self.__tree):
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
715 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
716 nArgs = len(node.args)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
717
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
718 if (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
719 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
720 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
721 node.func.id in ('list', 'set')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
722 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
723 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
724 "list": "M181",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
725 "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
726 }[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
727 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
728
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
729 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
730 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
731 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
732 isinstance(node.args[0].elt, ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
733 len(node.args[0].elt.elts) == 2 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
734 node.func.id == "dict"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
735 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
736 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
737
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
738 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
739 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
740 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
741 node.func.id in ('list', 'set', 'dict')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
742 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
743 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
744 'list': 'M195',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
745 'dict': 'M185',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
746 '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
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)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
749
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
750 elif nArgs == 1 and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
751 isinstance(node.args[0], ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
752 node.func.id == "tuple" or
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
753 isinstance(node.args[0], ast.List) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
754 node.func.id == "list"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
755 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
756 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
757 'tuple': 'M197',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
758 'list': 'M198',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
759 }[node.func.id]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
760 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
761 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
762 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
763
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
764 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
765 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
766 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
767 node.func.id in ("tuple", "list", "set", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
768 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
769 errorCode = {
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
770 "tuple": "M192",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
771 "list": "M193",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
772 "set": "M191",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
773 "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
774 }[node.func.id]
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
775 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
776 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
777 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
778
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
779 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
780 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
781 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
782 node.func.id in ('all', 'any', 'enumerate', 'frozenset',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
783 'max', 'min', 'sorted', 'sum', 'tuple',)
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 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
786 node.func.id)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
787
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
788 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
789 nArgs == 0 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
790 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
791 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
792 node.func.id in ("tuple", "list", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
793 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
794 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
795 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
796
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
797 elif isinstance(node, ast.Compare) and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
798 len(node.ops) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
799 isinstance(node.ops[0], ast.In) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
800 len(node.comparators) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
801 isinstance(node.comparators[0], ast.ListComp)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
802 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
803 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
804
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
805 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
806 """
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
807 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
808 """
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
809 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
810 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
811 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
812 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
813 ast.Set,
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
814 )
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
815 mutableCalls = (
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
816 "Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
817 "OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
818 "collections.Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
819 "collections.OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
820 "collections.defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
821 "collections.deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
822 "defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
823 "deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
824 "dict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
825 "list",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
826 "set",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
827 )
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
828 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
829 "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
830 "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
831 )
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
832 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
833 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
834 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
835 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
836 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
837
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
838 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
839 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
840 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
841 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
842 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
843 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
844 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
845 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
846 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
847 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
848 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
849 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
850 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
851 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
852 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
853 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
854 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
855 "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
856 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
857 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
858 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
859 "M822", typeName)
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
860 else:
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
861 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
862 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
863 "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
864
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
865 def __dictShouldBeChecked(self, node):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
866 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
867 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
868
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
869 @param node reference to the AST node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
870 @return flag indicating to check the node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
871 @rtype bool
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
872 """
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
873 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
874 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
875
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
876 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
877 "__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
878 "__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
879 ):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
880 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
881
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
882 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
883 return len(lineNumbers) == len(set(lineNumbers))
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
884
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
885 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
886 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
887 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
888 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
889 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
890 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
891 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
892 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
893 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
894 "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
895
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
896 def __checkLogging(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
897 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
898 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
899 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
900 visitor = LoggingVisitor()
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
901 visitor.visit(self.__tree)
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
902 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
903 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
904
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
905 def __checkGettext(self):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
906 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
907 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
908 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
909 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
910 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
911 isinstance(node, ast.ImportFrom) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
912 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
913 ):
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
914 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
915 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
916
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
917 def __checkBugBear(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
918 """
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
919 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
920 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
921 visitor = BugBearVisitor()
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
922 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
923 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
924 node = violation[0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
925 reason = violation[1]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
926 params = violation[2:]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
927 self.__error(node.lineno - 1, node.col_offset, reason, *params)
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
928
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
929 def __checkReturn(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
930 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
931 Private method to check return statements.
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
932 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
933 visitor = ReturnVisitor()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
934 visitor.visit(self.__tree)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
935 for violation in visitor.violations:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
936 node = violation[0]
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
937 reason = violation[1]
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
938 self.__error(node.lineno - 1, node.col_offset, reason)
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
939
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
940 def __checkDateTime(self):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
941 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
942 Private method to check use of naive datetime functions.
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
943 """
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
944 # step 1: generate an augmented node tree containing parent info
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
945 # for each child node
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
946 tree = self.__generateTree()
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
947 for node in ast.walk(tree):
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
948 for childNode in ast.iter_child_nodes(node):
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
949 childNode._dtCheckerParent = node