eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

Thu, 19 Sep 2019 19:39:04 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 19 Sep 2019 19:39:04 +0200
changeset 7249
0bf517e60f54
parent 7245
b47179fbb9d8
child 7256
4ef3b78ebb4e
permissions
-rw-r--r--

Fixed some code style issues.

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"])
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 copyrightRegexStr = \
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}"
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 tocheck = max(1024, copyrightMinFileSize)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 topOfSource = source[:tocheck]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 if len(topOfSource) < copyrightMinFileSize:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 copyrightRe = re.compile(copyrightRegexStr.format(author=r".*"),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 if not copyrightRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 self.__error(0, 0, "M111")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 if copyrightAuthor:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 copyrightAuthorRe = re.compile(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 copyrightRegexStr.format(author=copyrightAuthor),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 re.IGNORECASE)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 if not copyrightAuthorRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 self.__error(0, 0, "M112")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
385 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
386 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
387 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
388 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
389 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
390
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
391 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
392 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
393 "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
394 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
395 "Aggressive",
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
396 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
397 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
398 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
399 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
400
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
401 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
402 """
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
403 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
404 """
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 # 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416 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
417
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 # 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
419 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
420 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
421 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
422 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
423 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
424
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 def __checkPrintStatements(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 Private method to check for print statements.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 for node in ast.walk(self.__tree):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 if (isinstance(node, ast.Call) and
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 getattr(node.func, 'id', None) == 'print') or \
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 (hasattr(ast, 'Print') and isinstance(node, ast.Print)):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 self.__error(node.lineno - 1, node.col_offset, "M801")
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
434
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
435 def __checkTuple(self):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
436 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
437 Private method to check for one element tuples.
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
438 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
439 for node in ast.walk(self.__tree):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
440 if isinstance(node, ast.Tuple) and \
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
441 len(node.elts) == 1:
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
442 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
443
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
444 def __checkFuture(self):
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
445 """
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
446 Private method to check the __future__ imports.
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
447 """
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
448 expectedImports = {
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
449 i.strip()
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
450 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
451 if bool(i.strip())}
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
452 if len(expectedImports) == 0:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
453 # 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
454 return
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
455
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
456 imports = set()
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
457 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
458 hasCode = False
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
459
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
460 for node in ast.walk(self.__tree):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
461 if (isinstance(node, ast.ImportFrom) and
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
462 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
463 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
464 elif isinstance(node, ast.Expr):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
465 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
466 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
467 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
468 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
469 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
470 break
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
471
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
472 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
473 return
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
474
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
475 if not (imports >= expectedImports):
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
476 if imports:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
477 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
478 ", ".join(expectedImports), ", ".join(imports))
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
479 else:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
480 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
481 ", ".join(expectedImports))
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
482
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
483 def __checkPep3101(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
484 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
485 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
486 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
487 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
488 match = self.__pep3101FormatRegex.search(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
489 if match:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
490 lineLen = len(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
491 pos = line.find('%')
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
492 formatPos = pos
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
493 formatter = '%'
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
494 if line[pos + 1] == "(":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
495 pos = line.find(")", pos)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
496 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
497 while c not in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
498 pos += 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
499 if pos >= lineLen:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
500 break
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 if c in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
503 formatter += c
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
504 self.__error(lineno, formatPos, "M601", formatter)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
505
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
506 def __checkFormatString(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
507 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
508 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
509 """
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
510 coding = self.__getCoding()[1]
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
511 if not coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
512 # default to utf-8
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
513 coding = "utf-8"
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
514
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
515 visitor = TextVisitor()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
516 visitor.visit(self.__tree)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
517 for node in visitor.nodes:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
518 text = node.s
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
519 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
520 try:
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
521 text = text.decode(coding)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
522 except UnicodeDecodeError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
523 continue
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
524 fields, implicit, explicit = self.__getFields(text)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
525 if implicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
526 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
527 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
528 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
529 if node.is_docstring:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
530 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
531 else:
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, "M613")
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
533
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
534 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
535 call, strArgs = visitor.calls[node]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
536
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
537 numbers = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
538 names = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
539 # 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
540 for name in fields:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
541 fieldMatch = self.FormatFieldRegex.match(name)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
542 try:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
543 number = int(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
544 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
545 number = -1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
546 # negative numbers are considered keywords
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
547 if number >= 0:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
548 numbers.add(number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
549 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
550 names.add(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
551
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
552 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
553 numArgs = len(call.args)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
554 if strArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
555 numArgs -= 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
556 if sys.version_info < (3, 5):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
557 hasKwArgs = bool(call.kwargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
558 hasStarArgs = bool(call.starargs)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
559 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
560 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
561 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
562 if isinstance(arg, ast.Starred))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
563
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
564 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
565 keywords.discard(None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
566 if hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
567 numArgs -= 1
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 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
570 # 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
571 if hasKwArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
572 if not names:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
573 # No names but kwargs
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
574 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
575 if hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
576 if not numbers:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
577 # No numbers but args
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
578 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
579
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
580 if not hasKwArgs and not hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
581 # can actually verify numbers and names
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
582 for number in sorted(numbers):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
583 if number >= numArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
584 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
585 "M621", number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
586
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
587 for name in sorted(names):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
588 if name not in keywords:
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 "M622", name)
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 arg in range(numArgs):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
593 if arg not in numbers:
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, "M631",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
595 arg)
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 keyword in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
598 if keyword not in names:
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, "M632",
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
600 keyword)
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 if implicit and explicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
603 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
604
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
605 def __getFields(self, string):
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 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
608
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
609 @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
610 @type str
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
611 @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
612 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
613 @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
614 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
615 fields = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
616 cnt = itertools.count()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
617 implicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
618 explicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
619 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
620 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
621 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
622 if not field:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
623 field = str(next(cnt))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
624 implicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
625 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
626 explicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
627 fields.add(field)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
628 fields.update(parsedSpec[1]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
629 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
630 if parsedSpec[1] is not None)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
631 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
632 return set(), False, False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
633 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
634 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
635
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
636 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
637 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
638 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
639 """
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
640 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
641 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
642 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
643 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
644 pass
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
645
5619
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
646 ignoreBuiltinAssignments = self.__args.get(
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
647 "BuiltinsChecker", self.__defaultArgs["BuiltinsChecker"])
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
648
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
649 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
650 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
651 # assign statement
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
652 for element in node.targets:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
653 if isinstance(element, ast.Name) and \
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
654 element.id in self.__builtins:
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
655 value = node.value
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
656 if isinstance(value, ast.Name) and \
5619
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
657 element.id in ignoreBuiltinAssignments and \
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
658 value.id in ignoreBuiltinAssignments[element.id]:
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
659 # 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
660 continue
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
661 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
662 "M131", element.id)
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
663 elif isinstance(element, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
664 for tupleElement in element.elts:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
665 if isinstance(tupleElement, ast.Name) and \
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
666 tupleElement.id in self.__builtins:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
667 self.__error(tupleElement.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
668 tupleElement.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
669 "M131", tupleElement.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
670 elif isinstance(node, ast.For):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
671 # for loop
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
672 target = node.target
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
673 if isinstance(target, ast.Name) and \
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
674 target.id in self.__builtins:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
675 self.__error(target.lineno - 1, target.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
676 "M131", target.id)
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
677 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
678 for element in target.elts:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
679 if isinstance(element, ast.Name) and \
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
680 element.id in self.__builtins:
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
681 self.__error(element.lineno - 1,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
682 element.col_offset,
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
683 "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
684 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
685 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
686 # (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
687 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
688 for arg in node.args.args:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
689 if isinstance(arg, ast.arg) and \
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
690 arg.arg in self.__builtins:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
691 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
692 "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
693 else:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
694 for arg in node.args.args:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
695 if isinstance(arg, ast.Name) and \
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
696 arg.id in self.__builtins:
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
697 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
698 "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
699
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
700 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
701 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
702 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
703 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
704 for node in ast.walk(self.__tree):
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
705 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
706 nArgs = len(node.args)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
707
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
708 if (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
709 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
710 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
711 node.func.id in ('list', 'set')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
712 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
713 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
714 "list": "M181",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
715 "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
716 }[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
717 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
718
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
719 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
720 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
721 isinstance(node.args[0], ast.GeneratorExp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
722 isinstance(node.args[0].elt, ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
723 len(node.args[0].elt.elts) == 2 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
724 node.func.id == "dict"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
725 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
726 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
727
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
728 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
729 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
730 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
731 node.func.id in ('list', 'set', 'dict')
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
732 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
733 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
734 'list': 'M195',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
735 'dict': 'M185',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
736 '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
737 }[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
738 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
739
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
740 elif nArgs == 1 and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
741 isinstance(node.args[0], ast.Tuple) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
742 node.func.id == "tuple" or
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
743 isinstance(node.args[0], ast.List) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
744 node.func.id == "list"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
745 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
746 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
747 'tuple': 'M197',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
748 'list': 'M198',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
749 }[node.func.id]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
750 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
751 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
752 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
753
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
754 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
755 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
756 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
757 node.func.id in ("tuple", "list", "set", "dict")
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 errorCode = {
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
760 "tuple": "M192",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
761 "list": "M193",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
762 "set": "M191",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
763 "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
764 }[node.func.id]
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
765 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
766 type(node.args[0]).__name__.lower(),
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
767 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
768
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
769 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
770 nArgs == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
771 isinstance(node.args[0], ast.ListComp) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
772 node.func.id in ('all', 'any', 'enumerate', 'frozenset',
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
773 'max', 'min', 'sorted', 'sum', 'tuple',)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
774 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
775 self.__error(node.lineno - 1, node.col_offset, "M187",
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
776 node.func.id)
7245
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 elif (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
779 nArgs == 0 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
780 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
781 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
782 node.func.id in ("tuple", "list", "dict")
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
783 ):
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, "M186",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
785 node.func.id)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
786
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
787 elif isinstance(node, ast.Compare) and (
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
788 len(node.ops) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
789 isinstance(node.ops[0], ast.In) and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
790 len(node.comparators) == 1 and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
791 isinstance(node.comparators[0], ast.ListComp)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
792 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
793 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
794
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
795 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
796 """
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
797 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
798 """
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
799 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
800 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
801 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
802 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
803 ast.Set,
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
804 )
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
805 mutableCalls = (
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
806 "Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
807 "OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
808 "collections.Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
809 "collections.OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
810 "collections.defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
811 "collections.deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
812 "defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
813 "deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
814 "dict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
815 "list",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
816 "set",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
817 )
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
818 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
819 "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
820 "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
821 )
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
822 functionDefs = [ast.FunctionDef]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
823 try:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
824 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
825 except AttributeError:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
826 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
827
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
828 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
829 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
830 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
831 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
832 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
833 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
834 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
835 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
836 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
837 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
838 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
839 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
840 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
841 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
842 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
843 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
844 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
845 "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
846 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
847 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
848 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
849 "M822", typeName)
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
850 else:
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
851 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
852 default.col_offset,
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
853 "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
854
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
855 def __dictShouldBeChecked(self, node):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
856 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
857 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
858
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
859 @param node reference to the AST node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
860 @return flag indicating to check the node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
861 @rtype bool
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
862 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
863 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
864 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
865
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
866 if "__IGNORE_WARNING__" in self.__source[node.lineno - 1] or \
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
867 "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1]:
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
868 return False
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
869
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
870 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
871 return len(lineNumbers) == len(set(lineNumbers))
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
872
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
873 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
874 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
875 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
876 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
877 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
878 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
879 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
880 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
881 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
882 "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
883
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
884 def __checkLogging(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
885 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
886 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
887 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
888 visitor = LoggingVisitor()
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
889 visitor.visit(self.__tree)
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
890 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
891 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
892
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
893 def __checkGettext(self):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
894 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
895 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
896 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
897 for node in ast.walk(self.__tree):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
898 if isinstance(node, ast.ImportFrom) and \
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
899 any(name.asname == '_' for name in node.names):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
900 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
901 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
902
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
903 def __checkBugBear(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
904 """
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
905 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
906 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
907 visitor = BugBearVisitor()
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
908 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
909 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
910 node = violation[0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
911 reason = violation[1]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
912 params = violation[2:]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
913 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
914
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
915 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
916 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
917 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
918 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
919 visitor = ReturnVisitor()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
920 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
921 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
922 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
923 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
924 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
925
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
926 def __checkDateTime(self):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
927 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
928 Private method to check use of naive datetime functions.
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
929 """
7249
0bf517e60f54 Fixed some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7245
diff changeset
930 if sys.version_info[0] >= 3:
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
931 # this check is only performed for Python 3
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
932
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
933 # step 1: generate an augmented node tree containing parent info
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
934 # for each child node
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
935 tree = self.__generateTree()
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
936 for node in ast.walk(tree):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
937 for childNode in ast.iter_child_nodes(node):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
938 childNode._dtCheckerParent = node
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
939
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
940 # step 2: perform checks and report issues
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
941 visitor = DateTimeVisitor()
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
942 visitor.visit(tree)
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
943 for violation in visitor.violations:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
944 node = violation[0]
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
945 reason = violation[1]
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
946 self.__error(node.lineno - 1, node.col_offset, reason)
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
947
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
948 def __checkSysVersion(self):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057