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

Sat, 10 Apr 2021 12:34:29 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 10 Apr 2021 12:34:29 +0200
changeset 8209
14470a65a52e
parent 8205
4a0f1f896341
child 8210
b5903eaa7a7b
permissions
-rw-r--r--

Code Style Checker
- added a check to help simplifying 'super(Foo, self)' to just 'super()'

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)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
126 def visit_IfExp(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
127 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
128 Public method to process an IfExp node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
129
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
130 @param node reference to the IfExp node
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
131 @type ast.IfExp
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
132 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
133 self.__check211(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
134 self.__check212(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
135 self.__check213(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
136
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
137 self.generic_visit(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
138
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 def visit_For(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 Public method to process a For node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 @param node reference to the For node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 self.__check104(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__check110_111(node)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
148 self.__check113(node)
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
149 self.__check118(node)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 def visit_Try(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 Public method to process a Try node.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 @param node reference to the Try node
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 self.__check105(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 self.__check107(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 self.generic_visit(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
165 def visit_Call(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
166 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
167 Public method to process a Call node.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
168
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
169 @param node reference to the Call node
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
170 @type ast.Call
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
171 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
172 self.__check115(node)
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
173 self.__check182(node)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
174
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
175 self.generic_visit(node)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
176
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
177 def visit_With(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
178 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
179 Public method to process a With node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
180
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
181 @param node reference to the With node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
182 @type ast.With
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 self.__check117(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
185
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
186 self.generic_visit(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
187
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
188 def visit_Compare(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
189 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
190 Public method to process a Compare node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
191
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
192 @param node reference to the Compare node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
193 @type ast.Compare
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
194 """
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
195 self.__check118(node)
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
196 self.__check301(node)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
197
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
198 self.generic_visit(node)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
199
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
200 def visit_ClassDef(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
201 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
202 Public method to process a ClassDef node.
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
203
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
204 @param node reference to the ClassDef node
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
205 @type ast.ClassDef
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
206 """
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
207 # register the name of the class being defined
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
208 self.__classDefinitionStack.append(node.name)
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
209
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
210 self.__check119(node)
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
211 self.__check120(node)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
212
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
213 self.generic_visit(node)
8209
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
214
14470a65a52e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8205
diff changeset
215 self.__classDefinitionStack.pop()
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
216
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
217 def visit_UnaryOp(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
218 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
219 Public method to process a UnaryOp node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
220
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
221 @param node reference to the UnaryOp node
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
222 @type ast.UnaryOp
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 self.__check201(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
225 self.__check202(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
226 self.__check203(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
227 self.__check204(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
228 self.__check205(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
229 self.__check206(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
230 self.__check207(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
231 self.__check208(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
232
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
233 self.generic_visit(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
234
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 ## Helper methods for the various checkers below
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 #############################################################
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 def __getDuplicatedIsinstanceCall(self, node):
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 Private method to get a list of isinstance arguments which could
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 be combined.
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 @param node reference to the AST node to be inspected
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 @return list of variable names of duplicated isinstance calls
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 @rtype list of str
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 counter = collections.defaultdict(int)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 for call in node.values:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 # Ensure this is a call of the built-in isinstance() function.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 if not isinstance(call, ast.Call) or len(call.args) != 2:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 continue
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 functionName = unparse(call.func)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 if functionName != "isinstance":
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
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 arg0Name = unparse(call.args[0])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 counter[arg0Name] += 1
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 return [name for name, count in counter.items() if count > 1]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
264 def __isConstantIncrease(self, expression):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
265 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
266 Private method check the given expression for being a constant
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
267 increase.
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 @param expression reference to the expression node
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
270 @type ast.AugAssign
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
271 @return flag indicating a constant increase
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
272 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
273 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
274 return (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
275 isinstance(expression.op, ast.Add) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
276 isinstance(expression.value, (ast.Constant, ast.Num))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
277 )
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
278
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
279 def __getIfBodyPairs(self, node):
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 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
282 If 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 @param node reference to the If node to be processed
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
285 @type ast.If
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
286 @return list of pairs of test and body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
287 @rtype list of tuples of (ast.expr, [ast.stmt])
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
288 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
289 pairs = [(node.test, node.body)]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
290 orelse = node.orelse
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
291 while (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
292 isinstance(orelse, list) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
293 len(orelse) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
294 isinstance(orelse[0], ast.If)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
295 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
296 pairs.append((orelse[0].test, orelse[0].body))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
297 orelse = orelse[0].orelse
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
298 return pairs
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
299
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
300 def __isSameBody(self, body1, body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
301 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
302 Private method check, if the given bodies are equivalent.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
303
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
304 @param body1 list of statements of the first body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
305 @type list of ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
306 @param body2 list of statements of the second body
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
307 @type list of ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
308 @return flag indicating identical bodies
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
309 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
310 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
311 if len(body1) != len(body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
312 return False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
313 for a, b in zip(body1, body2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
314 try:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
315 statementEqual = self.__isStatementEqual(a, b)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
316 except RecursionError: # maximum recursion depth
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
317 statementEqual = False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
318 if not statementEqual:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
319 return False
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
320
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
321 return True
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
322
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
323 def __isSameExpression(self, a, b):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
324 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
325 Private method to check, if two expressions are equal.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
326
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
327 @param a first expression to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
328 @type ast.expr
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
329 @param b second expression to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
330 @type ast.expr
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
331 @return flag indicating equal expressions
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
332 @rtype bool
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
333 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
334 if isinstance(a, ast.Name) and isinstance(b, ast.Name):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
335 return a.id == b.id
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
336 else:
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
337 return False
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
338
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
339 def __isStatementEqual(self, a, b):
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
340 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
341 Private method to check, if two statements are equal.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
342
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
343 @param a reference to the first statement
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
344 @type ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
345 @param b reference to the second statement
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
346 @type ast.stmt
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
347 @return flag indicating if the two statements are equal
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
348 @rtype bool
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
349 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
350 if type(a) is not type(b):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
351 return False
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 isinstance(a, ast.AST):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
354 for k, v in vars(a).items():
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
355 if k in ("lineno", "col_offset", "ctx", "end_lineno",
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
356 "parent"):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
357 continue
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
358 if not self.__isStatementEqual(v, getattr(b, k)):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
359 return False
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
360 return True
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
361 elif isinstance(a, list):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
362 return all(itertools.starmap(self.__isStatementEqual, zip(a, b)))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
363 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
364 return a == b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
365
8195
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
366 def __isExceptionCheck(self, node):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
367 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
368 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
369
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
370 @param node reference to the node to be checked
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
371 @type ast.If
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
372 @return flag indicating an exception check
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
373 @rtype bool
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
374 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
375 return (
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
376 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
377 )
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
378
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
379 def __negateTest(self, node):
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 Private method negate the given Compare node.
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
382
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
383 @param node reference to the node to be negated
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
384 @type ast.Compare
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
385 @return node with negated logic
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
386 @rtype ast.Compare
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
387 """
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
388 newNode = copy.deepcopy(node)
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
389 op = newNode.ops[0]
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
390 if isinstance(op, ast.Eq):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
391 op = ast.NotEq()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
392 elif isinstance(op, ast.NotEq):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
393 op = ast.Eq()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
394 elif isinstance(op, ast.Lt):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
395 op = ast.GtE()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
396 elif isinstance(op, ast.LtE):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
397 op = ast.Gt()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
398 elif isinstance(op, ast.Gt):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
399 op = ast.LtE()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
400 elif isinstance(op, ast.GtE):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
401 op = ast.Lt()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
402 elif isinstance(op, ast.Is):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
403 op = ast.IsNot()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
404 elif isinstance(op, ast.IsNot):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
405 op = ast.Is()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
406 elif isinstance(op, ast.In):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
407 op = ast.NotIn()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
408 elif isinstance(op, ast.NotIn):
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
409 op = ast.In()
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
410 newNode.ops = [op]
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
411 return newNode
db7f2badd374 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8194
diff changeset
412
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 #############################################################
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 ## Methods to check for possible code simplifications below
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 #############################################################
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 def __check101(self, node):
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 Private method to check for duplicate isinstance() calls.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 @type ast.BoolOp
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 if isinstance(node.op, ast.Or):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 for variable in self.__getDuplicatedIsinstanceCall(node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 self.__error(node.lineno - 1, node.col_offset, "Y101",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 variable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 def __check102(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 Private method to check for nested if statements without else blocks.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 @type ast.If
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 # ## Pattern 1
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 # if a: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 # if b: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 # c
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 isPattern1 = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 node.orelse == [] and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 isinstance(node.body[0], ast.If) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 node.body[0].orelse == []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 # ## Pattern 2
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 # if a: < irrelvant for here
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 # pass
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 # elif b: <--- this is treated like a nested block
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 # if c: <---
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 # d
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 if isPattern1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 self.__error(node.lineno - 1, node.col_offset, "Y102")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 def __check103(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 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
458 a bool.
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 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 # if cond:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 not isinstance(node.body[0], ast.Return) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 not isinstance(node.body[0].value, BOOL_CONST_TYPES) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 node.body[0].value.value is True or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 node.body[0].value.value is False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 ) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 len(node.orelse) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 not isinstance(node.orelse[0], ast.Return) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 not isinstance(node.orelse[0].value, BOOL_CONST_TYPES) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 node.orelse[0].value.value is True or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 node.orelse[0].value.value is False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 condition = unparse(node.test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 self.__error(node.lineno - 1, node.col_offset, "Y103", condition)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 def __check104(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 Private method to check for "iterate and yield" patterns.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 # for item in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 # yield item
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 not isinstance(node.body[0], ast.Expr) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 not isinstance(node.body[0].value, ast.Yield) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 not isinstance(node.target, ast.Name) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 not isinstance(node.body[0].value.value, ast.Name) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 node.target.id != node.body[0].value.value.id or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 node.orelse != []
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 iterable = unparse(node.iter)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 self.__error(node.lineno - 1, node.col_offset, "Y104", iterable)
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 def __check105(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 Private method to check for "try-except-pass" patterns.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 # try:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 # foo()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 # except ValueError:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 # pass
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 if not (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 len(node.body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 len(node.handlers) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 not isinstance(node.handlers[0], ast.ExceptHandler) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 len(node.handlers[0].body) != 1 or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 not isinstance(node.handlers[0].body[0], ast.Pass) or
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 node.orelse != []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 if node.handlers[0].type is None:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 exception = "Exception"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 exception = unparse(node.handlers[0].type)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 self.__error(node.lineno - 1, node.col_offset, "Y105", exception)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 def __check106(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 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
535
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 # if cond:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 # raise Exception
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 just_one = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 len(node.orelse) >= 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 isinstance(node.orelse[-1], ast.Raise) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 not isinstance(node.body[-1], ast.Raise)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 many = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 len(node.body) > 2 * len(node.orelse) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 len(node.orelse) >= 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 isinstance(node.orelse[-1], ast.Raise) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 not isinstance(node.body[-1], ast.Raise)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 if just_one or many:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 self.__error(node.lineno - 1, node.col_offset, "Y106")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 def __check107(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 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
561 'return'.
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 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 @type ast.Try
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 # def foo():
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 # try:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 # 1 / 0
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 # return "1"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 # except:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 # return "2"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 # finally:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 # return "3"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 tryHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 for stmt in node.body:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576 if isinstance(stmt, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 tryHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 break
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 exceptHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 for stmt2 in node.handlers:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 if isinstance(stmt2, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 exceptHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 break
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 finallyHasReturn = False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 finallyReturn = None
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 for stmt in node.finalbody:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 if isinstance(stmt, ast.Return):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 finallyHasReturn = True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 finallyReturn = stmt
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 if (tryHasReturn or exceptHasReturn) and finallyHasReturn:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 if finallyReturn is not None:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 self.__error(finallyReturn.lineno - 1,
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 finallyReturn.col_offset, "Y107")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 def __check108(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 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
602 operator assignment.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 @type ast.If
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 # if a:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 # b = c
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 # else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 # b = d
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
611 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
612 # but not:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
613 # if a:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
614 # b = c
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
615 # elif c:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
616 # b = e
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
617 # else:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
618 # b = d
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 if (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 len(node.body) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 len(node.orelse) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 isinstance(node.body[0], ast.Assign) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 isinstance(node.orelse[0], ast.Assign) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 len(node.body[0].targets) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 len(node.orelse[0].targets) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 isinstance(node.body[0].targets[0], ast.Name) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 isinstance(node.orelse[0].targets[0], ast.Name) and
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
628 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
629 not isinstance(node.parent, ast.If)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 assign = unparse(node.body[0].targets[0])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 body = unparse(node.body[0].value)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 cond = unparse(node.test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 orelse = unparse(node.orelse[0].value)
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
635 if len(
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
636 "{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
637 ) < 79:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
638 # 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
639 self.__error(node.lineno - 1, node.col_offset, "Y108",
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
640 assign, body, cond, orelse)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 def __check109(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 Private method to check for multiple equalities with the same value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 are combined via "or".
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 @type ast.BoolOp
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 # if a == b or a == c:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 # d
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 if isinstance(node.op, ast.Or):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 equalities = [
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 for value in node.values
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 if isinstance(value, ast.Compare) and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 len(value.ops) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 isinstance(value.ops[0], ast.Eq)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 ]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 ids = [] # (name, compared_to)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 for eq in equalities:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 if isinstance(eq.left, ast.Name):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 ids.append((eq.left, eq.comparators[0]))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 if (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 len(eq.comparators) == 1 and
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 isinstance(eq.comparators[0], ast.Name)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 ids.append((eq.comparators[0], eq.left))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 id2count = {}
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 for identifier, comparedTo in ids:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 if identifier.id not in id2count:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 id2count[identifier.id] = []
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 id2count[identifier.id].append(comparedTo)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 for value, values in id2count.items():
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 if len(values) == 1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 continue
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 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
680 value, unparse(ast.Tuple(elts=values)),
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 unparse(node))
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 def __check110_111(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 Private method to check if any / all could be used.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 @type ast.For
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 # for x in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 # if check(x):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 #
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 # for x in iterable:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 # if check(x):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 # return False
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 # return True
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 if (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
700 len(node.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
701 isinstance(node.body[0], ast.If) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
702 len(node.body[0].body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
703 isinstance(node.body[0].body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
704 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
705 hasattr(node.body[0].body[0].value, "value")
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 ):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 check = unparse(node.body[0].test)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 target = unparse(node.target)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 iterable = unparse(node.iter)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 if node.body[0].body[0].value.value is True:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 self.__error(node.lineno - 1, node.col_offset, "Y110",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 check, target, iterable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 elif node.body[0].body[0].value.value is False:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 check = "not " + check
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 if check.startswith("not not"):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 check = check[len("not not "):]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 self.__error(node.lineno - 1, node.col_offset, "Y111",
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 check, target, iterable)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 def __check112(self, node):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 """
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
722 Private method to check for non-capitalized calls to environment
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 variables.
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 @param node reference to the AST node to be checked
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 @type ast.Expr
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 """
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 # os.environ["foo"]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 # os.environ.get("bar")
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 isIndexCall = (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
731 isinstance(node.value, ast.Subscript) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
732 isinstance(node.value.value, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
733 isinstance(node.value.value.value, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
734 node.value.value.value.id == "os" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
735 node.value.value.attr == "environ" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
736 (
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
738 isinstance(node.value.slice, ast.Index) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
739 isinstance(node.value.slice.value, STR_TYPES)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
740 ) or
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
741 isinstance(node.value.slice, ast.Constant)
8189
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744 if isIndexCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 subscript = node.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 slice_ = subscript.slice
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 if isinstance(slice_, ast.Index):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 # Python < 3.9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 stringPart = slice_.value # type: ignore
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 if isinstance(stringPart, ast.Str):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 envName = stringPart.s # Python 3.6 / 3.7 fallback
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 envName = stringPart.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 elif isinstance(slice_, ast.Constant):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 # Python 3.9
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 envName = slice_.value
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 # Check if this has a change
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 hasChange = envName != envName.upper()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 isGetCall = (
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
762 isinstance(node.value, ast.Call) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
763 isinstance(node.value.func, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
764 isinstance(node.value.func.value, ast.Attribute) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
765 isinstance(node.value.func.value.value, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
766 node.value.func.value.value.id == "os" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
767 node.value.func.value.attr == "environ" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
768 node.value.func.attr == "get" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
769 len(node.value.args) in [1, 2] and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
770 isinstance(node.value.args[0], STR_TYPES)
8189
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 if isGetCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 call = node.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 stringPart = call.args[0]
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 if isinstance(stringPart, ast.Str):
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 envName = stringPart.s # Python 3.6 / 3.7 fallback
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 envName = stringPart.value
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 # Check if this has a change
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 hasChange = envName != envName.upper()
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 if not (isIndexCall or isGetCall) or not hasChange:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 return
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 if isIndexCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 original = unparse(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 expected = f"os.environ['{envName.upper()}']"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 elif isGetCall:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 original = unparse(node)
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 if len(node.value.args) == 1:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 expected = f"os.environ.get('{envName.upper()}')"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 else:
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 defaultValue = unparse(node.value.args[1])
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 expected = (
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 f"os.environ.get('{envName.upper()}', '{defaultValue}')"
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 )
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 else:
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
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 self.__error(node.lineno - 1, node.col_offset, "Y112", expected,
17df5c8df8c1 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 original)
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
800
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
801 def __check113(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
802 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
803 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
804 used.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
805
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
806 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
807 @type ast.For
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
808 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
809 # idx = 0
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
810 # for el in iterable:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
811 # ...
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
812 # idx += 1
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
813 variableCandidates = []
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
814 for expression in node.body:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
815 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
816 isinstance(expression, ast.AugAssign) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
817 self.__isConstantIncrease(expression) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
818 isinstance(expression.target, ast.Name)
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 variableCandidates.append(expression.target)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
821
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
822 for candidate in variableCandidates:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
823 self.__error(candidate.lineno - 1, candidate.col_offset, "Y113",
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
824 unparse(candidate))
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 def __check114(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
827 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
828 Private method to check for alternative if clauses with identical
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
829 bodies.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
830
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
831 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
832 @type ast.If
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 # if a:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
835 # b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
836 # elif c:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
837 # b
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
838 ifBodyPairs = self.__getIfBodyPairs(node)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
839 errorPairs = []
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
840 for ifbody1, ifbody2 in itertools.combinations(ifBodyPairs, 2):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
841 if self.__isSameBody(ifbody1[1], ifbody2[1]):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
842 errorPairs.append((ifbody1, ifbody2))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
843 for ifbody1, ifbody2 in errorPairs:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
844 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
845 unparse(ifbody1[0]), unparse(ifbody2[0]))
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
846
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
847 def __check115(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
848 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
849 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
850 a context handler.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
851
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
852 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
853 @type ast.Call
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
854 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
855 # f = open(...)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
856 #. .. # (do something with f)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
857 # f.close()
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
858 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
859 isinstance(node.func, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
860 node.func.id == "open" and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
861 not isinstance(node.parent, ast.withitem)
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 self.__error(node.lineno - 1, node.col_offset, "Y115")
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
864
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
865 def __check116(self, node):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
866 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
867 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
868 if-statements with direct returns.
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
869
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
870 * 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
871 same variable
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
872 * Each if-statement must just have a "return"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
873 * Else must also just have a return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
874
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
875 @param node reference to the AST node to be checked
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
876 @type ast.If
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
877 """
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
878 # if a == "foo":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
879 # return "bar"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
880 # elif a == "bar":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
881 # return "baz"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
882 # elif a == "boo":
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
883 # return "ooh"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
884 # else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
885 # return 42
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
886 if (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
887 isinstance(node.test, ast.Compare) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
888 isinstance(node.test.left, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
889 len(node.test.ops) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
890 isinstance(node.test.ops[0], ast.Eq) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
891 len(node.test.comparators) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
892 isinstance(node.test.comparators[0], AST_CONST_TYPES) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
893 len(node.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
894 isinstance(node.body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
895 len(node.orelse) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
896 isinstance(node.orelse[0], ast.If)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
897 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
898 variable = node.test.left
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
899 child = node.orelse[0]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
900 elseValue = None
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
901 if node.body[0].value is not None:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
902 bodyValueStr = unparse(node.body[0].value).strip("'")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
903 else:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
904 bodyValueStr = "None"
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
905 if isinstance(node.test.comparators[0], ast.Str):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
906 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
907 node.test.comparators[0].s: bodyValueStr
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
908 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
909 elif isinstance(node.test.comparators[0], ast.Num):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
910 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
911 node.test.comparators[0].n: bodyValueStr,
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
912 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
913 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
914 keyValuePairs = {
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
915 node.test.comparators[0].value: bodyValueStr
8191
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
916 }
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
917 while child:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
918 if not (
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
919 isinstance(child.test, ast.Compare) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
920 isinstance(child.test.left, ast.Name) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
921 child.test.left.id == variable.id and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
922 len(child.test.ops) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
923 isinstance(child.test.ops[0], ast.Eq) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
924 len(child.test.comparators) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
925 isinstance(child.test.comparators[0], AST_CONST_TYPES) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
926 len(child.body) == 1 and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
927 isinstance(child.body[0], ast.Return) and
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
928 len(child.orelse) <= 1
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
929 ):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
930 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
931
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
932 if isinstance(child.test.comparators[0], ast.Str):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
933 key = child.test.comparators[0].s
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
934 elif isinstance(child.test.comparators[0], ast.Num):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
935 key = child.test.comparators[0].n
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
936 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
937 key = child.test.comparators[0].value
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
938 keyValuePairs[key] = unparse(child.body[0].value).strip("'")
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
939 if len(child.orelse) == 1:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
940 if isinstance(child.orelse[0], ast.If):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
941 child = child.orelse[0]
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
942 elif isinstance(child.orelse[0], ast.Return):
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
943 elseValue = unparse(child.orelse[0].value)
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
944 child = None
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
945 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
946 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
947 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
948 child = None
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
949
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
950 if len(keyValuePairs) < 3:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
951 return
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
952
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
953 if elseValue:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
954 ret = f"{keyValuePairs}.get({variable.id}, {elseValue})"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
955 else:
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
956 ret = f"{keyValuePairs}.get({variable.id})"
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
957
9125da0c227e Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8189
diff changeset
958 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
959
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
960 def __check117(self, node):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
961 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
962 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
963
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
964 @param node reference to the AST node to be checked
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
965 @type ast.With
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
966 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
967 # with A() as a:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
968 # with B() as b:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
969 # print("hello")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
970 if (
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
971 len(node.body) == 1 and
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
972 isinstance(node.body[0], ast.With)
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 withItems = []
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
975 for withitem in node.items + node.body[0].items:
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
976 withItems.append(f"{unparse(withitem)}")
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
977 mergedWith = f"with {', '.join(withItems)}:"
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
978 self.__error(node.lineno - 1, node.col_offset, "Y117", mergedWith)
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
979
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
980 def __check118(self, node):
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
981 """
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
982 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
983
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
984 @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
985 @type ast.Compare or ast.For
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
986 """
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
987 # Pattern 1:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
988 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
989 # if key in dict.keys():
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
990 # # do something
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
991 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
992 # Pattern 2:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
993 #
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
994 # for key in dict.keys():
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
995 # # do something
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
996 if (
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
997 isinstance(node, ast.Compare) and
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
998 len(node.ops) == 1 and
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
999 isinstance(node.ops[0], ast.In) and
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1000 len(node.comparators) == 1
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1001 ):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1002 callNode = node.comparators[0]
8194
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1003 elif (
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1004 isinstance(node, ast.For)
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 callNode = node.iter
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1007 else:
b925628bf91f Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8192
diff changeset
1008 callNode = None
8192
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1009
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1010 if not isinstance(callNode, ast.Call):
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1011 return
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1012
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1013 attrNode = callNode.func
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8191
diff changeset
1014 if (
e1157bd8b4c2 Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de></