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

Sun, 11 Apr 2021 11:34:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 11 Apr 2021 11:34:32 +0200
changeset 8219
013aee248a62
parent 8218
7c09585bd960
child 8241
de2ebf20d822
permissions
-rw-r--r--

SimplifyNodeVisitor: fixed Y113 to not fire on augmented assignments with non-integer constants.

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