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

Thu, 08 Apr 2021 17:27:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 08 Apr 2021 17:27:12 +0200
changeset 8204
fd477cded1c1
parent 8202
df194f43119c
child 8205
4a0f1f896341
permissions
-rw-r--r--

Corrected some code style issues.

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