|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the checker for simplifying Python code. |
|
8 """ |
|
9 |
|
10 import ast |
|
11 import collections |
|
12 import sys |
|
13 |
|
14 try: |
|
15 from ast import unparse |
|
16 except AttributeError: |
|
17 # Python < 3.9 |
|
18 from .ast_unparse import unparse |
|
19 |
|
20 |
|
21 class SimplifyChecker(object): |
|
22 """ |
|
23 Class implementing a checker for to help simplifying Python code. |
|
24 """ |
|
25 Codes = [ |
|
26 "Y101", |
|
27 ] |
|
28 |
|
29 def __init__(self, source, filename, selected, ignored, expected, repeat): |
|
30 """ |
|
31 Constructor |
|
32 |
|
33 @param source source code to be checked |
|
34 @type list of str |
|
35 @param filename name of the source file |
|
36 @type str |
|
37 @param selected list of selected codes |
|
38 @type list of str |
|
39 @param ignored list of codes to be ignored |
|
40 @type list of str |
|
41 @param expected list of expected codes |
|
42 @type list of str |
|
43 @param repeat flag indicating to report each occurrence of a code |
|
44 @type bool |
|
45 """ |
|
46 self.__select = tuple(selected) |
|
47 self.__ignore = ('',) if selected else tuple(ignored) |
|
48 self.__expected = expected[:] |
|
49 self.__repeat = repeat |
|
50 self.__filename = filename |
|
51 self.__source = source[:] |
|
52 |
|
53 # statistics counters |
|
54 self.counters = {} |
|
55 |
|
56 # collection of detected errors |
|
57 self.errors = [] |
|
58 |
|
59 self.__checkCodes = (code for code in self.Codes |
|
60 if not self.__ignoreCode(code)) |
|
61 |
|
62 def __ignoreCode(self, code): |
|
63 """ |
|
64 Private method to check if the message code should be ignored. |
|
65 |
|
66 @param code message code to check for |
|
67 @type str |
|
68 @return flag indicating to ignore the given code |
|
69 @rtype bool |
|
70 """ |
|
71 return (code.startswith(self.__ignore) and |
|
72 not code.startswith(self.__select)) |
|
73 |
|
74 def __error(self, lineNumber, offset, code, *args): |
|
75 """ |
|
76 Private method to record an issue. |
|
77 |
|
78 @param lineNumber line number of the issue |
|
79 @type int |
|
80 @param offset position within line of the issue |
|
81 @type int |
|
82 @param code message code |
|
83 @type str |
|
84 @param args arguments for the message |
|
85 @type list |
|
86 """ |
|
87 if self.__ignoreCode(code): |
|
88 return |
|
89 |
|
90 if code in self.counters: |
|
91 self.counters[code] += 1 |
|
92 else: |
|
93 self.counters[code] = 1 |
|
94 |
|
95 # Don't care about expected codes |
|
96 if code in self.__expected: |
|
97 return |
|
98 |
|
99 if code and (self.counters[code] == 1 or self.__repeat): |
|
100 # record the issue with one based line number |
|
101 self.errors.append( |
|
102 { |
|
103 "file": self.__filename, |
|
104 "line": lineNumber + 1, |
|
105 "offset": offset, |
|
106 "code": code, |
|
107 "args": args, |
|
108 } |
|
109 ) |
|
110 |
|
111 def __reportInvalidSyntax(self): |
|
112 """ |
|
113 Private method to report a syntax error. |
|
114 """ |
|
115 exc_type, exc = sys.exc_info()[:2] |
|
116 if len(exc.args) > 1: |
|
117 offset = exc.args[1] |
|
118 if len(offset) > 2: |
|
119 offset = offset[1:3] |
|
120 else: |
|
121 offset = (1, 0) |
|
122 self.__error(offset[0] - 1, offset[1] or 0, |
|
123 'M901', exc_type.__name__, exc.args[0]) |
|
124 |
|
125 def __generateTree(self): |
|
126 """ |
|
127 Private method to generate an AST for our source. |
|
128 |
|
129 @return generated AST |
|
130 @rtype ast.AST |
|
131 """ |
|
132 return ast.parse("".join(self.__source), self.__filename) |
|
133 |
|
134 def run(self): |
|
135 """ |
|
136 Public method to check the given source against functions |
|
137 to be replaced by 'pathlib' equivalents. |
|
138 """ |
|
139 if not self.__filename: |
|
140 # don't do anything, if essential data is missing |
|
141 return |
|
142 |
|
143 if not self.__checkCodes: |
|
144 # don't do anything, if no codes were selected |
|
145 return |
|
146 |
|
147 try: |
|
148 self.__tree = self.__generateTree() |
|
149 except (SyntaxError, TypeError): |
|
150 self.__reportInvalidSyntax() |
|
151 return |
|
152 |
|
153 visitor = SimplifyVisitor(self.__error) |
|
154 visitor.visit(self.__tree) |
|
155 |
|
156 ###################################################################### |
|
157 ## The following code is derived from the flake8-simplify package. |
|
158 ## |
|
159 ## Original License: |
|
160 ## |
|
161 ## MIT License |
|
162 ## |
|
163 ## Copyright (c) 2020 Martin Thoma |
|
164 ## |
|
165 ## Permission is hereby granted, free of charge, to any person obtaining a copy |
|
166 ## of this software and associated documentation files (the "Software"), to |
|
167 ## deal in the Software without restriction, including without limitation the |
|
168 ## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
169 ## sell copies of the Software, and to permit persons to whom the Software is |
|
170 ## furnished to do so, subject to the following conditions: |
|
171 ## |
|
172 ## The above copyright notice and this permission notice shall be included in |
|
173 ## all copies or substantial portions of the Software. |
|
174 ## |
|
175 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
176 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
177 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
178 ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
179 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
180 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
181 ## IN THE SOFTWARE. |
|
182 ###################################################################### |
|
183 |
|
184 |
|
185 class SimplifyVisitor(ast.NodeVisitor): |
|
186 """ |
|
187 Class to traverse the AST node tree and check for code that can be |
|
188 simplified. |
|
189 """ |
|
190 def __init__(self, errorCallback): |
|
191 """ |
|
192 Constructor |
|
193 |
|
194 @param checkCallback callback function taking a reference to the |
|
195 AST node and the resolved name |
|
196 @type func |
|
197 """ |
|
198 super(SimplifyVisitor, self).__init__() |
|
199 |
|
200 self.__error = errorCallback |
|
201 |
|
202 def visit_BoolOp(self, node): |
|
203 """ |
|
204 Public method to process a BoolOp node. |
|
205 |
|
206 @param node reference to the BoolOp node |
|
207 @type ast.BoolOp |
|
208 """ |
|
209 self.__check101(node) |
|
210 |
|
211 ############################################################# |
|
212 ## Methods to check for possible code simplifications below |
|
213 ############################################################# |
|
214 |
|
215 def __getDuplicatedIsinstanceCall(self, node): |
|
216 """ |
|
217 Private method to get a list of isinstance arguments which could |
|
218 be combined. |
|
219 |
|
220 @param node reference to the AST node to be inspected |
|
221 @type ast.BoolOp |
|
222 """ |
|
223 counter = collections.defaultdict(int) |
|
224 |
|
225 for call in node.values: |
|
226 # Ensure this is a call of the built-in isinstance() function. |
|
227 if not isinstance(call, ast.Call) or len(call.args) != 2: |
|
228 continue |
|
229 functionName = call.func.id |
|
230 if functionName != "isinstance": |
|
231 continue |
|
232 |
|
233 arg0Name = unparse(call.args[0]) |
|
234 counter[arg0Name] += 1 |
|
235 |
|
236 return [name for name, count in counter.items() if count > 1] |
|
237 |
|
238 def __check101(self, node): |
|
239 """ |
|
240 Private method to check for duplicate isinstance() calls. |
|
241 |
|
242 @param node reference to the AST node to be checked |
|
243 @type ast.BoolOp |
|
244 """ |
|
245 if not isinstance(node.op, ast.Or): |
|
246 return |
|
247 |
|
248 for variable in self.__getDuplicatedIsinstanceCall(node): |
|
249 self.__error(node.lineno - 1, node.col_offset, "Y101", variable) |