src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py

Sat, 31 Dec 2022 16:23:21 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Dec 2022 16:23:21 +0100
branch
eric7
changeset 9653
e67609152c5e
parent 9595
2bd590c40309
child 10048
df836ff707fd
permissions
-rw-r--r--

Updated copyright for 2023.

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
9653
e67609152c5e Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9595
diff changeset
3 # Copyright (c) 2015 - 2023 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
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
10 import ast
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
11 import builtins
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
12 import contextlib
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
13 import copy
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
14 import itertools
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
15 import re
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import sys
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
17 import tokenize
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9327
diff changeset
18
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
19 from collections import defaultdict, namedtuple
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
20 from keyword import iskeyword
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
21 from string import Formatter
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
23 import AstUtilities
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
24
8168
bdb0258faf42 Code Style Checker: reorganized the code a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8166
diff changeset
25 from .eradicate import Eradicator
7980
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
26 from .MiscellaneousDefaults import MiscellaneousCheckerDefaultArgs
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
27
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
29 def composeCallPath(node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
30 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
31 Generator function to assemble the call path of a given node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
32
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
33 @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
34 @type ast.Node
7988
c4c17121eff8 Updated source code documentation with the new tags.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7980
diff changeset
35 @yield call path components
c4c17121eff8 Updated source code documentation with the new tags.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7980
diff changeset
36 @ytype str
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
37 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
38 if isinstance(node, ast.Attribute):
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8198
diff changeset
39 yield from composeCallPath(node.value)
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
40 yield node.attr
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
41 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
42 yield node.id
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
43
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
44
8207
d359172d11be Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
45 class MiscellaneousChecker:
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 Class implementing a checker for miscellaneous checks.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
49
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 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
51 ## Coding line
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
52 "M101",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
53 "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
54 ## Copyright
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
55 "M111",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
56 "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
57 ## Shadowed Builtins
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
58 "M131",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
59 "M132",
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
60 ## Comprehensions
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
61 "M181",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
62 "M182",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
63 "M183",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
64 "M184",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
65 "M185",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
66 "M186",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
67 "M187",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
68 "M188",
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
69 "M189",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
70 "M191",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
71 "M192",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
72 "M193",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
73 "M195",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
74 "M196",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
75 "M197",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
76 "M198",
7042
2be5b245e1b8 Code Style Checker: added check for line continuation methods not suggested by PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7040
diff changeset
77 ## 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
78 "M201",
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
79 ## Naive datetime usage
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
80 "M301",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
81 "M302",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
82 "M303",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
83 "M304",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
84 "M305",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
85 "M306",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
86 "M307",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
87 "M308",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
88 "M311",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
89 "M312",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
90 "M313",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
91 "M314",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
92 "M315",
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
93 "M321",
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
94 ## sys.version and sys.version_info usage
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
95 "M401",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
96 "M402",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
97 "M403",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
98 "M411",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
99 "M412",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
100 "M413",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
101 "M414",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
102 "M421",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
103 "M422",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
104 "M423",
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
105 ## Bugbear
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
106 "M501",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
107 "M502",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
108 "M503",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
109 "M504",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
110 "M505",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
111 "M507",
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
112 "M509",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
113 "M510",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
114 "M511",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
115 "M512",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
116 "M513",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
117 "M514",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
118 "M515",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
119 "M516",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
120 "M517",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
121 "M518",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
122 "M519",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
123 "M520",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
124 "M521",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
125 "M522",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
126 "M523",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
127 "M524",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
128 "M525",
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
129 "M526",
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
130 "M527",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
131 ## Bugbear++
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
132 "M581",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
133 "M582",
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
134 ## Format Strings
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
135 "M601",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
136 "M611",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
137 "M612",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
138 "M613",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
139 "M621",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
140 "M622",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
141 "M623",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
142 "M624",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
143 "M625",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
144 "M631",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
145 "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
146 ## Logging
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
147 "M651",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
148 "M652",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
149 "M653",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
150 "M654",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
151 "M655",
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
152 ## Future statements
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
153 "M701",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
154 "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
155 ## Gettext
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
156 "M711",
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
157 ## print
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 "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
159 ## one element tuple
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
160 "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
161 ## Mutable Defaults
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
162 "M821",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
163 "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
164 ## return statements
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
165 "M831",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
166 "M832",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
167 "M833",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
168 "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
169 ## 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
170 "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
171 ## 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
172 "M891",
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 ]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
174
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
175 Formatter = Formatter()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
176 FormatFieldRegex = re.compile(r"^((?:\s|.)*?)(\..*|\[.*\])?$")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
177
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
178 BuiltinsWhiteList = [
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
179 "__name__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
180 "__doc__",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
181 "credits",
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
182 ]
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
184 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
187
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
188 @param source source code to be checked
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
189 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
190 @param filename name of the source file
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
191 @type str
8198
1c765dc90c21 Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8168
diff changeset
192 @param tree AST tree of the source code
1c765dc90c21 Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8168
diff changeset
193 @type ast.Module
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
194 @param select list of selected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
195 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
196 @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
197 @type list of str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
198 @param expected list of expected codes
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
199 @type list of str
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 @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
201 @type bool
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
202 @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
203 @type dict
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 self.__select = tuple(select)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
206 self.__ignore = ("",) if select else tuple(ignore)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 self.__expected = expected[:]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 self.__repeat = repeat
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 self.__filename = filename
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 self.__source = source[:]
8198
1c765dc90c21 Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8168
diff changeset
211 self.__tree = copy.deepcopy(tree)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 self.__args = args
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
213
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 self.__pep3101FormatRegex = re.compile(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
215 r'^(?:[^\'"]*[\'"][^\'"]*[\'"])*\s*%|^\s*%'
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
216 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
217
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
218 self.__builtins = [b for b in dir(builtins) if b not in self.BuiltinsWhiteList]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
219
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
220 self.__eradicator = Eradicator()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
221
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 # statistics counters
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 self.counters = {}
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
224
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 # collection of detected errors
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 self.errors = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
227
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 checkersWithCodes = [
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 (self.__checkCoding, ("M101", "M102")),
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 (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
231 (self.__checkBuiltins, ("M131", "M132")),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
232 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
233 self.__checkComprehensions,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
234 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
235 "M181",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
236 "M182",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
237 "M183",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
238 "M184",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
239 "M185",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
240 "M186",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
241 "M187",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
242 "M188",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
243 "M189",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
244 "M191",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
245 "M192",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
246 "M193",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
247 "M195",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
248 "M196",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
249 "M197",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
250 "M198",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
251 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
252 ),
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
253 (self.__checkDictWithSortedKeys, ("M201",)),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
254 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
255 self.__checkDateTime,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
256 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
257 "M301",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
258 "M302",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
259 "M303",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
260 "M304",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
261 "M305",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
262 "M306",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
263 "M307",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
264 "M308",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
265 "M311",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
266 "M312",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
267 "M313",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
268 "M314",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
269 "M315",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
270 "M321",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
271 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
272 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
273 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
274 self.__checkSysVersion,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
275 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
276 "M401",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
277 "M402",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
278 "M403",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
279 "M411",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
280 "M412",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
281 "M413",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
282 "M414",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
283 "M421",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
284 "M422",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
285 "M423",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
286 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
287 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
288 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
289 self.__checkBugBear,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
290 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
291 "M501",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
292 "M502",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
293 "M503",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
294 "M504",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
295 "M505",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
296 "M507",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
297 "M509",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
298 "M510",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
299 "M511",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
300 "M512",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
301 "M513",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
302 "M514",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
303 "M515",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
304 "M516",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
305 "M517",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
306 "M518",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
307 "M519",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
308 "M520",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
309 "M521",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
310 "M522",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
311 "M523",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
312 "M524",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
313 "M525",
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
314 "M526",
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
315 "M527",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
316 "M581",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
317 "M582",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
318 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
319 ),
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
320 (self.__checkPep3101, ("M601",)),
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
321 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
322 self.__checkFormatString,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
323 (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
324 "M611",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
325 "M612",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
326 "M613",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
327 "M621",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
328 "M622",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
329 "M623",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
330 "M624",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
331 "M625",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
332 "M631",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
333 "M632",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
334 ),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
335 ),
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
336 (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
337 (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
338 (self.__checkGettext, ("M711",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 (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
340 (self.__checkTuple, ("M811",)),
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
341 (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
342 (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
343 (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
344 (self.__checkCommentedCode, ("M891",)),
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 ]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
346
7980
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
347 # the eradicate whitelist
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
348 commentedCodeCheckerArgs = self.__args.get(
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
349 "CommentedCodeChecker",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
350 MiscellaneousCheckerDefaultArgs["CommentedCodeChecker"],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
351 )
7980
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
352 commentedCodeCheckerWhitelist = commentedCodeCheckerArgs.get(
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
353 "WhiteList",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
354 MiscellaneousCheckerDefaultArgs["CommentedCodeChecker"]["WhiteList"],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
355 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
356 self.__eradicator.update_whitelist(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
357 commentedCodeCheckerWhitelist, extend_default=False
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
358 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
359
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 self.__checkers = []
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 for checker, codes in checkersWithCodes:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
362 if any(not (code and self.__ignoreCode(code)) for code in codes):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 self.__checkers.append(checker)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
364
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 def __ignoreCode(self, code):
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 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
368
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
369 @param code message code to check for
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
370 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
371 @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
372 @rtype bool
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
374 return code.startswith(self.__ignore) and not code.startswith(self.__select)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
375
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 def __error(self, lineNumber, offset, code, *args):
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 Private method to record an issue.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
379
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
380 @param lineNumber line number of the issue
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
381 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
382 @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
383 @type int
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
384 @param code message code
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
385 @type str
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
386 @param args arguments for the message
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
387 @type list
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 if self.__ignoreCode(code):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
391
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if code in self.counters:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 self.counters[code] += 1
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 self.counters[code] = 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
396
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 # Don't care about expected codes
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 if code in self.__expected:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
400
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 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
402 # 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
403 self.errors.append(
7610
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
404 {
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
405 "file": self.__filename,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
406 "line": lineNumber + 1,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
407 "offset": offset,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
408 "code": code,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
409 "args": args,
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
410 }
df7025fe26a3 Code Style Checker: reworked the API between frontend and backend to get some more flexibility for the future.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
411 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
412
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 def run(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 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
416 conditions.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 if not self.__filename:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 # 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
420 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
421
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 if not self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 # 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
424 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
425
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 for check in self.__checkers:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 check()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
428
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
429 def __getCoding(self):
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
430 """
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
431 Private method to get the defined coding of the source.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
432
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
433 @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
434 @rtype tuple of int and str
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
435 """
6106
921d070cec82 Finished implementing support for EditorConfig.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6100
diff changeset
436 for lineno, line in enumerate(self.__source[:5]):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
437 matched = re.search(r"coding[:=]\s*([-\w_.]+)", line, re.IGNORECASE)
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
438 if matched:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
439 return lineno, matched.group(1)
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
440 else:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
441 return 0, ""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
442
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 def __checkCoding(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 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
446 encodings.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 if len(self.__source) == 0:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
450
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
451 encodings = [
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
452 e.lower().strip()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
453 for e in self.__args.get(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
454 "CodingChecker", MiscellaneousCheckerDefaultArgs["CodingChecker"]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
455 ).split(",")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
456 ]
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
457 lineno, coding = self.__getCoding()
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
458 if coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
459 if coding.lower() not in encodings:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
460 self.__error(lineno, 0, "M102", coding)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 else:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 self.__error(0, 0, "M101")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
463
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 def __checkCopyright(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 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
467 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 source = "".join(self.__source)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 copyrightArgs = self.__args.get(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
470 "CopyrightChecker", MiscellaneousCheckerDefaultArgs["CopyrightChecker"]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
471 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 copyrightMinFileSize = copyrightArgs.get(
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 "MinFilesize",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
474 MiscellaneousCheckerDefaultArgs["CopyrightChecker"]["MinFilesize"],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
475 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 copyrightAuthor = copyrightArgs.get(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
477 "Author", MiscellaneousCheckerDefaultArgs["CopyrightChecker"]["Author"]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
478 )
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
479 copyrightRegexStr = (
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 r"Copyright\s+(\(C\)\s+)?(\d{{4}}\s+-\s+)?\d{{4}}\s+{author}"
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
481 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
482
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 tocheck = max(1024, copyrightMinFileSize)
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 topOfSource = source[:tocheck]
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 if len(topOfSource) < copyrightMinFileSize:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 return
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
488 copyrightRe = re.compile(copyrightRegexStr.format(author=r".*"), re.IGNORECASE)
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 if not copyrightRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 self.__error(0, 0, "M111")
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
492
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 if copyrightAuthor:
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 copyrightAuthorRe = re.compile(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
495 copyrightRegexStr.format(author=copyrightAuthor), re.IGNORECASE
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
496 )
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 if not copyrightAuthorRe.search(topOfSource):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 self.__error(0, 0, "M112")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
499
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
500 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
501 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
502 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
503 """
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
504 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
505 commentedCodeCheckerArgs = self.__args.get(
7980
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7972
diff changeset
506 "CommentedCodeChecker",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
507 MiscellaneousCheckerDefaultArgs["CommentedCodeChecker"],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
508 )
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
509 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
510 "Aggressive",
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
511 MiscellaneousCheckerDefaultArgs["CommentedCodeChecker"]["Aggressive"],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
512 )
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
513 for markedLine in self.__eradicator.commented_out_code_line_numbers(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
514 source, aggressive=aggressive
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
515 ):
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7021
diff changeset
516 self.__error(markedLine - 1, 0, "M891")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
517
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
518 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
519 """
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
520 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
521 """
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
522 # 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
523 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
524 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
525 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
526 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
527 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
528 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
529 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
530 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
531 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
532 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
533 stripped[lineno - 1] = withoutComment.rstrip()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
534
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
535 # 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
536 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
537 strippedLine = line.strip()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
538 if strippedLine.endswith("\\") and not strippedLine.startswith(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
539 ("assert", "with")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
540 ):
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
541 self.__error(lineIndex, len(line), "M841")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
542
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 def __checkPrintStatements(self):
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 Private method to check for print statements.
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 """
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 for node in ast.walk(self.__tree):
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
548 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
549 isinstance(node, ast.Call) and getattr(node.func, "id", None) == "print"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
550 ) or (hasattr(ast, "Print") and isinstance(node, ast.Print)):
4506
57666e501a42 Started to add more code style checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 self.__error(node.lineno - 1, node.col_offset, "M801")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
552
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
553 def __checkTuple(self):
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
554 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
555 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
556 """
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
557 for node in ast.walk(self.__tree):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
558 if isinstance(node, ast.Tuple) and len(node.elts) == 1:
4507
1a5bc1ac7c2e Added a check for one element tuples.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4506
diff changeset
559 self.__error(node.lineno - 1, node.col_offset, "M811")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
560
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
561 def __checkFuture(self):
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
562 """
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
563 Private method to check the __future__ imports.
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
564 """
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
565 expectedImports = {
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
566 i.strip()
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
567 for i in self.__args.get("FutureChecker", "").split(",")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
568 if bool(i.strip())
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
569 }
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
570 if len(expectedImports) == 0:
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
571 # 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
572 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
573
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
574 imports = set()
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
575 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
576 hasCode = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
577
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
578 for node in ast.walk(self.__tree):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
579 if isinstance(node, ast.ImportFrom) and 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
580 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
581 elif isinstance(node, ast.Expr):
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
582 if not AstUtilities.isString(node.value):
4540
45627d092846 Fixed the futures checker raising false psitives when a file only contains a docstring.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4515
diff changeset
583 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
584 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
585 elif not (AstUtilities.isString(node) or isinstance(node, ast.Module)):
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
586 hasCode = True
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
587 break
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
588
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
589 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
590 return
4508
a3b38825acf0 Added a check for __future__ imports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4507
diff changeset
591
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8198
diff changeset
592 if imports < expectedImports:
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
593 if imports:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
594 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
595 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
596 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
597 "M701",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
598 ", ".join(expectedImports),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
599 ", ".join(imports),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
600 )
4509
7797ee4a45f9 Redid the future imports checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4508
diff changeset
601 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
602 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
603 node.lineno - 1, node.col_offset, "M702", ", ".join(expectedImports)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
604 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
605
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
606 def __checkPep3101(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
607 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
608 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
609 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
610 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
611 match = self.__pep3101FormatRegex.search(line)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
612 if match:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
613 lineLen = len(line)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
614 pos = line.find("%")
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
615 formatPos = pos
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
616 formatter = "%"
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
617 if line[pos + 1] == "(":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
618 pos = line.find(")", pos)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
619 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
620 while c not in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
621 pos += 1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
622 if pos >= lineLen:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
623 break
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
624 c = line[pos]
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
625 if c in "diouxXeEfFgGcrs":
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
626 formatter += c
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
627 self.__error(lineno, formatPos, "M601", formatter)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
628
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
629 def __checkFormatString(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
630 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
631 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
632 """
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
633 coding = self.__getCoding()[1]
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
634 if not coding:
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
635 # default to utf-8
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
636 coding = "utf-8"
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
637
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
638 visitor = TextVisitor()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
639 visitor.visit(self.__tree)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
640 for node in visitor.nodes:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
641 text = node.s
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
642 if isinstance(text, bytes):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
643 try:
4515
d7cebe39ffba Some refinements to the miscellaneous checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4511
diff changeset
644 text = text.decode(coding)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
645 except UnicodeDecodeError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
646 continue
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
647 fields, implicit, explicit = self.__getFields(text)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
648 if implicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
649 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
650 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
651 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
652 if node.is_docstring:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
653 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
654 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
655 self.__error(node.lineno - 1, node.col_offset, "M613")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
656
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
657 if node in visitor.calls:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
658 call, strArgs = visitor.calls[node]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
659
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
660 numbers = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
661 names = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
662 # 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
663 for name in fields:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
664 fieldMatch = self.FormatFieldRegex.match(name)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
665 try:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
666 number = int(fieldMatch.group(1))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
667 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
668 number = -1
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
669 # negative numbers are considered keywords
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
670 if number >= 0:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
671 numbers.add(number)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
672 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
673 names.add(fieldMatch.group(1))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
674
5588
6ba512d9f46a Continued fixing code style issues detected by the extended style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5585
diff changeset
675 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
676 numArgs = len(call.args)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
677 if strArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
678 numArgs -= 1
7894
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
679 hasKwArgs = any(kw.arg is None for kw in call.keywords)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
680 hasStarArgs = sum(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
681 1 for arg in call.args if isinstance(arg, ast.Starred)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
682 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
683
7894
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
684 if hasKwArgs:
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
685 keywords.discard(None)
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
686 if hasStarArgs:
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
687 numArgs -= 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
688
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
689 # 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
690 # parameters but at least check if the args are used
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
691 if hasKwArgs and not names:
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
692 # No names but kwargs
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
693 self.__error(call.lineno - 1, call.col_offset, "M623")
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
694 if hasStarArgs and not numbers:
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
695 # No numbers but args
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
696 self.__error(call.lineno - 1, call.col_offset, "M624")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
697
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
698 if not hasKwArgs and not hasStarArgs:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
699 # can actually verify numbers and names
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
700 for number in sorted(numbers):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
701 if number >= numArgs:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
702 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
703 call.lineno - 1, call.col_offset, "M621", number
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
704 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
705
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
706 for name in sorted(names):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
707 if name not in keywords:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
708 self.__error(call.lineno - 1, call.col_offset, "M622", name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
709
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
710 for arg in range(numArgs):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
711 if arg not in numbers:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
712 self.__error(call.lineno - 1, call.col_offset, "M631", arg)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
713
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
714 for keyword in keywords:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
715 if keyword not in names:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
716 self.__error(call.lineno - 1, call.col_offset, "M632", keyword)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
717
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
718 if implicit and explicit:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
719 self.__error(call.lineno - 1, call.col_offset, "M625")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
720
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
721 def __getFields(self, string):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
722 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
723 Private method to extract the format field information.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
724
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
725 @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
726 @type str
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
727 @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
728 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
729 @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
730 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
731 fields = set()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
732 cnt = itertools.count()
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
733 implicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
734 explicit = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
735 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
736 for _literal, field, spec, conv in self.Formatter.parse(string):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
737 if field is not None and (conv is None or conv in "rsa"):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
738 if not field:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
739 field = str(next(cnt))
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
740 implicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
741 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
742 explicit = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
743 fields.add(field)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
744 fields.update(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
745 parsedSpec[1]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
746 for parsedSpec in self.Formatter.parse(spec)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
747 if parsedSpec[1] is not None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
748 )
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
749 except ValueError:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
750 return set(), False, False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
751 else:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
752 return fields, implicit, explicit
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
753
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
754 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
755 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
756 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
757 """
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
758 functionDefs = [ast.FunctionDef]
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8228
diff changeset
759 with contextlib.suppress(AttributeError):
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
760 functionDefs.append(ast.AsyncFunctionDef)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
761
5619
ab999dc48132 Made the built-ins checker configurable.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5617
diff changeset
762 ignoreBuiltinAssignments = self.__args.get(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
763 "BuiltinsChecker", MiscellaneousCheckerDefaultArgs["BuiltinsChecker"]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
764 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
765
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
766 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
767 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
768 # assign statement
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
769 for element in node.targets:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
770 if isinstance(element, ast.Name) and 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
771 value = node.value
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
772 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
773 isinstance(value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
774 and element.id in ignoreBuiltinAssignments
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
775 and value.id in ignoreBuiltinAssignments[element.id]
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
776 ):
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
777 # 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
778 continue
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
779 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
780 element.lineno - 1, element.col_offset, "M131", element.id
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
781 )
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
782 elif isinstance(element, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
783 for tupleElement in element.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
784 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
785 isinstance(tupleElement, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
786 and tupleElement.id in self.__builtins
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
787 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
788 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
789 tupleElement.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
790 tupleElement.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
791 "M131",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
792 tupleElement.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
793 )
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
794 elif isinstance(node, ast.For):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
795 # for loop
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
796 target = node.target
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
797 if isinstance(target, ast.Name) and target.id in self.__builtins:
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
798 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
799 target.lineno - 1, target.col_offset, "M131", target.id
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
800 )
5589
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
801 elif isinstance(target, (ast.Tuple, ast.List)):
ad8f37c6a306 Fixed code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5588
diff changeset
802 for element in target.elts:
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
803 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
804 isinstance(element, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
805 and element.id in self.__builtins
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
806 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
807 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
808 element.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
809 element.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
810 "M131",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
811 element.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
812 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
813 elif any(isinstance(node, functionDef) for functionDef in functionDefs):
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
814 # (asynchronous) function definition
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
815 for arg in node.args.args:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
816 if isinstance(arg, ast.arg) and arg.arg in self.__builtins:
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
817 self.__error(arg.lineno - 1, arg.col_offset, "M132", arg.arg)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
818
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
819 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
820 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
821 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
822 """
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
823 for node in ast.walk(self.__tree):
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
824 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
825 nArgs = len(node.args)
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
826 nKwArgs = len(node.keywords)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
827
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
828 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
829 nArgs == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
830 and isinstance(node.args[0], ast.GeneratorExp)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
831 and node.func.id in ("list", "set")
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
832 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
833 errorCode = {
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
834 "list": "M181",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
835 "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
836 }[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
837 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
838
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
839 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
840 nArgs == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
841 and isinstance(node.args[0], (ast.GeneratorExp, ast.ListComp))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
842 and isinstance(node.args[0].elt, ast.Tuple)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
843 and len(node.args[0].elt.elts) == 2
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
844 and node.func.id == "dict"
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
845 ):
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
846 if isinstance(node.args[0], ast.GeneratorExp):
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
847 errorCode = "M183"
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
848 else:
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
849 errorCode = "M185"
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
850 self.__error(node.lineno - 1, node.col_offset, errorCode)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
851
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
852 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
853 nArgs == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
854 and isinstance(node.args[0], ast.ListComp)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
855 and node.func.id in ("list", "set")
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
856 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
857 errorCode = {
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
858 "list": "M195",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
859 "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
860 }[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
861 self.__error(node.lineno - 1, node.col_offset, errorCode)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
862
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
863 elif nArgs == 1 and (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
864 isinstance(node.args[0], ast.Tuple)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
865 and node.func.id == "tuple"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
866 or isinstance(node.args[0], ast.List)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
867 and node.func.id == "list"
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
868 ):
5585
dab20c39f08c Added checks for shadowed Python builtins and unneccessary comprehensions and generators.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
869 errorCode = {
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
870 "tuple": "M197",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
871 "list": "M198",
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
872 }[node.func.id]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
873 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
874 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
875 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
876 errorCode,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
877 type(node.args[0]).__name__.lower(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
878 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
879 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
880
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
881 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
882 nArgs == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
883 and isinstance(node.args[0], (ast.Tuple, ast.List))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
884 and node.func.id in ("tuple", "list", "set", "dict")
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
885 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
886 errorCode = {
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
887 "tuple": "M192",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
888 "list": "M193",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
889 "set": "M191",
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
890 "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
891 }[node.func.id]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
892 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
893 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
894 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
895 errorCode,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
896 type(node.args[0]).__name__.lower(),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
897 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
898 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
899
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
900 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
901 nArgs == 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
902 and not any(isinstance(a, ast.Starred) for a in node.args)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
903 and not any(k.arg is None for k in node.keywords)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
904 and node.func.id == "dict"
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
905 ) or (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
906 nArgs == 0 and nKwArgs == 0 and node.func.id in ("tuple", "list")
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
907 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
908 self.__error(node.lineno - 1, node.col_offset, "M186", node.func.id)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
909
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
910 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
911 node.func.id in {"list", "reversed"}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
912 and nArgs > 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
913 and isinstance(node.args[0], ast.Call)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
914 and isinstance(node.args[0].func, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
915 and node.args[0].func.id == "sorted"
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
916 ):
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
917 if node.func.id == "reversed":
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
918 reverseFlagValue = False
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
919 for kw in node.args[0].keywords:
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
920 if kw.arg != "reverse":
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
921 continue
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
922 if isinstance(kw.value, ast.NameConstant):
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
923 reverseFlagValue = kw.value.value
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
924 elif isinstance(kw.value, ast.Num):
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
925 reverseFlagValue = bool(kw.value.n)
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
926 else:
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
927 # Complex value
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
928 reverseFlagValue = None
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
929
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
930 if reverseFlagValue is None:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
931 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
932 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
933 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
934 "M187a",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
935 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
936 node.args[0].func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
937 )
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
938 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
939 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
940 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
941 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
942 "M187b",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
943 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
944 node.args[0].func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
945 not reverseFlagValue,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
946 )
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
947 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
948 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
949 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
950 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
951 "M187c",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
952 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
953 node.args[0].func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
954 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
955
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
956 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
957 nArgs > 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
958 and isinstance(node.args[0], ast.Call)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
959 and isinstance(node.args[0].func, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
960 and (
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
961 (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
962 node.func.id in {"set", "sorted"}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
963 and node.args[0].func.id
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
964 in {"list", "reversed", "sorted", "tuple"}
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
965 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
966 or (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
967 node.func.id in {"list", "tuple"}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
968 and node.args[0].func.id in {"list", "tuple"}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
969 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
970 or (node.func.id == "set" and node.args[0].func.id == "set")
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
971 )
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
972 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
973 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
974 node.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
975 node.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
976 "M188",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
977 node.args[0].func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
978 node.func.id,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
979 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
980
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
981 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
982 node.func.id in {"reversed", "set", "sorted"}
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
983 and nArgs > 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
984 and isinstance(node.args[0], ast.Subscript)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
985 and isinstance(node.args[0].slice, ast.Slice)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
986 and node.args[0].slice.lower is None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
987 and node.args[0].slice.upper is None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
988 and isinstance(node.args[0].slice.step, ast.UnaryOp)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
989 and isinstance(node.args[0].slice.step.op, ast.USub)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
990 and isinstance(node.args[0].slice.step.operand, ast.Num)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
991 and node.args[0].slice.step.operand.n == 1
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
992 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
993 self.__error(node.lineno - 1, node.col_offset, "M189", node.func.id)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
994
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
995 elif isinstance(node, (ast.ListComp, ast.SetComp)) and (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
996 len(node.generators) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
997 and not node.generators[0].ifs
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
998 and not node.generators[0].is_async
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
999 and (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1000 isinstance(node.elt, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1001 and isinstance(node.generators[0].target, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1002 and node.elt.id == node.generators[0].target.id
8759
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1003 )
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1004 ):
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1005 compType = {
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1006 ast.DictComp: "dict",
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1007 ast.ListComp: "list",
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1008 ast.SetComp: "set",
7efebdfa5dc2 Updated the comprehensions check of the Miscellaneous code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
1009 }[node.__class__]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1010
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1011 self.__error(node.lineno - 1, node.col_offset, "M196", compType)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1012
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
1013 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
1014 """
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
1015 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
1016 """
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1017 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
1018 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
1019 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
1020 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
1021 ast.Set,
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1022 ast.DictComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1023 ast.ListComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1024 ast.SetComp,
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1025 )
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1026 mutableCalls = (
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1027 "Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1028 "OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1029 "collections.Counter",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1030 "collections.OrderedDict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1031 "collections.defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1032 "collections.deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1033 "defaultdict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1034 "deque",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1035 "dict",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1036 "list",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1037 "set",
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1038 )
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
1039 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
1040 "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
1041 "frozenset",
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1042 "types.MappingProxyType",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1043 "MappingProxyType",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1044 "re.compile",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1045 "operator.attrgetter",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1046 "operator.itemgetter",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1047 "operator.methodcaller",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1048 "attrgetter",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1049 "itemgetter",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1050 "methodcaller",
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
1051 )
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1052 functionDefs = [ast.FunctionDef]
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8228
diff changeset
1053 with contextlib.suppress(AttributeError):
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1054 functionDefs.append(ast.AsyncFunctionDef)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1055
5621
80cce672d505 Added a checker for using mutable types as default functiona rgument (M821).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5619
diff changeset
1056 for node in ast.walk(self.__tree):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1057 if any(isinstance(node, functionDef) 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
1058 defaults = node.args.defaults[:]
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8228
diff changeset
1059 with contextlib.suppress(AttributeError):
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
1060 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
1061 for default in defaults:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1062 if any(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1063 isinstance(default, mutableType) for mutableType in mutableTypes
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1064 ):
5703
7650c11cc5f2 MiscChecker, ast: No special case for Python 2 needed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5649
diff changeset
1065 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
1066 if isinstance(default, ast.Call):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1067 callPath = ".".join(composeCallPath(default.func))
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1068 if callPath in mutableCalls:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1069 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1070 default.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1071 default.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1072 "M823",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1073 callPath + "()",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1074 )
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
1075 elif callPath not in immutableCalls:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1076 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1077 default.lineno - 1,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1078 default.col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1079 "M822",
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1080 typeName,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1081 )
5639
9ee960bceb51 Refined the "mutable default arguments" checker a little bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5621
diff changeset
1082 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1083 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1084 default.lineno - 1, default.col_offset, "M821", typeName
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1085 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1086
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1087 def __dictShouldBeChecked(self, node):
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1088 """
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1089 Private function to test, if the node should be checked.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1090
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1091 @param node reference to the AST node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1092 @return flag indicating to check the node
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1093 @rtype bool
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1094 """
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1095 if not all(AstUtilities.isString(key) for key in node.keys):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1096 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1097
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1098 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1099 "__IGNORE_WARNING__" in self.__source[node.lineno - 1]
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1100 or "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1]
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1101 ):
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1102 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1103
6178
905ea208884a Some refinements of the last changeset.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6177
diff changeset
1104 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
1105 return len(lineNumbers) == len(set(lineNumbers))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1106
6177
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
1107 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
1108 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
1109 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
1110 """
af76e795c4ce Code Style Checker: added check for dictionaries with sorted keys (M201)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6106
diff changeset
1111 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
1112 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
1113 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
1114 if key2.s < key1.s:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1115 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1116 key2.lineno - 1, key2.col_offset, "M201", key2.s, key1.s
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1117 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1118
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1119 def __checkLogging(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1120 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1121 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
1122 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1123 visitor = LoggingVisitor()
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1124 visitor.visit(self.__tree)
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1125 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
1126 self.__error(node.lineno - 1, node.col_offset, reason)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1127
6182
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
1128 def __checkGettext(self):
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
1129 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
1130 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
1131 """
f293e95b914d Code Style Checker: added check for the 'gettext' import statement
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6180
diff changeset
1132 for node in ast.walk(self.__tree):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1133 if isinstance(node, ast.ImportFrom) and any(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1134 name.asname == "_" for name in node.names
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1135 ):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1136 self.__error(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1137 node.lineno - 1, node.col_offset, "M711", node.names[0].name
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1138 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1139
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1140 def __checkBugBear(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1141 """
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
1142 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
1143 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1144 visitor = BugBearVisitor()
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1145 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
1146 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
1147 node = violation[0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1148 reason = violation[1]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1149 params = violation[2:]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1150 self.__error(node.lineno - 1, node.col_offset, reason, *params)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1151
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
1152 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
1153 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
1154 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
1155 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
1156 visitor = ReturnVisitor()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
1157 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
1158 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
1159 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
1160 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
1161 self.__error(node.lineno - 1, node.col_offset, reason)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1162
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
1163 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
1164 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
1165 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
1166 """
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1167 # step 1: generate an augmented node tree containing parent info
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1168 # for each child node
8198
1c765dc90c21 Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8168
diff changeset
1169 tree = copy.deepcopy(self.__tree)
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1170 for node in ast.walk(tree):
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1171 for childNode in ast.iter_child_nodes(node):
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1172 childNode._dtCheckerParent = node
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1173
7637
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1174 # step 2: perform checks and report issues
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1175 visitor = DateTimeVisitor()
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1176 visitor.visit(tree)
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1177 for violation in visitor.violations:
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1178 node = violation[0]
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1179 reason = violation[1]
c878e8255972 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7610
diff changeset
1180 self.__error(node.lineno - 1, node.col_offset, reason)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1181
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1182 def __checkSysVersion(self):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1183 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1184 Private method to check the use of sys.version and sys.version_info.
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1185 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1186 visitor = SysVersionVisitor()
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1187 visitor.visit(self.__tree)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1188 for violation in visitor.violations:
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1189 node = violation[0]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1190 reason = violation[1]
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
1191 self.__error(node.lineno - 1, node.col_offset, reason)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1192
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1193
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1194 class TextVisitor(ast.NodeVisitor):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1195 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1196 Class implementing a node visitor for bytes and str instances.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1197
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1198 It tries to detect docstrings as string of the first expression of each
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1199 module, class or function.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1200 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1201
9274
86fab0c74430 Added some TODO markers to update some checker code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
1202 # modeled after the string format flake8 extension
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1203
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1204 def __init__(self):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1205 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1206 Constructor
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1207 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1208 super().__init__()
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1209 self.nodes = []
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1210 self.calls = {}
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1211
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1212 def __addNode(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1213 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1214 Private method to add a node to our list of nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1215
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1216 @param node reference to the node to add
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1217 @type ast.AST
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1218 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1219 if not hasattr(node, "is_docstring"):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1220 node.is_docstring = False
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1221 self.nodes.append(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1222
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1223 def visit_Str(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1224 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1225 Public method to record a string node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1226
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1227 @param node reference to the string node
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1228 @type ast.Str
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1229 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1230 self.__addNode(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1231
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1232 def visit_Bytes(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1233 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1234 Public method to record a bytes node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1235
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1236 @param node reference to the bytes node
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1237 @type ast.Bytes
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1238 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1239 self.__addNode(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1240
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1241 def visit_Constant(self, node):
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1242 """
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1243 Public method to handle constant nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1244
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1245 @param node reference to the bytes node
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1246 @type ast.Constant
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1247 """
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1248 if sys.version_info >= (3, 8, 0):
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1249 if AstUtilities.isBaseString(node):
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1250 self.__addNode(node)
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1251 else:
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1252 super().generic_visit(node)
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1253 else:
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1254 super().generic_visit(node)
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1255
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1256 def __visitDefinition(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1257 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1258 Private method handling class and function definitions.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1259
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1260 @param node reference to the node to handle
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1261 @type ast.FunctionDef, ast.AsyncFunctionDef or ast.ClassDef
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1262 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1263 # Manually traverse class or function definition
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1264 # * Handle decorators normally
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1265 # * Use special check for body content
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1266 # * Don't handle the rest (e.g. bases)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1267 for decorator in node.decorator_list:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1268 self.visit(decorator)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1269 self.__visitBody(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1270
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1271 def __visitBody(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1272 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1273 Private method to traverse the body of the node manually.
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1274
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1275 If the first node is an expression which contains a string or bytes it
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1276 marks that as a docstring.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1277
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1278 @param node reference to the node to traverse
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1279 @type ast.AST
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1280 """
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1281 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1282 node.body
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1283 and isinstance(node.body[0], ast.Expr)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1284 and AstUtilities.isBaseString(node.body[0].value)
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1285 ):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1286 node.body[0].value.is_docstring = True
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1287
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1288 for subnode in node.body:
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1289 self.visit(subnode)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1290
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1291 def visit_Module(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1292 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1293 Public method to handle a module.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1294
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1295 @param node reference to the node to handle
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1296 @type ast.Module
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1297 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1298 self.__visitBody(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1299
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1300 def visit_ClassDef(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1301 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1302 Public method to handle a class definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1303
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1304 @param node reference to the node to handle
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1305 @type ast.ClassDef
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1306 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1307 # Skipped nodes: ('name', 'bases', 'keywords', 'starargs', 'kwargs')
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1308 self.__visitDefinition(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1309
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1310 def visit_FunctionDef(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1311 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1312 Public method to handle a function definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1313
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1314 @param node reference to the node to handle
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1315 @type ast.FunctionDef
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1316 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1317 # Skipped nodes: ('name', 'args', 'returns')
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1318 self.__visitDefinition(node)
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1319
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1320 def visit_AsyncFunctionDef(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1321 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1322 Public method to handle an asynchronous function definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1323
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1324 @param node reference to the node to handle
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1325 @type ast.AsyncFunctionDef
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1326 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1327 # Skipped nodes: ('name', 'args', 'returns')
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1328 self.__visitDefinition(node)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1329
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1330 def visit_Call(self, node):
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1331 """
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1332 Public method to handle a function call.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1333
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1334 @param node reference to the node to handle
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1335 @type ast.Call
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1336 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1337 if isinstance(node.func, ast.Attribute) and node.func.attr == "format":
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1338 if AstUtilities.isBaseString(node.func.value):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1339 self.calls[node.func.value] = (node, False)
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1340 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1341 isinstance(node.func.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1342 and node.func.value.id == "str"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1343 and node.args
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1344 and AstUtilities.isBaseString(node.args[0])
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
1345 ):
4511
b5e4e7efa904 Added a checker for string format strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4510
diff changeset
1346 self.calls[node.args[0]] = (node, True)
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1347 super().generic_visit(node)
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4540
diff changeset
1348
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1349
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1350 class LoggingVisitor(ast.NodeVisitor):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1351 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1352 Class implementing a node visitor to check logging statements.
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1353 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1354
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1355 LoggingLevels = {
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1356 "debug",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1357 "critical",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1358 "error",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1359 "info",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1360 "warn",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1361 "warning",
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1362 }
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1363
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1364 def __init__(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1365 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1366 Constructor
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1367 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1368 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1369
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1370 self.__currentLoggingCall = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1371 self.__currentLoggingArgument = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1372 self.__currentLoggingLevel = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1373 self.__currentExtraKeyword = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1374 self.violations = []
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1375
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1376 def __withinLoggingStatement(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1377 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1378 Private method to check, if we are inside a logging statement.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1379
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1380 @return flag indicating we are inside a logging statement
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1381 @rtype bool
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1382 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1383 return self.__currentLoggingCall is not None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1384
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1385 def __withinLoggingArgument(self):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1386 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1387 Private method to check, if we are inside a logging argument.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1388
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1389 @return flag indicating we are inside a logging argument
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1390 @rtype bool
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1391 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1392 return self.__currentLoggingArgument is not None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1393
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1394 def __withinExtraKeyword(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1395 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1396 Private method to check, if we are inside the extra keyword.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1397
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1398 @param node reference to the node to be checked
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1399 @type ast.keyword
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1400 @return flag indicating we are inside the extra keyword
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1401 @rtype bool
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1402 """
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1403 return (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1404 self.__currentExtraKeyword is not None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1405 and self.__currentExtraKeyword != node
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1406 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1407
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1408 def __detectLoggingLevel(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1409 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1410 Private method to decide whether an AST Call is a logging call.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1411
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1412 @param node reference to the node to be processed
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1413 @type ast.Call
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1414 @return logging level
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1415 @rtype str or None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1416 """
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8228
diff changeset
1417 with contextlib.suppress(AttributeError):
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1418 if node.func.value.id == "warnings":
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1419 return None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1420
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1421 if node.func.attr in LoggingVisitor.LoggingLevels:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1422 return node.func.attr
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1423
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1424 return None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1425
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1426 def __isFormatCall(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1427 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1428 Private method to check if a function call uses format.
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1429
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1430 @param node reference to the node to be processed
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1431 @type ast.Call
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1432 @return flag indicating the function call uses format
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1433 @rtype bool
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1434 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1435 try:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1436 return node.func.attr == "format"
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1437 except AttributeError:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1438 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1439
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1440 def visit_Call(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1441 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1442 Public method to handle a function call.
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1443
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1444 Every logging statement and string format is expected to be a function
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1445 call.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1446
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1447 @param node reference to the node to be processed
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1448 @type ast.Call
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1449 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1450 # we are in a logging statement
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1451 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1452 self.__withinLoggingStatement()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1453 and self.__withinLoggingArgument()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1454 and self.__isFormatCall(node)
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1455 ):
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1456 self.violations.append((node, "M651"))
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1457 super().generic_visit(node)
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1458 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1459
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1460 loggingLevel = self.__detectLoggingLevel(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1461
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1462 if loggingLevel and self.__currentLoggingLevel is None:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1463 self.__currentLoggingLevel = loggingLevel
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1464
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1465 # we are in some other statement
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1466 if loggingLevel is None:
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1467 super().generic_visit(node)
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1468 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1469
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1470 # we are entering a new logging statement
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1471 self.__currentLoggingCall = node
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1472
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1473 if loggingLevel == "warn":
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1474 self.violations.append((node, "M655"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1475
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1476 for index, child in enumerate(ast.iter_child_nodes(node)):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1477 if index == 1:
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1478 self.__currentLoggingArgument = child
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1479 if index > 1 and isinstance(child, ast.keyword) and child.arg == "extra":
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1480 self.__currentExtraKeyword = child
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1481
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1482 super().visit(child)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1483
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1484 self.__currentLoggingArgument = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1485 self.__currentExtraKeyword = None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1486
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1487 self.__currentLoggingCall = None
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1488 self.__currentLoggingLevel = None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1489
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1490 def visit_BinOp(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1491 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1492 Public method to handle binary operations while processing the first
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1493 logging argument.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1494
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1495 @param node reference to the node to be processed
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1496 @type ast.BinOp
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1497 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1498 if self.__withinLoggingStatement() and self.__withinLoggingArgument():
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1499 # handle percent format
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1500 if isinstance(node.op, ast.Mod):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1501 self.violations.append((node, "M652"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1502
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1503 # handle string concat
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1504 if isinstance(node.op, ast.Add):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1505 self.violations.append((node, "M653"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1506
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1507 super().generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1508
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1509 def visit_JoinedStr(self, node):
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1510 """
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1511 Public method to handle f-string arguments.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1512
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1513 @param node reference to the node to be processed
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1514 @type ast.JoinedStr
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1515 """
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1516 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1517 self.__withinLoggingStatement()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1518 and any(isinstance(i, ast.FormattedValue) for i in node.values)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1519 and self.__withinLoggingArgument()
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1520 ):
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1521 self.violations.append((node, "M654"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1522
8222
5994b80b8760 Applied some more code simplifications suggested by the new Simplify checker (Y102: use single if) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
1523 super().generic_visit(node)
6180
8d72871c16ba Code Style Checker: added checks for logging statements
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6178
diff changeset
1524
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1525
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1526 BugBearContext = namedtuple("BugBearContext", ["node", "stack"])
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1527
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1528
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1529 class BugBearVisitor(ast.NodeVisitor):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1530 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1531 Class implementing a node visitor to check for various topics.
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1532 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1533
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1534 #
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
1535 # This class was implemented along the BugBear flake8 extension (v 22.12.6).
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1536 # Original: Copyright (c) 2016 Łukasz Langa
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1537 #
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1538
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1539 CONTEXTFUL_NODES = (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1540 ast.Module,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1541 ast.ClassDef,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1542 ast.AsyncFunctionDef,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1543 ast.FunctionDef,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1544 ast.Lambda,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1545 ast.ListComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1546 ast.SetComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1547 ast.DictComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1548 ast.GeneratorExp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1549 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1550
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1551 FUNCTION_NODES = (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1552 ast.AsyncFunctionDef,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1553 ast.FunctionDef,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1554 ast.Lambda,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1555 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1556
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1557 NodeWindowSize = 4
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1558
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1559 def __init__(self):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1560 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1561 Constructor
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1562 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1563 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1564
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1565 self.nodeWindow = []
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1566 self.violations = []
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1567 self.contexts = []
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1568
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1569 self.__M523Seen = set()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1570
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1571 @property
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1572 def nodeStack(self):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1573 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1574 Public method to get a reference to the most recent node stack.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1575
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1576 @return reference to the most recent node stack
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1577 @rtype list
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1578 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1579 if len(self.contexts) == 0:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1580 return []
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1581
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1582 context, stack = self.contexts[-1]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1583 return stack
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1584
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1585 def __isIdentifier(self, arg):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1586 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1587 Private method to check if arg is a valid identifier.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1588
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1589 See https://docs.python.org/2/reference/lexical_analysis.html#identifiers
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1590
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1591 @param arg reference to an argument node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1592 @type ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1593 @return flag indicating a valid identifier
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1594 @rtype TYPE
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1595 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1596 if not AstUtilities.isString(arg):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1597 return False
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1598
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1599 return (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1600 re.match(r"^[A-Za-z_][A-Za-z0-9_]*$", AstUtilities.getValue(arg))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1601 is not None
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1602 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1603
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1604 def __composeCallPath(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1605 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1606 Private method get the individual elements of the call path of a node.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1607
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1608 @param node reference to the node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1609 @type ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1610 @yield one element of the call path
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1611 @ytype ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1612 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1613 if isinstance(node, ast.Attribute):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1614 yield from self.__composeCallPath(node.value)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1615 yield node.attr
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1616 elif isinstance(node, ast.Call):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1617 yield from self.__composeCallPath(node.func)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1618 elif isinstance(node, ast.Name):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1619 yield node.id
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1620
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1621 def __toNameStr(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1622 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1623 Private method to turn Name and Attribute nodes to strings, handling any
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1624 depth of attribute accesses.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1625
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1626
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1627 @param node reference to the node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1628 @type ast.Name or ast.Attribute
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1629 @return string representation
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1630 @rtype str
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1631 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1632 if isinstance(node, ast.Name):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1633 return node.id
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1634
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1635 if isinstance(node, ast.Call):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1636 return self.__toNameStr(node.func)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1637
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1638 try:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1639 return self.__toNameStr(node.value) + "." + node.attr
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1640 except AttributeError:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1641 return self.__toNameStr(node.value)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1642
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1643 def __typesafeIssubclass(self, obj, classOrTuple):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1644 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1645 Private method implementing a type safe issubclass() function.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1646
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1647 @param obj reference to the object to be tested
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1648 @type any
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1649 @param classOrTuple type to check against
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1650 @type type
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1651 @return flag indicating a subclass
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1652 @rtype bool
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1653 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1654 try:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1655 return issubclass(obj, classOrTuple)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1656 except TypeError:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1657 # User code specifies a type that is not a type in our current run.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1658 # Might be their error, might be a difference in our environments.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1659 # We don't know so we ignore this.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1660 return False
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1661
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1662 def __getAssignedNames(self, loopNode):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1663 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1664 Private method to get the names of a for loop.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1665
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1666 @param loopNode reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1667 @type ast.For
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1668 @yield DESCRIPTION
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1669 @ytype TYPE
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1670 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1671 loopTargets = (ast.For, ast.AsyncFor, ast.comprehension)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1672 for node in self.__childrenInScope(loopNode):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1673 if isinstance(node, (ast.Assign)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1674 for child in node.targets:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1675 yield from self.__namesFromAssignments(child)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1676 if isinstance(node, loopTargets + (ast.AnnAssign, ast.AugAssign)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1677 yield from self.__namesFromAssignments(node.target)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1678
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1679 def __namesFromAssignments(self, assignTarget):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1680 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1681 Private method to get names of an assignment.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1682
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1683 @param assignTarget reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1684 @type ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1685 @yield name of the assignment
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1686 @ytype str
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1687 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1688 if isinstance(assignTarget, ast.Name):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1689 yield assignTarget.id
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1690 elif isinstance(assignTarget, ast.Starred):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1691 yield from self.__namesFromAssignments(assignTarget.value)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1692 elif isinstance(assignTarget, (ast.List, ast.Tuple)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1693 for child in assignTarget.elts:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1694 yield from self.__namesFromAssignments(child)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1695
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1696 def __childrenInScope(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1697 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1698 Private method to get all child nodes in the given scope.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1699
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1700 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1701 @type ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1702 @yield reference to a child node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1703 @ytype ast.Node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1704 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1705 yield node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1706 if not isinstance(node, BugBearVisitor.FUNCTION_NODES):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1707 for child in ast.iter_child_nodes(node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1708 yield from self.__childrenInScope(child)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1709
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1710 def visit(self, node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1711 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1712 Public method to traverse a given AST node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1713
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1714 @param node AST node to be traversed
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1715 @type ast.Node
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1716 """
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1717 isContextful = isinstance(node, BugBearVisitor.CONTEXTFUL_NODES)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1718
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1719 if isContextful:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1720 context = BugBearContext(node, [])
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1721 self.contexts.append(context)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1722
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1723 self.nodeStack.append(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1724 self.nodeWindow.append(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1725 self.nodeWindow = self.nodeWindow[-BugBearVisitor.NodeWindowSize :]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1726
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
1727 super().visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1728
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1729 self.nodeStack.pop()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1730
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1731 if isContextful:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1732 self.contexts.pop()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1733
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1734 def visit_ExceptHandler(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1735 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1736 Public method to handle exception handlers.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1737
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1738 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1739 @type ast.ExceptHandler
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1740 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1741 redundantExceptions = {
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1742 "OSError": {
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1743 # All of these are actually aliases of OSError since Python 3.3
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1744 "IOError",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1745 "EnvironmentError",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1746 "WindowsError",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1747 "mmap.error",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1748 "socket.error",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1749 "select.error",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1750 },
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1751 "ValueError": {
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1752 "binascii.Error",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1753 },
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1754 }
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1755
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1756 if node.type is None:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1757 # bare except is handled by pycodestyle already
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1758 pass
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1759
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1760 elif isinstance(node.type, ast.Tuple):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1761 names = [self.__toNameStr(e) for e in node.type.elts]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1762 as_ = " as " + node.name if node.name is not None else ""
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1763 if len(names) == 0:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1764 self.violations.append((node, "M501", as_))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1765 elif len(names) == 1:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1766 self.violations.append((node, "M513", *names))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1767 else:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1768 # See if any of the given exception names could be removed, e.g. from:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1769 # (MyError, MyError) # duplicate names
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1770 # (MyError, BaseException) # everything derives from the Base
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1771 # (Exception, TypeError) # builtins where one subclasses another
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1772 # (IOError, OSError) # IOError is an alias of OSError since Python3.3
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1773 # but note that other cases are impractical to handle from the AST.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1774 # We expect this is mostly useful for users who do not have the
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1775 # builtin exception hierarchy memorised, and include a 'shadowed'
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1776 # subtype without realising that it's redundant.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1777 good = sorted(set(names), key=names.index)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1778 if "BaseException" in good:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1779 good = ["BaseException"]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1780 # Remove redundant exceptions that the automatic system either handles
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1781 # poorly (usually aliases) or can't be checked (e.g. it's not an
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1782 # built-in exception).
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1783 for primary, equivalents in redundantExceptions.items():
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1784 if primary in good:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1785 good = [g for g in good if g not in equivalents]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1786
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1787 for name, other in itertools.permutations(tuple(good), 2):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1788 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1789 self.__typesafeIssubclass(
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1790 getattr(builtins, name, type), getattr(builtins, other, ())
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1791 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1792 and name in good
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1793 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1794 good.remove(name)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1795 if good != names:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1796 desc = (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1797 good[0] if len(good) == 1 else "({0})".format(", ".join(good))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1798 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1799 self.violations.append((node, "M514", ", ".join(names), as_, desc))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1800
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1801 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1802
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1803 def visit_UAdd(self, node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1804 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1805 Public method to handle unary additions.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1806
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1807 @param node reference to the node to be processed
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1808 @type ast.UAdd
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1809 """
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1810 trailingNodes = list(map(type, self.nodeWindow[-4:]))
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1811 if trailingNodes == [ast.UnaryOp, ast.UAdd, ast.UnaryOp, ast.UAdd]:
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1812 originator = self.nodeWindow[-4]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1813 self.violations.append((originator, "M502"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1814
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1815 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1816
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1817 def visit_Call(self, node):
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1818 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1819 Public method to handle a function call.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1820
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1821 @param node reference to the node to be processed
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1822 @type ast.Call
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1823 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1824 if isinstance(node.func, ast.Attribute):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1825 self.__checkForM505(node)
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1826 else:
8243
cc717c2ae956 Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8228
diff changeset
1827 with contextlib.suppress(AttributeError, IndexError):
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
1828 # bad super() call
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
1829 if isinstance(node.func, ast.Name) and node.func.id == "super":
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
1830 args = node.args
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
1831 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1832 len(args) == 2
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1833 and isinstance(args[0], ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1834 and isinstance(args[0].value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1835 and args[0].value.id == "self"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1836 and args[0].attr == "__class__"
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
1837 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1838 self.violations.append((node, "M582"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1839
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
1840 # bad getattr and setattr
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1841 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1842 node.func.id in ("getattr", "hasattr")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1843 and node.args[1].s == "__call__"
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
1844 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1845 self.violations.append((node, "M504"))
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
1846 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1847 node.func.id == "getattr"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1848 and len(node.args) == 2
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1849 and self.__isIdentifier(node.args[1])
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1850 and iskeyword(AstUtilities.getValue(node.args[1]))
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
1851 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1852 self.violations.append((node, "M509"))
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
1853 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1854 node.func.id == "setattr"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1855 and len(node.args) == 3
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1856 and self.__isIdentifier(node.args[1])
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1857 and iskeyword(AstUtilities.getValue(node.args[1]))
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
1858 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1859 self.violations.append((node, "M510"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1860
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
1861 self.__checkForM526(node)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
1862
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1863 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1864
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1865 def visit_Assign(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1866 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1867 Public method to handle assignments.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1868
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1869 @param node reference to the node to be processed
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1870 @type ast.Assign
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1871 """
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1872 if len(node.targets) == 1:
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1873 target = node.targets[0]
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1874 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1875 isinstance(target, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1876 and isinstance(target.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1877 and (target.value.id, target.attr) == ("os", "environ")
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1878 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1879 self.violations.append((node, "M503"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1880
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1881 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1882
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1883 def visit_For(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1884 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1885 Public method to handle 'for' statements.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1886
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1887 @param node reference to the node to be processed
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1888 @type ast.For
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1889 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1890 self.__checkForM507(node)
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1891 self.__checkForM520(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1892 self.__checkForM523(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1893
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
1894 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1895
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1896 def visit_AsyncFor(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1897 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1898 Public method to handle 'for' statements.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1899
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1900 @param node reference to the node to be processed
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1901 @type ast.AsyncFor
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1902 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1903 self.__checkForM507(node)
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1904 self.__checkForM520(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1905 self.__checkForM523(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1906
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1907 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1908
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1909 def visit_While(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1910 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1911 Public method to handle 'while' statements.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1912
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1913 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1914 @type ast.While
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1915 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1916 self.__checkForM523(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1917
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1918 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1919
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1920 def visit_ListComp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1921 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1922 Public method to handle list comprehensions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1923
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1924 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1925 @type ast.ListComp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1926 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1927 self.__checkForM523(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1928
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1929 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1930
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1931 def visit_SetComp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1932 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1933 Public method to handle set comprehensions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1934
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1935 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1936 @type ast.SetComp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1937 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1938 self.__checkForM523(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1939
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1940 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1941
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1942 def visit_DictComp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1943 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1944 Public method to handle dictionary comprehensions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1945
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1946 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1947 @type ast.DictComp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1948 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1949 self.__checkForM523(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1950
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1951 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1952
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1953 def visit_GeneratorExp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1954 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1955 Public method to handle generator expressions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1956
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1957 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1958 @type ast.GeneratorExp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1959 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1960 self.__checkForM523(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1961
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
1962 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1963
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
1964 def visit_Assert(self, node):
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
1965 """
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
1966 Public method to handle 'assert' statements.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1967
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
1968 @param node reference to the node to be processed
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
1969 @type ast.Assert
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
1970 """
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1971 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1972 AstUtilities.isNameConstant(node.test)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
1973 and AstUtilities.getValue(node.test) is False
7256
4ef3b78ebb4e Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7249
diff changeset
1974 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1975 self.violations.append((node, "M511"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1976
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1977 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1978
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1979 def visit_FunctionDef(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1980 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1981 Public method to handle function definitions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1982
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1983 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1984 @type ast.FunctionDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1985 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1986 self.__checkForM518(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1987 self.__checkForM519(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1988 self.__checkForM521(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1989
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1990 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1991
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1992 def visit_ClassDef(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1993 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1994 Public method to handle class definitions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1995
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1996 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1997 @type ast.ClassDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1998 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
1999 self.__checkForM518(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2000 self.__checkForM521(node)
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2001 self.__checkForM524AndM527(node)
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2002
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2003 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2004
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2005 def visit_Try(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2006 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2007 Public method to handle 'try' statements'.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2008
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2009 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2010 @type ast.Try
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2011 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2012 self.__checkForM512(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2013 self.__checkForM525(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2014
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2015 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2016
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2017 def visit_Compare(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2018 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2019 Public method to handle comparison statements.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2020
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2021 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2022 @type ast.Compare
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2023 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2024 self.__checkForM515(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2025
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2026 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2027
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2028 def visit_Raise(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2029 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2030 Public method to handle 'raise' statements.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2031
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2032 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2033 @type ast.Raise
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2034 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2035 self.__checkForM516(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2036
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2037 self.generic_visit(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2038
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2039 def visit_With(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2040 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2041 Public method to handle 'with' statements.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2042
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2043 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2044 @type ast.With
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2045 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2046 self.__checkForM517(node)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2047 self.__checkForM522(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2048
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
2049 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2050
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
2051 def visit_JoinedStr(self, node):
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
2052 """
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
2053 Public method to handle f-string arguments.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2054
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
2055 @param node reference to the node to be processed
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
2056 @type ast.JoinedStr
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
2057 """
7894
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
2058 for value in node.values:
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
2059 if isinstance(value, ast.FormattedValue):
4370a8b30648 Changed the minimum supported Python version to 3.6.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7784
diff changeset
2060 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2061
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2062 self.violations.append((node, "M581"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2063
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2064 def __checkForM505(self, node):
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2065 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2066 Private method to check the use of *strip().
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2067
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2068 @param node reference to the node to be processed
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2069 @type ast.Call
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2070 """
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2071 if node.func.attr not in ("lstrip", "rstrip", "strip"):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2072 return # method name doesn't match
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2073
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
2074 if len(node.args) != 1 or not AstUtilities.isString(node.args[0]):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2075 return # used arguments don't match the builtin strip
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2076
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
2077 s = AstUtilities.getValue(node.args[0])
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2078 if len(s) == 1:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2079 return # stripping just one character
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2080
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2081 if len(s) == len(set(s)):
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2082 return # no characters appear more than once
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2083
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2084 self.violations.append((node, "M505"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2085
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2086 def __checkForM507(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2087 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2088 Private method to check for unused loop variables.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2089
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2090 @param node reference to the node to be processed
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2091 @type ast.For or ast.AsyncFor
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2092 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2093 targets = NameFinder()
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2094 targets.visit(node.target)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2095 ctrlNames = set(filter(lambda s: not s.startswith("_"), targets.getNames()))
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2096 body = NameFinder()
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2097 for expr in node.body:
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2098 body.visit(expr)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2099 usedNames = set(body.getNames())
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2100 for name in sorted(ctrlNames - usedNames):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2101 n = targets.getNames()[name][0]
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2102 self.violations.append((n, "M507", name))
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2103
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2104 def __checkForM512(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2105 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2106 Private method to check for return/continue/break inside finally blocks.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2107
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2108 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2109 @type ast.Try
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2110 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2111
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2112 def _loop(node, badNodeTypes):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2113 if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2114 return
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2115
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2116 if isinstance(node, (ast.While, ast.For)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2117 badNodeTypes = (ast.Return,)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2118
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2119 elif isinstance(node, badNodeTypes):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2120 self.violations.append((node, "M512"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2121
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2122 for child in ast.iter_child_nodes(node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2123 _loop(child, badNodeTypes)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2124
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2125 for child in node.finalbody:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2126 _loop(child, (ast.Return, ast.Continue, ast.Break))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2127
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2128 def __checkForM515(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2129 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2130 Private method to check for pointless comparisons.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2131
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2132 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2133 @type ast.Compare
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2134 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2135 if isinstance(self.nodeStack[-2], ast.Expr):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2136 self.violations.append((node, "M515"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2137
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2138 def __checkForM516(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2139 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2140 Private method to check for raising a literal instead of an exception.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2141
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2142 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2143 @type ast.Raise
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2144 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2145 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2146 AstUtilities.isNameConstant(node.exc)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2147 or AstUtilities.isNumber(node.exc)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2148 or AstUtilities.isString(node.exc)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2149 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2150 self.violations.append((node, "M516"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2151
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2152 def __checkForM517(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2153 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2154 Private method to check for use of the evil syntax
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2155 'with assertRaises(Exception):.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2156
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2157 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2158 @type ast.With
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2159 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2160 item = node.items[0]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2161 itemContext = item.context_expr
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2162 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2163 hasattr(itemContext, "func")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2164 and hasattr(itemContext.func, "attr")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2165 and itemContext.func.attr == "assertRaises"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2166 and len(itemContext.args) == 1
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2167 and isinstance(itemContext.args[0], ast.Name)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2168 and itemContext.args[0].id == "Exception"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2169 and not item.optional_vars
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2170 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2171 self.violations.append((node, "M517"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2172
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2173 def __checkForM518(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2174 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2175 Private method to check for useless expressions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2176
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2177 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2178 @type ast.FunctionDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2179 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2180 subnodeClasses = (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2181 (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2182 ast.Constant,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2183 ast.List,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2184 ast.Set,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2185 ast.Dict,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2186 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2187 if sys.version_info >= (3, 8, 0)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2188 else (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2189 ast.Num,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2190 ast.Bytes,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2191 ast.NameConstant,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2192 ast.List,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2193 ast.Set,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2194 ast.Dict,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2195 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2196 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2197 for subnode in node.body:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2198 if not isinstance(subnode, ast.Expr):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2199 continue
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2200
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2201 if isinstance(subnode.value, subnodeClasses) and not AstUtilities.isString(
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2202 subnode.value
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2203 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2204 self.violations.append((subnode, "M518"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2205
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2206 def __checkForM519(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2207 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2208 Private method to check for use of 'functools.lru_cache' or 'functools.cache'.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2209
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2210 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2211 @type ast.FunctionDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2212 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2213 caches = {
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2214 "functools.cache",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2215 "functools.lru_cache",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2216 "cache",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2217 "lru_cache",
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2218 }
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2219
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2220 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2221 len(node.decorator_list) == 0
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2222 or len(self.contexts) < 2
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2223 or not isinstance(self.contexts[-2].node, ast.ClassDef)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2224 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2225 return
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2226
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2227 # Preserve decorator order so we can get the lineno from the decorator node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2228 # rather than the function node (this location definition changes in Python 3.8)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2229 resolvedDecorators = (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2230 ".".join(self.__composeCallPath(decorator))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2231 for decorator in node.decorator_list
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2232 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2233 for idx, decorator in enumerate(resolvedDecorators):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2234 if decorator in {"classmethod", "staticmethod"}:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2235 return
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2236
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2237 if decorator in caches:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2238 self.violations.append((node.decorator_list[idx], "M519"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2239 return
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2240
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2241 def __checkForM520(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2242 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2243 Private method to check for a loop that modifies its iterable.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2244
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2245 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2246 @type ast.For or ast.AsyncFor
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2247 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2248 targets = NameFinder()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2249 targets.visit(node.target)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2250 ctrlNames = set(targets.getNames())
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2251
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2252 iterset = M520NameFinder()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2253 iterset.visit(node.iter)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2254 itersetNames = set(iterset.getNames())
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2255
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2256 for name in sorted(ctrlNames):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2257 if name in itersetNames:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2258 n = targets.getNames()[name][0]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2259 self.violations.append((n, "M520"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2260
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2261 def __checkForM521(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2262 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2263 Private method to check for use of an f-string as docstring.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2264
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2265 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2266 @type ast.FunctionDef or ast.ClassDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2267 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2268 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2269 node.body
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2270 and isinstance(node.body[0], ast.Expr)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2271 and isinstance(node.body[0].value, ast.JoinedStr)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2272 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2273 self.violations.append((node.body[0].value, "M521"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2274
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2275 def __checkForM522(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2276 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2277 Private method to check for use of an f-string as docstring.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2278
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2279 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2280 @type ast.With
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2281 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2282 item = node.items[0]
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2283 itemContext = item.context_expr
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2284 if (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2285 hasattr(itemContext, "func")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2286 and hasattr(itemContext.func, "value")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2287 and hasattr(itemContext.func.value, "id")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2288 and itemContext.func.value.id == "contextlib"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2289 and hasattr(itemContext.func, "attr")
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2290 and itemContext.func.attr == "suppress"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2291 and len(itemContext.args) == 0
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2292 ):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2293 self.violations.append((node, "M522"))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2294
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2295 def __checkForM523(self, loopNode):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2296 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2297 Private method to check that functions (including lambdas) do not use loop
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2298 variables.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2299
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2300 @param loopNode reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2301 @type ast.For, ast.AsyncFor, ast.While, ast.ListComp, ast.SetComp,ast.DictComp,
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2302 or ast.GeneratorExp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2303 """
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2304 safe_functions = []
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2305 suspiciousVariables = []
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2306 for node in ast.walk(loopNode):
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2307 # check if function is immediately consumed to avoid false alarm
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2308 if isinstance(node, ast.Call):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2309 # check for filter&reduce
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2310 if (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2311 isinstance(node.func, ast.Name)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2312 and node.func.id in ("filter", "reduce", "map")
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2313 ) or (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2314 isinstance(node.func, ast.Attribute)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2315 and node.func.attr == "reduce"
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2316 and isinstance(node.func.value, ast.Name)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2317 and node.func.value.id == "functools"
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2318 ):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2319 for arg in node.args:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2320 if isinstance(arg, BugBearVisitor.FUNCTION_NODES):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2321 safe_functions.append(arg)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2322
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2323 # check for key=
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2324 for keyword in node.keywords:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2325 if keyword.arg == "key" and isinstance(
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2326 keyword.value, BugBearVisitor.FUNCTION_NODES
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2327 ):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2328 safe_functions.append(keyword.value)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2329
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2330 # mark `return lambda: x` as safe
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2331 # does not (currently) check inner lambdas in a returned expression
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2332 # e.g. `return (lambda: x, )
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2333 if isinstance(node, ast.Return) and isinstance(
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2334 node.value, BugBearVisitor.FUNCTION_NODES
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2335 ):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2336 safe_functions.append(node.value)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2337
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2338 # find unsafe functions
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2339 if (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2340 isinstance(node, BugBearVisitor.FUNCTION_NODES)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2341 and node not in safe_functions
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2342 ):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2343 argnames = {
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2344 arg.arg for arg in ast.walk(node.args) if isinstance(arg, ast.arg)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2345 }
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2346 if isinstance(node, ast.Lambda):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2347 bodyNodes = ast.walk(node.body)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2348 else:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2349 bodyNodes = itertools.chain.from_iterable(map(ast.walk, node.body))
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2350 errors = []
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2351 for name in bodyNodes:
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2352 if isinstance(name, ast.Name) and name.id not in argnames:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2353 if isinstance(name.ctx, ast.Load):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2354 errors.append((name.lineno, name.col_offset, name.id, name))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2355 elif isinstance(name.ctx, ast.Store):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2356 argnames.add(name.id)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2357 for err in errors:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2358 if err[2] not in argnames and err not in self.__M523Seen:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2359 self.__M523Seen.add(err) # dedupe across nested loops
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2360 suspiciousVariables.append(err)
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2361
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2362 if suspiciousVariables:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2363 reassignedInLoop = set(self.__getAssignedNames(loopNode))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2364
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2365 for err in sorted(suspiciousVariables):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2366 if reassignedInLoop.issuperset(err[2]):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2367 self.violations.append((err[3], "M523", err[2]))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2368
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2369 def __checkForM524AndM527(self, node):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2370 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2371 Private method to check for inheritance from abstract classes in abc and lack of
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2372 any methods decorated with abstract*.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2373
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2374 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2375 @type ast.ClassDef
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2376 """ # __IGNORE_WARNING_D234r__
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2377
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2378 def isAbcClass(value, name="ABC"):
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2379 if isinstance(value, ast.keyword):
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2380 return value.arg == "metaclass" and isAbcClass(value.value, "ABCMeta")
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2381
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2382 # class foo(ABC)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2383 # class foo(abc.ABC)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2384 return (isinstance(value, ast.Name) and value.id == name) or (
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2385 isinstance(value, ast.Attribute)
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2386 and value.attr == name
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2387 and isinstance(value.value, ast.Name)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2388 and value.value.id == "abc"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2389 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2390
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2391 def isAbstractDecorator(expr):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2392 return (isinstance(expr, ast.Name) and expr.id[:8] == "abstract") or (
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2393 isinstance(expr, ast.Attribute) and expr.attr[:8] == "abstract"
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2394 )
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2395
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2396 def isOverload(expr):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2397 return (isinstance(expr, ast.Name) and expr.id == "overload") or (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2398 isinstance(expr, ast.Attribute) and expr.attr == "overload"
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2399 )
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2400
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2401 def emptyBody(body):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2402 def isStrOrEllipsis(node):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2403 # ast.Ellipsis and ast.Str used in python<3.8
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2404 return isinstance(node, (ast.Ellipsis, ast.Str)) or (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2405 isinstance(node, ast.Constant)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2406 and (node.value is Ellipsis or isinstance(node.value, str))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2407 )
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2408
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2409 # Function body consist solely of `pass`, `...`, and/or (doc)string literals
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2410 return all(
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2411 isinstance(stmt, ast.Pass)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2412 or (isinstance(stmt, ast.Expr) and isStrOrEllipsis(stmt.value))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2413 for stmt in body
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2414 )
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2415
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2416 # don't check multiple inheritance
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2417 if len(node.bases) + len(node.keywords) > 1:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2418 return
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2419
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2420 # only check abstract classes
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2421 if not any(map(isAbcClass, (*node.bases, *node.keywords))):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2422 return
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2423
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2424 hasAbstractMethod = False
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2425
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2426 if not any(map(isAbcClass, (*node.bases, *node.keywords))):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2427 return
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2428
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2429 for stmt in node.body:
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2430 # Ignore abc's that declares a class attribute that must be set
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2431 if isinstance(stmt, (ast.AnnAssign, ast.Assign)):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2432 hasAbstractMethod = True
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2433 continue
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2434
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2435 # only check function defs
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2436 if not isinstance(stmt, (ast.FunctionDef, ast.AsyncFunctionDef)):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2437 continue
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2438
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2439 hasAbstractDecorator = any(map(isAbstractDecorator, stmt.decorator_list))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2440
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2441 hasAbstractMethod |= hasAbstractDecorator
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2442
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2443 if (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2444 not hasAbstractDecorator
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2445 and emptyBody(stmt.body)
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2446 and not any(map(isOverload, stmt.decorator_list))
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2447 ):
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2448 self.violations.append((stmt, "M527", stmt.name))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2449
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2450 if not hasAbstractMethod:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2451 self.violations.append((node, "M524", node.name))
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2452
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2453 def __checkForM525(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2454 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2455 Private method to check for exceptions being handled multiple times.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2456
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2457 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2458 @type ast.Try
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2459 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2460 seen = []
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2461
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2462 for handler in node.handlers:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2463 if isinstance(handler.type, (ast.Name, ast.Attribute)):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2464 name = ".".join(self.__composeCallPath(handler.type))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2465 seen.append(name)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2466 elif isinstance(handler.type, ast.Tuple):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2467 # to avoid checking the same as M514, remove duplicates per except
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2468 uniques = set()
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2469 for entry in handler.type.elts:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2470 name = ".".join(self.__composeCallPath(entry))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2471 uniques.add(name)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2472 seen.extend(uniques)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2473
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2474 # sort to have a deterministic output
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2475 duplicates = sorted({x for x in seen if seen.count(x) > 1})
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2476 for duplicate in duplicates:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2477 self.violations.append((node, "M525", duplicate))
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2478
9595
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2479 def __checkForM526(self, node):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2480 """
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2481 Private method to check for Star-arg unpacking after keyword argument.
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2482
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2483 @param node reference to the node to be processed
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2484 @type ast.Call
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2485 """
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2486 if not node.keywords:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2487 return
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2488
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2489 starreds = [arg for arg in node.args if isinstance(arg, ast.Starred)]
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2490 if not starreds:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2491 return
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2492
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2493 firstKeyword = node.keywords[0].value
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2494 for starred in starreds:
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2495 if (starred.lineno, starred.col_offset) > (
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2496 firstKeyword.lineno,
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2497 firstKeyword.col_offset,
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2498 ):
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2499 self.violations.append((node, "M526"))
2bd590c40309 Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9482
diff changeset
2500
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2501
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2502 class NameFinder(ast.NodeVisitor):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2503 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2504 Class to extract a name out of a tree of nodes.
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2505 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2506
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2507 def __init__(self):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2508 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2509 Constructor
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2510 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
2511 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2512
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2513 self.__names = {}
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2514
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2515 def visit_Name(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2516 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2517 Public method to handle 'Name' nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2518
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2519 @param node reference to the node to be processed
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2520 @type ast.Name
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2521 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2522 self.__names.setdefault(node.id, []).append(node)
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2523
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2524 def visit(self, node):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2525 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2526 Public method to traverse a given AST node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2527
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2528 @param node AST node to be traversed
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2529 @type ast.Node
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2530 @return reference to the last processed node
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2531 @rtype ast.Node
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2532 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2533 if isinstance(node, list):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2534 for elem in node:
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
2535 super().visit(elem)
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2536 return node
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2537 else:
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2538 return super().visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2539
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2540 def getNames(self):
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2541 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2542 Public method to return the extracted names and Name nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2543
6184
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2544 @return dictionary containing the names as keys and the list of nodes
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2545 @rtype dict
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2546 """
789e88d94899 Code Style Checker: added some checks of the bugbear checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6183
diff changeset
2547 return self.__names
6183
29384109306c Code Style Checker: started to add some bugbear code checks
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6182
diff changeset
2548
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2549
9327
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2550 class M520NameFinder(NameFinder):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2551 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2552 Class to extract a name out of a tree of nodes ignoring names defined within the
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2553 local scope of a comprehension.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2554 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2555
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2556 def visit_GeneratorExp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2557 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2558 Public method to handle a generator expressions.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2559
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2560 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2561 @type ast.GeneratorExp
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2562 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2563 self.visit(node.generators)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2564
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2565 def visit_ListComp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2566 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2567 Public method to handle a list comprehension.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2568
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2569 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2570 @type TYPE
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2571 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2572 self.visit(node.generators)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2573
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2574 def visit_DictComp(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2575 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2576 Public method to handle a dictionary comprehension.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2577
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2578 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2579 @type TYPE
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2580 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2581 self.visit(node.generators)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2582
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2583 def visit_comprehension(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2584 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2585 Public method to handle the 'for' of a comprehension.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2586
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2587 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2588 @type ast.comprehension
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2589 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2590 self.visit(node.iter)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2591
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2592 def visit_Lambda(self, node):
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2593 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2594 Public method to handle a Lambda function.
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2595
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2596 @param node reference to the node to be processed
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2597 @type ast.Lambda
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2598 """
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2599 self.visit(node.body)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2600 for lambdaArg in node.args.args:
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2601 self.getNames().pop(lambdaArg.arg, None)
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2602
2b768afcaee1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9274
diff changeset
2603
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2604 class ReturnVisitor(ast.NodeVisitor):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2605 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2606 Class implementing a node visitor 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
2607 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2608
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2609 Assigns = "assigns"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2610 Refs = "refs"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2611 Returns = "returns"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2612
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2613 def __init__(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2614 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2615 Constructor
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2616 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
2617 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2618
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2619 self.__stack = []
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2620 self.violations = []
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2621 self.__loopCount = 0
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2622
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2623 @property
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2624 def assigns(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2625 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2626 Public method to get the Assign nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2627
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2628 @return dictionary containing the node name as key and line number
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2629 as value
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2630 @rtype dict
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2631 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2632 return self.__stack[-1][ReturnVisitor.Assigns]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2633
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2634 @property
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2635 def refs(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2636 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2637 Public method to get the References nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2638
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2639 @return dictionary containing the node name as key and line number
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2640 as value
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2641 @rtype dict
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2642 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2643 return self.__stack[-1][ReturnVisitor.Refs]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2644
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2645 @property
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2646 def returns(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2647 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2648 Public method to get the Return nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2649
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2650 @return dictionary containing the node name as key and line number
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2651 as value
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2652 @rtype dict
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2653 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2654 return self.__stack[-1][ReturnVisitor.Returns]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2655
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2656 def visit_For(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2657 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2658 Public method to handle a for loop.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2659
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2660 @param node reference to the for node to handle
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2661 @type ast.For
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2662 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2663 self.__visitLoop(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2664
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2665 def visit_AsyncFor(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2666 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2667 Public method to handle an async for loop.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2668
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2669 @param node reference to the async for node to handle
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2670 @type ast.AsyncFor
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2671 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2672 self.__visitLoop(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2673
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2674 def visit_While(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2675 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2676 Public method to handle a while loop.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2677
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2678 @param node reference to the while node to handle
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2679 @type ast.While
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2680 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2681 self.__visitLoop(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2682
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2683 def __visitLoop(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2684 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2685 Private method to handle loop nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2686
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2687 @param node reference to the loop node to handle
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2688 @type ast.For, ast.AsyncFor or ast.While
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2689 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2690 self.__loopCount += 1
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2691 self.generic_visit(node)
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2692 self.__loopCount -= 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2693
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2694 def __visitWithStack(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2695 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2696 Private method to traverse a given function node using a stack.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2697
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2698 @param node AST node to be traversed
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2699 @type ast.FunctionDef or ast.AsyncFunctionDef
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2700 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2701 self.__stack.append(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2702 {
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2703 ReturnVisitor.Assigns: defaultdict(list),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2704 ReturnVisitor.Refs: defaultdict(list),
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2705 ReturnVisitor.Returns: [],
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2706 }
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2707 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2708
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2709 self.generic_visit(node)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2710 self.__checkFunction(node)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2711 self.__stack.pop()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2712
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2713 def visit_FunctionDef(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2714 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2715 Public method to handle a function definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2716
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2717 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2718 @type ast.FunctionDef
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2719 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2720 self.__visitWithStack(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2721
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2722 def visit_AsyncFunctionDef(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2723 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2724 Public method to handle a function definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2725
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2726 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2727 @type ast.AsyncFunctionDef
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2728 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2729 self.__visitWithStack(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2730
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2731 def visit_Return(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2732 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2733 Public method to handle a return node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2734
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2735 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2736 @type ast.Return
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2737 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2738 self.returns.append(node)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2739 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2740
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2741 def visit_Assign(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2742 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2743 Public method to handle an assign node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2744
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2745 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2746 @type ast.Assign
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2747 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2748 if not self.__stack:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2749 return
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2750
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2751 self.generic_visit(node.value)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2752
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2753 target = node.targets[0]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2754 if isinstance(target, ast.Tuple) and not isinstance(node.value, ast.Tuple):
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2755 # skip unpacking assign
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2756 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2757
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2758 self.__visitAssignTarget(target)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2759
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2760 def visit_Name(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2761 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2762 Public method to handle a name node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2763
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2764 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2765 @type ast.Name
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2766 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2767 if self.__stack:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2768 self.refs[node.id].append(node.lineno)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2769
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2770 def __visitAssignTarget(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2771 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2772 Private method to handle an assign target node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2773
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2774 @param node reference to the node to handle
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2775 @type ast.AST
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2776 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2777 if isinstance(node, ast.Tuple):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2778 for elt in node.elts:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2779 self.__visitAssignTarget(elt)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2780 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2781
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2782 if not self.__loopCount and isinstance(node, ast.Name):
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2783 self.assigns[node.id].append(node.lineno)
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2784 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2785
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2786 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2787
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2788 def __checkFunction(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2789 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2790 Private method to check a function definition node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2791
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2792 @param node reference to the node to check
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2793 @type ast.AsyncFunctionDef or ast.FunctionDef
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2794 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2795 if not self.returns or not node.body:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2796 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2797
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2798 if len(node.body) == 1 and isinstance(node.body[-1], ast.Return):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2799 # skip functions that consist of `return None` only
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2800 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2801
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2802 if not self.__resultExists():
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2803 self.__checkUnnecessaryReturnNone()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2804 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2805
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2806 self.__checkImplicitReturnValue()
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2807 self.__checkImplicitReturn(node.body[-1])
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2808
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2809 for n in self.returns:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2810 if n.value:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2811 self.__checkUnnecessaryAssign(n.value)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2812
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2813 def __isNone(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2814 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2815 Private method to check, if a node value is None.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2816
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2817 @param node reference to the node to check
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2818 @type ast.AST
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2819 @return flag indicating the node contains a None value
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2820 @rtype bool
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2821 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2822 return AstUtilities.isNameConstant(node) and AstUtilities.getValue(node) is None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2823
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2824 def __isFalse(self, node):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2825 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2826 Private method to check, if a node value is False.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2827
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2828 @param node reference to the node to check
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2829 @type ast.AST
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2830 @return flag indicating the node contains a False value
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2831 @rtype bool
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2832 """
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2833 return (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2834 AstUtilities.isNameConstant(node) and AstUtilities.getValue(node) is False
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2835 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2836
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2837 def __resultExists(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2838 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2839 Private method to check the existance of a return result.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2840
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2841 @return flag indicating the existence of a return result
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2842 @rtype bool
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2843 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2844 for node in self.returns:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2845 value = node.value
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2846 if value and not self.__isNone(value):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2847 return True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2848
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2849 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2850
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2851 def __checkImplicitReturnValue(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2852 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2853 Private method to check for implicit return values.
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2854 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2855 for node in self.returns:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2856 if not node.value:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2857 self.violations.append((node, "M832"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2858
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2859 def __checkUnnecessaryReturnNone(self):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2860 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2861 Private method to check for an unnecessary 'return None' statement.
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2862 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2863 for node in self.returns:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2864 if self.__isNone(node.value):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2865 self.violations.append((node, "M831"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2866
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2867 def __checkImplicitReturn(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2868 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2869 Private method to check for an implicit return statement.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2870
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2871 @param node reference to the node to check
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2872 @type ast.AST
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2873 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2874 if isinstance(node, ast.If):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2875 if not node.body or not node.orelse:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2876 self.violations.append((node, "M833"))
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2877 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2878
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2879 self.__checkImplicitReturn(node.body[-1])
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2880 self.__checkImplicitReturn(node.orelse[-1])
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2881 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2882
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2883 if isinstance(node, (ast.For, ast.AsyncFor)) and node.orelse:
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2884 self.__checkImplicitReturn(node.orelse[-1])
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2885 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2886
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2887 if isinstance(node, (ast.With, ast.AsyncWith)):
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2888 self.__checkImplicitReturn(node.body[-1])
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2889 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2890
7651
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2891 if isinstance(node, ast.Assert) and self.__isFalse(node.test):
ca87b7490449 Code Style Checker: updated the return style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7639
diff changeset
2892 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2893
6889
334257ef9435 MiscellaneousChecker: fixed some Python2 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6882
diff changeset
2894 try:
334257ef9435 MiscellaneousChecker: fixed some Python2 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6882
diff changeset
2895 okNodes = (ast.Return, ast.Raise, ast.While, ast.Try)
334257ef9435 MiscellaneousChecker: fixed some Python2 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6882
diff changeset
2896 except AttributeError:
334257ef9435 MiscellaneousChecker: fixed some Python2 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6882
diff changeset
2897 okNodes = (ast.Return, ast.Raise, ast.While)
334257ef9435 MiscellaneousChecker: fixed some Python2 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6882
diff changeset
2898 if not isinstance(node, okNodes):
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2899 self.violations.append((node, "M833"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2900
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2901 def __checkUnnecessaryAssign(self, node):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2902 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2903 Private method to check for an unnecessary assign statement.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2904
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2905 @param node reference to the node to check
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2906 @type ast.AST
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2907 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2908 if not isinstance(node, ast.Name):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2909 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2910
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2911 varname = node.id
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2912 returnLineno = node.lineno
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2913
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2914 if varname not in self.assigns:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2915 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2916
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2917 if varname not in self.refs:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2918 self.violations.append((node, "M834"))
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2919 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2920
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2921 if self.__hasRefsBeforeNextAssign(varname, returnLineno):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2922 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2923
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2924 self.violations.append((node, "M834"))
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2925
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2926 def __hasRefsBeforeNextAssign(self, varname, returnLineno):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2927 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2928 Private method to check for references before a following assign
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2929 statement.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2930
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2931 @param varname variable name to check for
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2932 @type str
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2933 @param returnLineno line number of the return statement
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2934 @type int
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2935 @return flag indicating the existence of references
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2936 @rtype bool
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2937 """
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2938 beforeAssign = 0
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2939 afterAssign = None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2940
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2941 for lineno in sorted(self.assigns[varname]):
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2942 if lineno > returnLineno:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2943 afterAssign = lineno
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2944 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2945
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2946 if lineno <= returnLineno:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2947 beforeAssign = lineno
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2948
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2949 for lineno in self.refs[varname]:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2950 if lineno == returnLineno:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2951 continue
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2952
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2953 if afterAssign:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2954 if beforeAssign < lineno <= afterAssign:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2955 return True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2956
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2957 elif beforeAssign < lineno:
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2958 return True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2959
6882
65d1bf4b7427 MiscellaneousChecker: added a checker for advised forms of the return statement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6645
diff changeset
2960 return False
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2961
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2962
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2963 class DateTimeVisitor(ast.NodeVisitor):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2964 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2965 Class implementing a node visitor to check datetime function calls.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2966
9274
86fab0c74430 Added some TODO markers to update some checker code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
2967 Note: This class is modeled after flake8_datetimez checker.
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2968 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2969
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2970 def __init__(self):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2971 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2972 Constructor
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2973 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
2974 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2975
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2976 self.violations = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2977
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2978 def __getFromKeywords(self, keywords, name):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2979 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2980 Private method to get a keyword node given its name.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2981
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2982 @param keywords list of keyword argument nodes
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2983 @type list of ast.AST
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2984 @param name name of the keyword node
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2985 @type str
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2986 @return keyword node
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2987 @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
2988 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2989 for keyword in keywords:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2990 if keyword.arg == name:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2991 return keyword
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2992
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2993 return None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
2994
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2995 def visit_Call(self, node):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2996 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2997 Public method to handle a function call.
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2998
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
2999 Every datetime related function call is check for use of the naive
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3000 variant (i.e. use without TZ info).
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3001
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3002 @param node reference to the node to be processed
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3003 @type ast.Call
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3004 """
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3005 # datetime.something()
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3006 isDateTimeClass = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3007 isinstance(node.func, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3008 and isinstance(node.func.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3009 and node.func.value.id == "datetime"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3010 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3011
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3012 # datetime.datetime.something()
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3013 isDateTimeModuleAndClass = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3014 isinstance(node.func, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3015 and isinstance(node.func.value, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3016 and node.func.value.attr == "datetime"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3017 and isinstance(node.func.value.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3018 and node.func.value.value.id == "datetime"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3019 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3020
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3021 if isDateTimeClass:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3022 if node.func.attr == "datetime":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3023 # datetime.datetime(2000, 1, 1, 0, 0, 0, 0,
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3024 # datetime.timezone.utc)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3025 isCase1 = len(node.args) >= 8 and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3026 AstUtilities.isNameConstant(node.args[7])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3027 and AstUtilities.getValue(node.args[7]) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3028 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3029
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3030 # datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3031 tzinfoKeyword = self.__getFromKeywords(node.keywords, "tzinfo")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3032 isCase2 = tzinfoKeyword is not None and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3033 AstUtilities.isNameConstant(tzinfoKeyword.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3034 and AstUtilities.getValue(tzinfoKeyword.value) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3035 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3036
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3037 if not (isCase1 or isCase2):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3038 self.violations.append((node, "M301"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3039
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3040 elif node.func.attr == "time":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3041 # time(12, 10, 45, 0, datetime.timezone.utc)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3042 isCase1 = len(node.args) >= 5 and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3043 AstUtilities.isNameConstant(node.args[4])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3044 and AstUtilities.getValue(node.args[4]) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3045 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3046
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3047 # datetime.time(12, 10, 45, tzinfo=datetime.timezone.utc)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3048 tzinfoKeyword = self.__getFromKeywords(node.keywords, "tzinfo")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3049 isCase2 = tzinfoKeyword is not None and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3050 AstUtilities.isNameConstant(tzinfoKeyword.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3051 and AstUtilities.getValue(tzinfoKeyword.value) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3052 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3053
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3054 if not (isCase1 or isCase2):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3055 self.violations.append((node, "M321"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3056
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3057 elif node.func.attr == "date":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3058 self.violations.append((node, "M311"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3059
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3060 if isDateTimeClass or isDateTimeModuleAndClass:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3061 if node.func.attr == "today":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3062 self.violations.append((node, "M302"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3063
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3064 elif node.func.attr == "utcnow":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3065 self.violations.append((node, "M303"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3066
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3067 elif node.func.attr == "utcfromtimestamp":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3068 self.violations.append((node, "M304"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3069
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3070 elif node.func.attr in "now":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3071 # datetime.now(UTC)
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3072 isCase1 = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3073 len(node.args) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3074 and len(node.keywords) == 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3075 and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3076 AstUtilities.isNameConstant(node.args[0])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3077 and AstUtilities.getValue(node.args[0]) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3078 )
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3079 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3080
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3081 # datetime.now(tz=UTC)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3082 tzKeyword = self.__getFromKeywords(node.keywords, "tz")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3083 isCase2 = tzKeyword is not None and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3084 AstUtilities.isNameConstant(tzKeyword.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3085 and AstUtilities.getValue(tzKeyword.value) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3086 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3087
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3088 if not (isCase1 or isCase2):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3089 self.violations.append((node, "M305"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3090
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3091 elif node.func.attr == "fromtimestamp":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3092 # datetime.fromtimestamp(1234, UTC)
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3093 isCase1 = (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3094 len(node.args) == 2
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3095 and len(node.keywords) == 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3096 and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3097 AstUtilities.isNameConstant(node.args[1])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3098 and AstUtilities.getValue(node.args[1]) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3099 )
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3100 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3101
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3102 # datetime.fromtimestamp(1234, tz=UTC)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3103 tzKeyword = self.__getFromKeywords(node.keywords, "tz")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3104 isCase2 = tzKeyword is not None and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3105 AstUtilities.isNameConstant(tzKeyword.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3106 and AstUtilities.getValue(tzKeyword.value) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3107 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3108
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3109 if not (isCase1 or isCase2):
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3110 self.violations.append((node, "M306"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3111
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3112 elif node.func.attr == "strptime":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3113 # datetime.strptime(...).replace(tzinfo=UTC)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3114 parent = getattr(node, "_dtCheckerParent", None)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3115 pparent = getattr(parent, "_dtCheckerParent", None)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3116 if not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3117 isinstance(parent, ast.Attribute) and parent.attr == "replace"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3118 ) or not isinstance(pparent, ast.Call):
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3119 isCase1 = False
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3120 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3121 tzinfoKeyword = self.__getFromKeywords(pparent.keywords, "tzinfo")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3122 isCase1 = tzinfoKeyword is not None and not (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3123 AstUtilities.isNameConstant(tzinfoKeyword.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3124 and AstUtilities.getValue(tzinfoKeyword.value) is None
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3125 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3126
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3127 if not isCase1:
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3128 self.violations.append((node, "M307"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3129
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3130 elif node.func.attr == "fromordinal":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3131 self.violations.append((node, "M308"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3132
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3133 # date.something()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3134 isDateClass = (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3135 isinstance(node.func, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3136 and isinstance(node.func.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3137 and node.func.value.id == "date"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3138 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3139
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3140 # datetime.date.something()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3141 isDateModuleAndClass = (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3142 isinstance(node.func, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3143 and isinstance(node.func.value, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3144 and node.func.value.attr == "date"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3145 and isinstance(node.func.value.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3146 and node.func.value.value.id == "datetime"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3147 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3148
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3149 if isDateClass or isDateModuleAndClass:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3150 if node.func.attr == "today":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3151 self.violations.append((node, "M312"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3152
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3153 elif node.func.attr == "fromtimestamp":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3154 self.violations.append((node, "M313"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3155
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3156 elif node.func.attr == "fromordinal":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3157 self.violations.append((node, "M314"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3158
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3159 elif node.func.attr == "fromisoformat":
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3160 self.violations.append((node, "M315"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3161
7045
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3162 self.generic_visit(node)
c2bf08f87a1d Code Style Checker: added checks for naive uses of datetime classes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7042
diff changeset
3163
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3164
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3165 class SysVersionVisitor(ast.NodeVisitor):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3166 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3167 Class implementing a node visitor to check the use of sys.version and
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3168 sys.version_info.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3169
9274
86fab0c74430 Added some TODO markers to update some checker code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
3170 Note: This class is modeled after flake8-2020 checker.
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3171 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3172
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3173 def __init__(self):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3174 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3175 Constructor
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3176 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8207
diff changeset
3177 super().__init__()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3178
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3179 self.violations = []
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3180 self.__fromImports = {}
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3181
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3182 def visit_ImportFrom(self, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3183 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3184 Public method to handle a from ... import ... statement.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3185
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3186 @param node reference to the node to be processed
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3187 @type ast.ImportFrom
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3188 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3189 for alias in node.names:
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3190 if node.module is not None and not alias.asname:
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3191 self.__fromImports[alias.name] = node.module
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3192
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3193 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3194
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3195 def __isSys(self, attr, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3196 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3197 Private method to check for a reference to sys attribute.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3198
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3199 @param attr attribute name
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3200 @type str
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3201 @param node reference to the node to be checked
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3202 @type ast.Node
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3203 @return flag indicating a match
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3204 @rtype bool
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3205 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3206 match = False
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3207 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3208 isinstance(node, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3209 and isinstance(node.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3210 and node.value.id == "sys"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3211 and node.attr == attr
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3212 ) or (
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3213 isinstance(node, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3214 and node.id == attr
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3215 and self.__fromImports.get(node.id) == "sys"
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3216 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3217 match = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3218
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3219 return match
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3220
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3221 def __isSysVersionUpperSlice(self, node, n):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3222 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3223 Private method to check the upper slice of sys.version.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3224
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3225 @param node reference to the node to be checked
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3226 @type ast.Node
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3227 @param n slice value to check against
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3228 @type int
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3229 @return flag indicating a match
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3230 @rtype bool
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3231 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3232 return (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3233 self.__isSys("version", node.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3234 and isinstance(node.slice, ast.Slice)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3235 and node.slice.lower is None
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3236 and AstUtilities.isNumber(node.slice.upper)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3237 and AstUtilities.getValue(node.slice.upper) == n
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3238 and node.slice.step is None
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3239 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3240
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3241 def visit_Subscript(self, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3242 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3243 Public method to handle a subscript.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3244
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3245 @param node reference to the node to be processed
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3246 @type ast.Subscript
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3247 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3248 if self.__isSysVersionUpperSlice(node, 1):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3249 self.violations.append((node.value, "M423"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3250 elif self.__isSysVersionUpperSlice(node, 3):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3251 self.violations.append((node.value, "M401"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3252 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3253 self.__isSys("version", node.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3254 and isinstance(node.slice, ast.Index)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3255 and AstUtilities.isNumber(node.slice.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3256 and AstUtilities.getValue(node.slice.value) == 2
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3257 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3258 self.violations.append((node.value, "M402"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3259 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3260 self.__isSys("version", node.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3261 and isinstance(node.slice, ast.Index)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3262 and AstUtilities.isNumber(node.slice.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3263 and AstUtilities.getValue(node.slice.value) == 0
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3264 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3265 self.violations.append((node.value, "M421"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3266
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3267 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3268
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3269 def visit_Compare(self, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3270 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3271 Public method to handle a comparison.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3272
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3273 @param node reference to the node to be processed
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3274 @type ast.Compare
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3275 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3276 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3277 isinstance(node.left, ast.Subscript)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3278 and self.__isSys("version_info", node.left.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3279 and isinstance(node.left.slice, ast.Index)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3280 and AstUtilities.isNumber(node.left.slice.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3281 and AstUtilities.getValue(node.left.slice.value) == 0
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3282 and len(node.ops) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3283 and isinstance(node.ops[0], ast.Eq)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3284 and AstUtilities.isNumber(node.comparators[0])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3285 and AstUtilities.getValue(node.comparators[0]) == 3
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3286 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3287 self.violations.append((node.left, "M411"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3288 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3289 self.__isSys("version", node.left)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3290 and len(node.ops) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3291 and isinstance(node.ops[0], (ast.Lt, ast.LtE, ast.Gt, ast.GtE))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3292 and AstUtilities.isString(node.comparators[0])
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3293 ):
7289
6f4761a73f5f MiscellaneousChecker: prepared the code for some changes in Python 3.8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7256
diff changeset
3294 if len(AstUtilities.getValue(node.comparators[0])) == 1:
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3295 errorCode = "M422"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3296 else:
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3297 errorCode = "M403"
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3298 self.violations.append((node.left, errorCode))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3299 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3300 isinstance(node.left, ast.Subscript)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3301 and self.__isSys("version_info", node.left.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3302 and isinstance(node.left.slice, ast.Index)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3303 and AstUtilities.isNumber(node.left.slice.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3304 and AstUtilities.getValue(node.left.slice.value) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3305 and len(node.ops) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3306 and isinstance(node.ops[0], (ast.Lt, ast.LtE, ast.Gt, ast.GtE))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3307 and AstUtilities.isNumber(node.comparators[0])
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3308 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3309 self.violations.append((node, "M413"))
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3310 elif (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3311 isinstance(node.left, ast.Attribute)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3312 and self.__isSys("version_info", node.left.value)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3313 and node.left.attr == "minor"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3314 and len(node.ops) == 1
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3315 and isinstance(node.ops[0], (ast.Lt, ast.LtE, ast.Gt, ast.GtE))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3316 and AstUtilities.isNumber(node.comparators[0])
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3317 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3318 self.violations.append((node, "M414"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3319
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3320 self.generic_visit(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3321
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3322 def visit_Attribute(self, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3323 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3324 Public method to handle an attribute.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3325
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3326 @param node reference to the node to be processed
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3327 @type ast.Attribute
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3328 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3329 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3330 isinstance(node.value, ast.Name)
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3331 and node.value.id == "six"
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3332 and node.attr == "PY3"
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3333 ):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3334 self.violations.append((node, "M412"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3335
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3336 self.generic_visit(node)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3337
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3338 def visit_Name(self, node):
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3339 """
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3340 Public method to handle an name.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3341
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3342 @param node reference to the node to be processed
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3343 @type ast.Name
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3344 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3345 if node.id == "PY3" and self.__fromImports.get(node.id) == "six":
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3346 self.violations.append((node, "M412"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3347
7245
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3348 self.generic_visit(node)
b47179fbb9d8 Code Style Checker (Miscellaneous Checker)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7057
diff changeset
3349
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
3350
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4540
diff changeset
3351 #
7639
422fd05e9c91 Removed some more Python2 related code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7637
diff changeset
3352 # eflag: noqa = M891

eric ide

mercurial