eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py

Sat, 10 Apr 2021 15:11:43 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 10 Apr 2021 15:11:43 +0200
changeset 8211
8322a6f219ff
parent 8210
b5903eaa7a7b
child 8212
91015c7ace79
permissions
-rw-r--r--

Code Style Checker
- don't treat 'if __name__ == "__main__":' as an issue
- added a check for if-blocks which only check if a key is in a dictionary
- added a check for bare boolean function arguments
- added a check for bare numeric function arguments

8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a node visitor checking for code that could be simplified.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import ast
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import collections
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
12 import copy
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
13 import itertools
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 try:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from ast import unparse
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 except ImportError:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 # Python < 3.9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 from .ast_unparse import unparse
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 ######################################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 ## The following code is derived from the flake8-simplify package.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 ## Original License:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 ## MIT License
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 ## Copyright (c) 2020 Martin Thoma
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 ## Permission is hereby granted, free of charge, to any person obtaining a copy
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 ## of this software and associated documentation files (the "Software"), to
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 ## deal in the Software without restriction, including without limitation the
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 ## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 ## sell copies of the Software, and to permit persons to whom the Software is
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 ## furnished to do so, subject to the following conditions:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 ## The above copyright notice and this permission notice shall be included in
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 ## all copies or substantial portions of the Software.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 ##
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 ## IN THE SOFTWARE.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 ######################################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 BOOL_CONST_TYPES = (ast.Constant, ast.NameConstant)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 AST_CONST_TYPES = (ast.Constant, ast.NameConstant, ast.Str, ast.Num)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 STR_TYPES = (ast.Constant, ast.Str)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 class SimplifyNodeVisitor(ast.NodeVisitor):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 Class to traverse the AST node tree and check for code that can be
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 simplified.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 def __init__(self, errorCallback):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 Constructor
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 @param errorCallback callback function to register an error
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 @type func
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 super(SimplifyNodeVisitor, self).__init__()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 self.__error = errorCallback
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
69
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
70 self.__classDefinitionStack = []
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 def visit_Expr(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 Public method to process an Expr node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 @param node reference to the Expr node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 @type ast.Expr
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 self.__check112(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
8202
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
83 def visit_Assign(self, node):
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
84 """
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
85 Public method to process an Assign node.
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
86
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
87 @param node reference to the Assign node
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
88 @type ast.Assign
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
89 """
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
90 self.__check181(node)
8202
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
91
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
92 self.generic_visit(node)
df194f43119c Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8195
diff changeset
93
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 def visit_BoolOp(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 Public method to process a BoolOp node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 @param node reference to the BoolOp node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.__check101(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.__check109(node)
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
103 self.__check221(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
104 self.__check222(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
105 self.__check223(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
106 self.__check224(node)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 def visit_If(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 Public method to process an If node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 @param node reference to the If node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 self.__check102(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 self.__check103(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 self.__check106(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 self.__check108(node)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
121 self.__check114(node)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
122 self.__check116(node)
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
123 self.__check122(node)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
127 def visit_IfExp(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
128 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
129 Public method to process an IfExp node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
130
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
131 @param node reference to the IfExp node
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
132 @type ast.IfExp
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
133 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
134 self.__check211(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
135 self.__check212(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
136 self.__check213(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
137
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
138 self.generic_visit(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
139
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 def visit_For(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 Public method to process a For node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @param node reference to the For node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__check104(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.__check110_111(node)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
149 self.__check113(node)
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
150 self.__check118(node)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 def visit_Try(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 Public method to process a Try node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @param node reference to the Try node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 self.__check105(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.__check107(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
166 def visit_Call(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
167 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
168 Public method to process a Call node.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
169
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
170 @param node reference to the Call node
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
171 @type ast.Call
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
172 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
173 self.__check115(node)
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
174 self.__check182(node)
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
175 self.__check401(node)
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
176 self.__check402(node)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
177
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
178 self.generic_visit(node)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
179
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
180 def visit_With(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
181 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
182 Public method to process a With node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
183
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
184 @param node reference to the With node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
185 @type ast.With
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
186 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
187 self.__check117(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
188
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
189 self.generic_visit(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
190
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
191 def visit_Compare(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
192 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
193 Public method to process a Compare node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
194
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
195 @param node reference to the Compare node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
196 @type ast.Compare
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
197 """
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
198 self.__check118(node)
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
199 self.__check301(node)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
200
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
201 self.generic_visit(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
202
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
203 def visit_ClassDef(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
204 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
205 Public method to process a ClassDef node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
206
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
207 @param node reference to the ClassDef node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
208 @type ast.ClassDef
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
209 """
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
210 # register the name of the class being defined
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
211 self.__classDefinitionStack.append(node.name)
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
212
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
213 self.__check119(node)
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
214 self.__check120_121(node)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
215
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
216 self.generic_visit(node)
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
217
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
218 self.__classDefinitionStack.pop()
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
219
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
220 def visit_UnaryOp(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
221 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
222 Public method to process a UnaryOp node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
223
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
224 @param node reference to the UnaryOp node
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
225 @type ast.UnaryOp
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
226 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
227 self.__check201(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
228 self.__check202(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
229 self.__check203(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
230 self.__check204(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
231 self.__check205(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
232 self.__check206(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
233 self.__check207(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
234 self.__check208(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
235
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
236 self.generic_visit(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
237
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 ## Helper methods for the various checkers below
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 def __getDuplicatedIsinstanceCall(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 Private method to get a list of isinstance arguments which could
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 be combined.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 @param node reference to the AST node to be inspected
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 @return list of variable names of duplicated isinstance calls
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 @rtype list of str
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 counter = collections.defaultdict(int)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 for call in node.values:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 # Ensure this is a call of the built-in isinstance() function.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 if not isinstance(call, ast.Call) or len(call.args) != 2:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 continue
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 functionName = unparse(call.func)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 if functionName != "isinstance":
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 continue
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 arg0Name = unparse(call.args[0])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 counter[arg0Name] += 1
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 return [name for name, count in counter.items() if count > 1]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
267 def __isConstantIncrease(self, expression):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
268 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
269 Private method check the given expression for being a constant
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
270 increase.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
271
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
272 @param expression reference to the expression node
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
273 @type ast.AugAssign
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
274 @return flag indicating a constant increase
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
275 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
276 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
277 return (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
278 isinstance(expression.op, ast.Add) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
279 isinstance(expression.value, (ast.Constant, ast.Num))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
280 )
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
281
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
282 def __getIfBodyPairs(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
283 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
284 Private method to extract a list of pairs of test and body for an
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
285 If node.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
286
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
287 @param node reference to the If node to be processed
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
288 @type ast.If
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
289 @return list of pairs of test and body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
290 @rtype list of tuples of (ast.expr, [ast.stmt])
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
291 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
292 pairs = [(node.test, node.body)]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
293 orelse = node.orelse
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
294 while (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
295 isinstance(orelse, list) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
296 len(orelse) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
297 isinstance(orelse[0], ast.If)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
298 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
299 pairs.append((orelse[0].test, orelse[0].body))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
300 orelse = orelse[0].orelse
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
301 return pairs
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
302
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
303 def __isSameBody(self, body1, body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
304 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
305 Private method check, if the given bodies are equivalent.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
306
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
307 @param body1 list of statements of the first body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
308 @type list of ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
309 @param body2 list of statements of the second body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
310 @type list of ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
311 @return flag indicating identical bodies
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
312 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
313 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
314 if len(body1) != len(body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
315 return False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
316 for a, b in zip(body1, body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
317 try:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
318 statementEqual = self.__isStatementEqual(a, b)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
319 except RecursionError: # maximum recursion depth
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
320 statementEqual = False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
321 if not statementEqual:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
322 return False
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
323
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
324 return True
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
325
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
326 def __isSameExpression(self, a, b):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
327 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
328 Private method to check, if two expressions are equal.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
329
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
330 @param a first expression to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
331 @type ast.expr
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
332 @param b second expression to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
333 @type ast.expr
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
334 @return flag indicating equal expressions
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
335 @rtype bool
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
336 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
337 if isinstance(a, ast.Name) and isinstance(b, ast.Name):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
338 return a.id == b.id
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
339 else:
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
340 return False
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
341
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
342 def __isStatementEqual(self, a, b):
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
343 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
344 Private method to check, if two statements are equal.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
345
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
346 @param a reference to the first statement
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
347 @type ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
348 @param b reference to the second statement
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
349 @type ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
350 @return flag indicating if the two statements are equal
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
351 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
352 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
353 if type(a) is not type(b):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
354 return False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
355
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
356 if isinstance(a, ast.AST):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
357 for k, v in vars(a).items():
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
358 if k in ("lineno", "col_offset", "ctx", "end_lineno",
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
359 "parent"):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
360 continue
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
361 if not self.__isStatementEqual(v, getattr(b, k)):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
362 return False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
363 return True
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
364 elif isinstance(a, list):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
365 return all(itertools.starmap(self.__isStatementEqual, zip(a, b)))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
366 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
367 return a == b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
368
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
369 def __isExceptionCheck(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
370 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
371 Private method to check, if the node is checking an exception.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
372
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
373 @param node reference to the node to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
374 @type ast.If
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
375 @return flag indicating an exception check
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
376 @rtype bool
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
377 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
378 return (
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
379 len(node.body) == 1 and isinstance(node.body[0], ast.Raise)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
380 )
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
381
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
382 def __negateTest(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
383 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
384 Private method negate the given Compare node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
385
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
386 @param node reference to the node to be negated
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
387 @type ast.Compare
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
388 @return node with negated logic
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
389 @rtype ast.Compare
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
390 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
391 newNode = copy.deepcopy(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
392 op = newNode.ops[0]
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
393 if isinstance(op, ast.Eq):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
394 op = ast.NotEq()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
395 elif isinstance(op, ast.NotEq):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
396 op = ast.Eq()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
397 elif isinstance(op, ast.Lt):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
398 op = ast.GtE()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
399 elif isinstance(op, ast.LtE):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
400 op = ast.Gt()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
401 elif isinstance(op, ast.Gt):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
402 op = ast.LtE()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
403 elif isinstance(op, ast.GtE):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
404 op = ast.Lt()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
405 elif isinstance(op, ast.Is):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
406 op = ast.IsNot()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
407 elif isinstance(op, ast.IsNot):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
408 op = ast.Is()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
409 elif isinstance(op, ast.In):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
410 op = ast.NotIn()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
411 elif isinstance(op, ast.NotIn):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
412 op = ast.In()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
413 newNode.ops = [op]
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
414 return newNode
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
415
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 ## Methods to check for possible code simplifications below
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 def __check101(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 Private method to check for duplicate isinstance() calls.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 if isinstance(node.op, ast.Or):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 for variable in self.__getDuplicatedIsinstanceCall(node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 self.__error(node.lineno - 1, node.col_offset, "Y101",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 variable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 def __check102(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 Private method to check for nested if statements without else blocks.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 """
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
439 # Don't treat 'if __name__ == "__main__":' as an issue.
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
440 if (
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
441 isinstance(node.test, ast.Compare) and
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
442 isinstance(node.test.left, ast.Name) and
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
443 node.test.left.id == "__name__" and
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
444 isinstance(node.test.ops[0], ast.Eq) and
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
445 isinstance(node.test.comparators[0], ast.Constant) and
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
446 node.test.comparators[0].value == "__main__"
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
447 ):
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
448 return
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
449
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 # ## Pattern 1
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 # if a: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 # if b: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 # c
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 isPattern1 = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 node.orelse == [] and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 isinstance(node.body[0], ast.If) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 node.body[0].orelse == []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 # ## Pattern 2
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
461 # if a: < irrelevant for here
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 # pass
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 # elif b: <--- this is treated like a nested block
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 # if c: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 # d
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 if isPattern1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 self.__error(node.lineno - 1, node.col_offset, "Y102")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 def __check103(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 Private method to check for calls that wrap a condition to return
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 a bool.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 # if cond:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 not isinstance(node.body[0], ast.Return) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 not isinstance(node.body[0].value, BOOL_CONST_TYPES) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 node.body[0].value.value is True or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 node.body[0].value.value is False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 ) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 len(node.orelse) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 not isinstance(node.orelse[0], ast.Return) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 not isinstance(node.orelse[0].value, BOOL_CONST_TYPES) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 node.orelse[0].value.value is True or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 node.orelse[0].value.value is False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 condition = unparse(node.test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 self.__error(node.lineno - 1, node.col_offset, "Y103", condition)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 def __check104(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 Private method to check for "iterate and yield" patterns.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 # for item in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 # yield item
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 not isinstance(node.body[0], ast.Expr) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 not isinstance(node.body[0].value, ast.Yield) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 not isinstance(node.target, ast.Name) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 not isinstance(node.body[0].value.value, ast.Name) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 node.target.id != node.body[0].value.value.id or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 node.orelse != []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 iterable = unparse(node.iter)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 self.__error(node.lineno - 1, node.col_offset, "Y104", iterable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 def __check105(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 Private method to check for "try-except-pass" patterns.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 # try:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 # foo()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 # except ValueError:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 # pass
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 len(node.handlers) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 not isinstance(node.handlers[0], ast.ExceptHandler) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 len(node.handlers[0].body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 not isinstance(node.handlers[0].body[0], ast.Pass) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 node.orelse != []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 if node.handlers[0].type is None:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 exception = "Exception"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 exception = unparse(node.handlers[0].type)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 self.__error(node.lineno - 1, node.col_offset, "Y105", exception)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 def __check106(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 Private method to check for calls where an exception is raised in else.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 # if cond:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 # raise Exception
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 just_one = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 len(node.orelse) >= 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 isinstance(node.orelse[-1], ast.Raise) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 not isinstance(node.body[-1], ast.Raise)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 many = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 len(node.body) > 2 * len(node.orelse) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 len(node.orelse) >= 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 isinstance(node.orelse[-1], ast.Raise) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 not isinstance(node.body[-1], ast.Raise)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 if just_one or many:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 self.__error(node.lineno - 1, node.col_offset, "Y106")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 def __check107(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 Private method to check for calls where try/except and finally have
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 'return'.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 # def foo():
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 # try:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 # 1 / 0
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 # return "1"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 # except:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 # return "2"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 # finally:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 # return "3"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 tryHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 for stmt in node.body:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 if isinstance(stmt, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 tryHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 break
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 exceptHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 for stmt2 in node.handlers:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 if isinstance(stmt2, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 exceptHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 break
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 finallyHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 finallyReturn = None
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 for stmt in node.finalbody:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 if isinstance(stmt, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 finallyHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 finallyReturn = stmt
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 break
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 if (tryHasReturn or exceptHasReturn) and finallyHasReturn:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 if finallyReturn is not None:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 self.__error(finallyReturn.lineno - 1,
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 finallyReturn.col_offset, "Y107")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 def __check108(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 Private method to check for if-elses which could be a ternary
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 operator assignment.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 # if a:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 # b = c
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 # b = d
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
625 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
626 # but not:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
627 # if a:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
628 # b = c
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
629 # elif c:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
630 # b = e
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
631 # else:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
632 # b = d
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 if (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 len(node.orelse) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 isinstance(node.body[0], ast.Assign) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 isinstance(node.orelse[0], ast.Assign) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 len(node.body[0].targets) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 len(node.orelse[0].targets) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 isinstance(node.body[0].targets[0], ast.Name) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 isinstance(node.orelse[0].targets[0], ast.Name) and
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
642 node.body[0].targets[0].id == node.orelse[0].targets[0].id and
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
643 not isinstance(node.parent, ast.If)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 assign = unparse(node.body[0].targets[0])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 body = unparse(node.body[0].value)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 cond = unparse(node.test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 orelse = unparse(node.orelse[0].value)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
649 if len(
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
650 "{0} = {1} if {2} else {3}".format(assign, body, cond, orelse)
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
651 ) < 79:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
652 # don't flag an issue, if the ternary would get too complicated
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
653 self.__error(node.lineno - 1, node.col_offset, "Y108",
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
654 assign, body, cond, orelse)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 def __check109(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 Private method to check for multiple equalities with the same value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 are combined via "or".
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 # if a == b or a == c:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 # d
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 if isinstance(node.op, ast.Or):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 equalities = [
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 for value in node.values
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 if isinstance(value, ast.Compare) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 len(value.ops) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 isinstance(value.ops[0], ast.Eq)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 ]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 ids = [] # (name, compared_to)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 for eq in equalities:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 if isinstance(eq.left, ast.Name):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 ids.append((eq.left, eq.comparators[0]))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 if (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 len(eq.comparators) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 isinstance(eq.comparators[0], ast.Name)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 ids.append((eq.comparators[0], eq.left))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 id2count = {}
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 for identifier, comparedTo in ids:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 if identifier.id not in id2count:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 id2count[identifier.id] = []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 id2count[identifier.id].append(comparedTo)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 for value, values in id2count.items():
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 if len(values) == 1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 continue
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 self.__error(node.lineno - 1, node.col_offset, "Y109",
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8204
diff changeset
694 value, unparse(ast.Tuple(elts=values)),
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 unparse(node))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 def __check110_111(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 Private method to check if any / all could be used.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 # for x in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 # if check(x):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 #
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 # for x in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 # if check(x):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 if (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
714 len(node.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
715 isinstance(node.body[0], ast.If) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
716 len(node.body[0].body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
717 isinstance(node.body[0].body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
718 isinstance(node.body[0].body[0].value, BOOL_CONST_TYPES) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
719 hasattr(node.body[0].body[0].value, "value")
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 check = unparse(node.body[0].test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 target = unparse(node.target)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 iterable = unparse(node.iter)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 if node.body[0].body[0].value.value is True:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 self.__error(node.lineno - 1, node.col_offset, "Y110",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 check, target, iterable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 elif node.body[0].body[0].value.value is False:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 check = "not " + check
8211
8322a6f219ff Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8210
diff changeset
729 if check.startswith("not not "):
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 check = check[len("not not "):]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 self.__error(node.lineno - 1, node.col_offset, "Y111",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 check, target, iterable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 def __check112(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 """
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
736 Private method to check for non-capitalized calls to environment
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 variables.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 @type ast.Expr
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 # os.environ["foo"]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 # os.environ.get("bar")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 isIndexCall = (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
745 isinstance(node.value, ast.Subscript) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
746 isinstance(node.value.value, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
747 isinstance(node.value.value.value, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
748 node.value.value.value.id == "os" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
749 node.value.value.attr == "environ" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
750 (
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
752 isinstance(node.value.slice, ast.Index) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
753 isinstance(node.value.slice.value, STR_TYPES)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
754 ) or
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
755 isinstance(node.value.slice, ast.Constant)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 if isIndexCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 subscript = node.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 slice_ = subscript.slice
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 if isinstance(slice_, ast.Index):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 # Python < 3.9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 stringPart = slice_.value # type: ignore
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 if isinstance(stringPart, ast.Str):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 envName = stringPart.s # Python 3.6 / 3.7 fallback
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 envName = stringPart.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 elif isinstance(slice_, ast.Constant):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 # Python 3.9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 envName = slice_.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 # Check if this has a change
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 hasChange = envName != envName.upper()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 isGetCall = (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
776 isinstance(node.value, ast.Call) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
777 isinstance(node.value.func, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
778 isinstance(node.value.func.value, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
779 isinstance(node.value.func.value.value, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
780 node.value.func.value.value.id == "os" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
781 node.value.func.value.attr == "environ" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
782 node.value.func.attr == "get" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
783 len(node.value.args) in [1, 2] and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
784 isinstance(node.value.args[0], STR_TYPES)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 if isGetCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 call = node.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 stringPart = call.args[0]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 if isinstance(stringPart, ast.Str):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 envName = stringPart.s # Python 3.6 / 3.7 fallback
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 envName = stringPart.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 # Check if this has a change
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 hasChange = envName != envName.upper()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 if not (isIndexCall or isGetCall) or not hasChange:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 return
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 if isIndexCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 original = unparse(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 expected = f"os.environ['{envName.upper()}']"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 elif isGetCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 original = unparse(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 if len(node.value.args) == 1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 expected = f"os.environ.get('{envName.upper()}')"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 defaultValue = unparse(node.value.args[1])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 expected = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 f"os.environ.get('{envName.upper()}', '{defaultValue}')"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 return
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 self.__error(node.lineno - 1, node.col_offset, "Y112", expected,
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 original)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
814
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
815 def __check113(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
816 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
817 Private method to check for loops in which "enumerate" should be
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
818 used.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
819
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
820 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
821 @type ast.For
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
822 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
823 # idx = 0
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
824 # for el in iterable:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
825 # ...
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
826 # idx += 1
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
827 variableCandidates = []
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
828 for expression in node.body:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
829 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
830 isinstance(expression, ast.AugAssign) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
831 self.__isConstantIncrease(expression) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
832 isinstance(expression.target, ast.Name)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
833 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
834 variableCandidates.append(expression.target)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
835
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
836 for candidate in variableCandidates:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
837 self.__error(candidate.lineno - 1, candidate.col_offset, "Y113",
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
838 unparse(candidate))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
839
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
840 def __check114(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
841 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
842 Private method to check for alternative if clauses with identical
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
843 bodies.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
844
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
845 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
846 @type ast.If
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
847 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
848 # if a:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
849 # b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
850 # elif c:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
851 # b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
852 ifBodyPairs = self.__getIfBodyPairs(node)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
853 errorPairs = []
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
854 for ifbody1, ifbody2 in itertools.combinations(ifBodyPairs, 2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
855 if self.__isSameBody(ifbody1[1], ifbody2[1]):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
856 errorPairs.append((ifbody1, ifbody2))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
857 for ifbody1, ifbody2 in errorPairs:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
858 self.__error(ifbody1[0].lineno - 1, ifbody1[0].col_offset, "Y114",
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
859 unparse(ifbody1[0]), unparse(ifbody2[0]))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
860
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
861 def __check115(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
862 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
863 Private method to to check for places where open() is called without
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
864 a context handler.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
865
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
866 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
867 @type ast.Call
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
868 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
869 # f = open(...)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
870 #. .. # (do something with f)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
871 # f.close()
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
872 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
873 isinstance(node.func, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
874 node.func.id == "open" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
875 not isinstance(node.parent, ast.withitem)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
876 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
877 self.__error(node.lineno - 1, node.col_offset, "Y115")
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
878
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
879 def __check116(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
880 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
881 Private method to check for places with 3 or more consecutive
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
882 if-statements with direct returns.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
883
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
884 * Each if-statement must be a check for equality with the
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
885 same variable
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
886 * Each if-statement must just have a "return"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
887 * Else must also just have a return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
888
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
889 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
890 @type ast.If
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
891 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
892 # if a == "foo":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
893 # return "bar"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
894 # elif a == "bar":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
895 # return "baz"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
896 # elif a == "boo":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
897 # return "ooh"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
898 # else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
899 # return 42
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
900 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
901 isinstance(node.test, ast.Compare) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
902 isinstance(node.test.left, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
903 len(node.test.ops) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
904 isinstance(node.test.ops[0], ast.Eq) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
905 len(node.test.comparators) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
906 isinstance(node.test.comparators[0], AST_CONST_TYPES) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
907 len(node.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
908 isinstance(node.body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
909 len(node.orelse) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
910 isinstance(node.orelse[0], ast.If)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
911 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
912 variable = node.test.left
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
913 child = node.orelse[0]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
914 elseValue = None
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
915 if node.body[0].value is not None:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
916 bodyValueStr = unparse(node.body[0].value).strip("'")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
917 else:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
918 bodyValueStr = "None"
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
919 if isinstance(node.test.comparators[0], ast.Str):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
920 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
921 node.test.comparators[0].s: bodyValueStr
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
922 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
923 elif isinstance(node.test.comparators[0], ast.Num):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
924 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
925 node.test.comparators[0].n: bodyValueStr,
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
926 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
927 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
928 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
929 node.test.comparators[0].value: bodyValueStr
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
930 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
931 while child:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
932 if not (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
933 isinstance(child.test, ast.Compare) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
934 isinstance(child.test.left, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
935 child.test.left.id == variable.id and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
936 len(child.test.ops) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
937 isinstance(child.test.ops[0], ast.Eq) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
938 len(child.test.comparators) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
939 isinstance(child.test.comparators[0], AST_CONST_TYPES) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
940 len(child.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
941 isinstance(child.body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
942 len(child.orelse) <= 1
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
943 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
944 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
945
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
946 if isinstance(child.test.comparators[0], ast.Str):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
947 key = child.test.comparators[0].s
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
948 elif isinstance(child.test.comparators[0], ast.Num):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
949 key = child.test.comparators[0].n
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
950 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
951 key = child.test.comparators[0].value
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
952 keyValuePairs[key] = unparse(child.body[0].value).strip("'")
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
953 if len(child.orelse) == 1:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
954 if isinstance(child.orelse[0], ast.If):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
955 child = child.orelse[0]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
956 elif isinstance(child.orelse[0], ast.Return):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
957 elseValue = unparse(child.orelse[0].value)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
958 child = None
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
959 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
960 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
961 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
962 child = None
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
963
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
964 if len(keyValuePairs) < 3:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
965 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
966
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
967 if elseValue:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
968 ret = f"{keyValuePairs}.get({variable.id}, {elseValue})"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
969 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
970 ret = f"{keyValuePairs}.get({variable.id})"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
971
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
972 self.__error(node.lineno - 1, node.col_offset, "Y116", ret)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
973
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
974 def __check117(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
975 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
976 Private method to check for multiple with-statements with same scope.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
977
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
978 @param node reference to the AST node to be checked
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
979 @type ast.With
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
980 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
981 # with A() as a:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
982 # with B() as b:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
983 # print("hello")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
984 if (
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
985 len(node.body) == 1 and
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
986 isinstance(node.body[0], ast.With)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
987 ):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
988 withItems = []
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
989 for withitem in node.items + node.body[0].items:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
990 withItems.append(f"{unparse(withitem)}")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
991 mergedWith = f"with {', '.join(withItems)}:"
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
992 self.__error(node.lineno - 1, node.col_offset, "Y117", mergedWith)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
993
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
994 def __check118(self, node):
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
995 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
996 Private method to check for usages of "key in dict.keys()".
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
997
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
998 @param node reference to the AST node to be checked
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
999 @type ast.Compare or ast.For
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1000 """
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1001 # Pattern 1:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1002 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1003 # if key in dict.keys():
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1004 # # do something
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1005 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1006 # Pattern 2:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1007 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1008 # for key in dict.keys():
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1009 # # do something
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1010 if (
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1011 isinstance(node, ast.Compare) and
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1012 len(node.ops) == 1 and
e1157bd8b4c2 Code Style