eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

Sat, 21 Sep 2019 20:30:56 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 21 Sep 2019 20:30:56 +0200
changeset 7256
4ef3b78ebb4e
parent 7249
0bf517e60f54
child 7289
6f4761a73f5f
permissions
-rw-r--r--

Continued to resolve code style issue M841.

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
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6247
diff changeset
3 # Copyright (c) 2015 - 2019 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
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
19 def composeCallPath(node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
20 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
21 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
22
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
23 @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
24 @type ast.Node
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
25 @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
26 @rtype str
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
27 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
28 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
29 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
30 yield v
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
31 yield node.attr
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
32 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
33 yield node.id
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
34
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
35
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 class MiscellaneousChecker(object):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 Class implementing a checker for miscellaneous checks.
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 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
41 ## Coding line
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 "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
43
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
44 ## Copyright
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 "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
46
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
47 ## 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
48 "M131", "M132",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
49
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
50 ## Comprehensions
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
51 "M181", "M182", "M183", "M184",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
52 "M185", "M186", "M187",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
53 "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
54 "M195", "M196", "M197", "M198",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
55
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
56 ## 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
57 "M201",
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
58
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
59 ## 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
60 "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
61 "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
62 "M321",
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
63
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
64 ## sys.version and sys.version_info usage
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
65 "M401", "M402", "M403",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
66 "M411", "M412", "M413", "M414",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
67 "M421", "M422", "M423",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
68
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
69 ## 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
70 "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
71 "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
72 "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
73 "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
74
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
75 ## Format Strings
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
76 "M601",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
77 "M611", "M612", "M613",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
78 "M621", "M622", "M623", "M624", "M625",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
79 "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
80
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
81 ## Logging
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
82 "M651", "M652", "M653", "M654", "M655",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
83
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
84 ## Future statements
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
85 "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
86
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
87 ## Gettext
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
88 "M711",
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
89
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
90 ## print
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 "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
92
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
93 ## one element tuple
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
94 "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
95
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
96 ## Mutable Defaults
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
97 "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
98
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
99 ## 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
100 "M831", "M832", "M833", "M834",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
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
102 ## 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
103 "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
104
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 ## 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
106 "M891",
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
107
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
108 ## syntax error
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 "M901",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 ]
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
111
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
112 Formatter = Formatter()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
113 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
114
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
115 BuiltinsWhiteList = [
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
116 "__name__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
117 "__doc__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
118 "credits",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
119 ]
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 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
122 args):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 Constructor
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
126 @param source source code to be checked
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
127 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
128 @param filename name of the source file
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
129 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
130 @param select list of selected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
131 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
132 @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
133 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
134 @param expected list of expected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
135 @type list of str
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 @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
137 @type bool
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
138 @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
139 @type dict
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 self.__select = tuple(select)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.__ignore = ('',) if select else tuple(ignore)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 self.__expected = expected[:]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 self.__repeat = repeat
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 self.__filename = filename
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 self.__source = source[:]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__args = args
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.__pep3101FormatRegex = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 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
151
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
152 if sys.version_info >= (3, 0):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
153 import builtins
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
154 self.__builtins = [b for b in dir(builtins)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
155 if b not in self.BuiltinsWhiteList]
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
156 else:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
157 import __builtin__
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
158 self.__builtins = [b for b in dir(__builtin__)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
159 if b not in self.BuiltinsWhiteList]
4506
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 # statistics counters
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.counters = {}
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 # collection of detected errors
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.errors = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 checkersWithCodes = [
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 (self.__checkCoding, ("M101", "M102")),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 (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
170 (self.__checkBuiltins, ("M131", "M132")),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
171 (self.__checkComprehensions, ("M181", "M182", "M183", "M184",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
172 "M185", "M186", "M187",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
173 "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
174 "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
175 (self.__checkDictWithSortedKeys, ("M201",)),
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
176 (self.__checkDateTime, ("M301", "M302", "M303", "M304", "M305",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
177 "M306", "M307", "M308", "M311", "M312",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
178 "M313", "M314", "M315", "M321")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
179 (self.__checkSysVersion, ("M401", "M402", "M403",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
180 "M411", "M412", "M413", "M414",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
181 "M421", "M422", "M423")),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
182 (self.__checkBugBear, ("M501", "M502", "M503", "M504", "M505",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
183 "M506", "M507", "M508", "M509",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
184 "M511", "M512", "M513",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
185 "M521", "M522", "M523", "M524")),
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
186 (self.__checkPep3101, ("M601",)),
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
187 (self.__checkFormatString, ("M611", "M612", "M613",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
188 "M621", "M622", "M623", "M624", "M625",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
189 "M631", "M632")),
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
190 (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
191 (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
192 (self.__checkGettext, ("M711",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 (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
194 (self.__checkTuple, ("M811",)),
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
195 (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
196 (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
197 (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
198 (self.__checkCommentedCode, ("M891",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 ]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 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
202 "BuiltinsChecker": {
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
203 "chr": ["unichr", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
204 "str": ["unicode", ],
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
205 },
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 "CodingChecker": 'latin-1, utf-8',
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 "CopyrightChecker": {
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
208 "Author": "",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 "MinFilesize": 0,
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 },
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
211 "CommentedCodeChecker": {
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
212 "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
213 }
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 }
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 self.__checkers = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 for checker, codes in checkersWithCodes:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 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
219 for code in codes):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 self.__checkers.append(checker)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 def __ignoreCode(self, code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 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
225
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
226 @param code message code to check for
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
227 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
228 @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
229 @rtype bool
4506
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 return (code.startswith(self.__ignore) and
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 not code.startswith(self.__select))
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 def __error(self, lineNumber, offset, code, *args):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 Private method to record an issue.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
238 @param lineNumber line number of the issue
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
239 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
240 @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
241 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
242 @param code message code
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
243 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
244 @param args arguments for the message
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
245 @type list
4506
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 self.__ignoreCode(code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 if code in self.counters:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 self.counters[code] += 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 self.counters[code] = 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 # Don't care about expected codes
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 if code in self.__expected:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 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
260 # 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
261 self.errors.append(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 (self.__filename, lineNumber + 1, offset, (code, args)))
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 def __reportInvalidSyntax(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 Private method to report a syntax error.
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 exc_type, exc = sys.exc_info()[:2]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 if len(exc.args) > 1:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 offset = exc.args[1]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 if len(offset) > 2:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 offset = offset[1:3]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 offset = (1, 0)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 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
276 'M901', exc_type.__name__, exc.args[0])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
278 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
279 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
280 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
281
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
282 @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
283 @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
284 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
285 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
286 # Check type for py2: if not str it's unicode
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
287 if sys.version_info[0] == 2:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
288 try:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
289 source = source.encode('utf-8')
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
290 except UnicodeError:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
291 pass
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
292
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
293 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
294
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 def run(self):
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 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
298 conditions.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 if not self.__filename:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 # 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
302 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 if not self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 # 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
306 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 try:
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
309 self.__tree = self.__generateTree()
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 except (SyntaxError, TypeError):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 self.__reportInvalidSyntax()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 for check in self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 check()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
317 def __getCoding(self):
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
318 """
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
319 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
320
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
321 @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
322 @rtype tuple of int and str
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
323 """
6106
921d070cec82 Finished implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6100
diff changeset
324 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
325 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
326 line, re.IGNORECASE)
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
327 if matched:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
328 return lineno, matched.group(1)
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
329 else:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
330 return 0, ""
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
331
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 def __checkCoding(self):
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 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
335 encodings.
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 if len(self.__source) == 0:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 encodings = [e.lower().strip()
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 for e in self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 "CodingChecker", self.__defaultArgs["CodingChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 .split(",")]
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
344 lineno, coding = self.__getCoding()
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
345 if coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
346 if coding.lower() not in encodings:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
347 self.__error(lineno, 0, "M102", coding)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 self.__error(0, 0, "M101")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 def __checkCopyright(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 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
354 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 source = "".join(self.__source)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 copyrightArgs = self.__args.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 "CopyrightChecker", self.__defaultArgs["CopyrightChecker"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 copyrightMinFileSize = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 "MinFilesize",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 self.__defaultArgs["CopyrightChecker"]["MinFilesize"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 copyrightAuthor = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 "Author",
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 self.__defaultArgs["CopyrightChecker"]["Author"])
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
364 copyrightRegexStr = (
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 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
366 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 tocheck = max(1024, copyrightMinFileSize)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 topOfSource = source[:tocheck]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 if len(topOfSource) < copyrightMinFileSize:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 copyrightRe = re.compile(copyrightRegexStr.format(author=r".*"),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 if not copyrightRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 self.__error(0, 0, "M111")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 if copyrightAuthor:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 copyrightAuthorRe = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 copyrightRegexStr.format(author=copyrightAuthor),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 if not copyrightAuthorRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 self.__error(0, 0, "M112")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
386 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
387 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
388 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
389 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
390 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
391
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
392 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
393 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
394 "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
395 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
396 "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.__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
398 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
399 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
400 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
401
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 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
403 """
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
404 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
405 """
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 # 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416 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
417 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
418
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 # 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
420 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
421 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
422 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
423 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
424 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
425
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 def __checkPrintStatements(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 Private method to check for print statements.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 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
431 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
432 (isinstance(node, ast.Call) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
433 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
434 (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
435 ):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 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
437
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
438 def __checkTuple(self):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
439 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
440 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
441 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
442 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
443 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
444 isinstance(node, ast.Tuple) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
445 len(node.elts) == 1
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
446 ):
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
447 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
448
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
449 def __checkFuture(self):
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
450 """
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
451 Private method to check the __future__ imports.
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
452 """
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
453 expectedImports = {
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
454 i.strip()
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
455 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
456 if bool(i.strip())}
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
457 if len(expectedImports) == 0:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
458 # 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
459 return
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
460
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
461 imports = set()
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
462 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
463 hasCode = False
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
464
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
465 for node in ast.walk(self.__tree):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
466 if (isinstance(node, ast.ImportFrom) and
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
467 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
468 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
469 elif isinstance(node, ast.Expr):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
470 if not isinstance(node.value, ast.Str):
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
471 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
472 break
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
473 elif not isinstance(node, (ast.Module, ast.Str)):
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
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
524 if sys.version_info[0] > 2 and isinstance(text, bytes):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
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
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
702 if sys.version_info >= (3, 0):
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
703 for arg in node.args.args:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
704 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
705 isinstance(arg, ast.arg) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
706 arg.arg in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
707 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
708 self.__error(arg.lineno - 1, arg.col_offset,
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
709 "M132", arg.arg)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
710 else:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
711 for arg in node.args.args:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
712 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
713 isinstance(arg, ast.Name) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
714 arg.id in self.__builtins
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
715 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
716 self.__error(arg.lineno - 1, arg.col_offset,
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
717 "M132", arg.id)
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
718
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
719 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
720 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
721 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
722 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
723 for node in ast.walk(self.__tree):
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
724 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
725 nArgs = len(node.args)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
726
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
727 if (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
728 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
729 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
730 node.func.id in ('list', 'set')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
731 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
732 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
733 "list": "M181",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
734 "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
735 }[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
736 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
737
7245
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.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
741 isinstance(node.args[0].elt, ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
742 len(node.args[0].elt.elts) == 2 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
743 node.func.id == "dict"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
744 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
745 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
746
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
747 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
748 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
749 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
750 node.func.id in ('list', 'set', 'dict')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
751 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
752 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
753 'list': 'M195',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
754 'dict': 'M185',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
755 '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
756 }[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
757 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
758
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
759 elif nArgs == 1 and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
760 isinstance(node.args[0], ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
761 node.func.id == "tuple" or
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
762 isinstance(node.args[0], ast.List) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
763 node.func.id == "list"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
764 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
765 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
766 'tuple': 'M197',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
767 'list': 'M198',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
768 }[node.func.id]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
769 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
770 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
771 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
772
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
773 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
774 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
775 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
776 node.func.id in ("tuple", "list", "set", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
777 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
778 errorCode = {
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
779 "tuple": "M192",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
780 "list": "M193",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
781 "set": "M191",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
782 "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
783 }[node.func.id]
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
784 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
785 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
786 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
787
7245
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 == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
790 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
791 node.func.id in ('all', 'any', 'enumerate', 'frozenset',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
792 'max', 'min', 'sorted', 'sum', 'tuple',)
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, "M187",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
795 node.func.id)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
796
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
797 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
798 nArgs == 0 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
799 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
800 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
801 node.func.id in ("tuple", "list", "dict")
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, "M186",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
804 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
805
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
806 elif isinstance(node, ast.Compare) and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
807 len(node.ops) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
808 isinstance(node.ops[0], ast.In) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
809 len(node.comparators) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
810 isinstance(node.comparators[0], ast.ListComp)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
811 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
812 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
813
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
814 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
815 """
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
816 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
817 """
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
818 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
819 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
820 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
821 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
822 ast.Set,
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
823 )
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
824 mutableCalls = (
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
825 "Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
826 "OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
827 "collections.Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
828 "collections.OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
829 "collections.defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
830 "collections.deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
831 "defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
832 "deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
833 "dict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
834 "list",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
835 "set",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
836 )
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
837 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
838 "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
839 "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
840 )
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
841 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
842 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
843 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
844 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
845 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
846
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
847 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
848 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
849 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
850 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
851 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
852 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
853 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
854 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
855 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
856 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
857 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
858 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
859 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
860 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
861 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
862 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
863 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
864 "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
865 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
866 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
867 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
868 "M822", typeName)
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
869 else:
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
870 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
871 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
872 "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
873
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
874 def __dictShouldBeChecked(self, node):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
875 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
876 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
877
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
878 @param node reference to the AST node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
879 @return flag indicating to check the node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
880 @rtype bool
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 if not all(isinstance(key, ast.Str) for key in node.keys):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
883 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
884
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
885 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
886 "__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
887 "__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
888 ):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
889 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
890
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
891 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
892 return len(lineNumbers) == len(set(lineNumbers))
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
893
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
894 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
895 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
896 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
897 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
898 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
899 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
900 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
901 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
902 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
903 "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
904
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
905 def __checkLogging(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
906 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
907 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
908 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
909 visitor = LoggingVisitor()
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
910 visitor.visit(self.__tree)
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
911 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
912 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
913
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
914 def __checkGettext(self):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
915 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
916 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
917 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
918 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
919 if (
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
920 isinstance(node, ast.ImportFrom) and
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
921 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
922 ):
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
923 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
924 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
925
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
926 def __checkBugBear(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
927 """
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
928 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
929 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
930 visitor = BugBearVisitor()
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
931 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
932 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
933 node = violation[0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
934 reason = violation[1]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
935 params = violation[2:]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
936 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
937
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
938 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
939 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
940 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
941 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
942 visitor = ReturnVisitor()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
943 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
944 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
945 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
946 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
947 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
948