Mon, 24 Feb 2025 15:11:18 +0100
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
8186 | 1 | # -*- coding: utf-8 -*- |
2 | ||
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2021 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
8186 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the checker for simplifying Python code. | |
8 | """ | |
9 | ||
10 | import ast | |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8195
diff
changeset
|
11 | import copy |
8186 | 12 | |
8189
17df5c8df8c1
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8186
diff
changeset
|
13 | from .SimplifyNodeVisitor import SimplifyNodeVisitor |
8186 | 14 | |
15 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8202
diff
changeset
|
16 | class SimplifyChecker: |
8186 | 17 | """ |
18 | Class implementing a checker for to help simplifying Python code. | |
19 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
20 | |
8186 | 21 | Codes = [ |
8189
17df5c8df8c1
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8186
diff
changeset
|
22 | # Python-specifics |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
23 | "Y-101", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
24 | "Y-102", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
25 | "Y-103", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
26 | "Y-104", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
27 | "Y-105", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
28 | "Y-106", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
29 | "Y-107", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
30 | "Y-108", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
31 | "Y-109", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
32 | "Y-110", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
33 | "Y-111", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
34 | "Y-112", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
35 | "Y-113", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
36 | "Y-114", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
37 | "Y-115", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
38 | "Y-116", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
39 | "Y-117", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
40 | "Y-118", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
41 | "Y-119", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
42 | "Y-120", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
43 | "Y-121", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
44 | "Y-122", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
45 | "Y-123", |
8209
14470a65a52e
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
46 | # Python-specifics not part of flake8-simplify |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
47 | "Y-181", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
48 | "Y-182", |
8189
17df5c8df8c1
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8186
diff
changeset
|
49 | # Comparations |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
50 | "Y-201", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
51 | "Y-202", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
52 | "Y-203", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
53 | "Y-204", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
54 | "Y-205", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
55 | "Y-206", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
56 | "Y-207", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
57 | "Y-208", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
58 | "Y-211", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
59 | "Y-212", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
60 | "Y-213", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
61 | "Y-221", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
62 | "Y-222", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
63 | "Y-223", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
64 | "Y-224", |
8195
db7f2badd374
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8194
diff
changeset
|
65 | # Opinionated |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
66 | "Y-301", |
8211
8322a6f219ff
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8210
diff
changeset
|
67 | # General Code Style |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
68 | "Y-401", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
69 | "Y-402", |
11141
2f5f73c51c7c
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
70 | # f-Strings |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
71 | "Y-411", |
9277
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
72 | # Additional Checks |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
73 | "Y-901", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
74 | "Y-904", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
75 | "Y-905", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
76 | "Y-906", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
77 | "Y-907", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
78 | "Y-909", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
79 | "Y-910", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
80 | "Y-911", |
8186 | 81 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
82 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
83 | def __init__(self, source, filename, tree, selected, ignored, expected, repeat): |
8186 | 84 | """ |
85 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
86 | |
8186 | 87 | @param source source code to be checked |
88 | @type list of str | |
89 | @param filename name of the source file | |
90 | @type str | |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8195
diff
changeset
|
91 | @param tree AST tree of the source code |
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8195
diff
changeset
|
92 | @type ast.Module |
8186 | 93 | @param selected list of selected codes |
94 | @type list of str | |
95 | @param ignored list of codes to be ignored | |
96 | @type list of str | |
97 | @param expected list of expected codes | |
98 | @type list of str | |
99 | @param repeat flag indicating to report each occurrence of a code | |
100 | @type bool | |
101 | """ | |
102 | self.__select = tuple(selected) | |
11142
2f0fb22c1d63
Fixed a few issues in the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11141
diff
changeset
|
103 | self.__ignore = tuple(ignored) |
8186 | 104 | self.__expected = expected[:] |
105 | self.__repeat = repeat | |
106 | self.__filename = filename | |
107 | self.__source = source[:] | |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8195
diff
changeset
|
108 | self.__tree = copy.deepcopy(tree) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
109 | |
8186 | 110 | # statistics counters |
111 | self.counters = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | |
8186 | 113 | # collection of detected errors |
114 | self.errors = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
116 | self.__checkCodes = (code for code in self.Codes if not self.__ignoreCode(code)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
117 | |
8186 | 118 | def __ignoreCode(self, code): |
119 | """ | |
120 | Private method to check if the message code should be ignored. | |
121 | ||
122 | @param code message code to check for | |
123 | @type str | |
124 | @return flag indicating to ignore the given code | |
125 | @rtype bool | |
126 | """ | |
11145
d328a7b74fd8
Corrected some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11142
diff
changeset
|
127 | return code in self.__ignore or ( |
d328a7b74fd8
Corrected some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11142
diff
changeset
|
128 | code.startswith(self.__ignore) and not code.startswith(self.__select) |
11142
2f0fb22c1d63
Fixed a few issues in the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11141
diff
changeset
|
129 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
130 | |
8186 | 131 | def __error(self, lineNumber, offset, code, *args): |
132 | """ | |
133 | Private method to record an issue. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
134 | |
8186 | 135 | @param lineNumber line number of the issue |
136 | @type int | |
137 | @param offset position within line of the issue | |
138 | @type int | |
139 | @param code message code | |
140 | @type str | |
141 | @param args arguments for the message | |
142 | @type list | |
143 | """ | |
144 | if self.__ignoreCode(code): | |
145 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
146 | |
8225
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
147 | # record the issue with one based line number |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
148 | errorInfo = { |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
149 | "file": self.__filename, |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
150 | "line": lineNumber + 1, |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
151 | "offset": offset, |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
152 | "code": code, |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
153 | "args": args, |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
154 | } |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
155 | |
8225
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
156 | if errorInfo not in self.errors: |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
157 | # this issue was not seen before |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
158 | if code in self.counters: |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
159 | self.counters[code] += 1 |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
160 | else: |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
161 | self.counters[code] = 1 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
162 | |
8225
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
163 | # Don't care about expected codes |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
164 | if code in self.__expected: |
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
165 | return |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
166 | |
8225
811b7fa1b8a9
SimplifyChecker: added code to prevent duplicate error counting.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8224
diff
changeset
|
167 | if code and (self.counters[code] == 1 or self.__repeat): |
8224
e0ecde23e7b2
SimplifyChecker: added code to prevent duplicate error reports.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8216
diff
changeset
|
168 | self.errors.append(errorInfo) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | |
8186 | 170 | def run(self): |
171 | """ | |
172 | Public method to check the given source against functions | |
173 | to be replaced by 'pathlib' equivalents. | |
174 | """ | |
175 | if not self.__filename: | |
176 | # don't do anything, if essential data is missing | |
177 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
8186 | 179 | if not self.__checkCodes: |
180 | # don't do anything, if no codes were selected | |
181 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
182 | |
8191
9125da0c227e
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8189
diff
changeset
|
183 | # Add parent information |
9277
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
184 | self.__addMeta(self.__tree) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
185 | |
8189
17df5c8df8c1
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8186
diff
changeset
|
186 | visitor = SimplifyNodeVisitor(self.__error) |
8186 | 187 | visitor.visit(self.__tree) |
9278
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9277
diff
changeset
|
188 | |
9277
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
189 | def __addMeta(self, root, level=0): |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
190 | """ |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
191 | Private method to amend the nodes of the given AST tree with backward and |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
192 | forward references. |
9278
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9277
diff
changeset
|
193 | |
9277
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
194 | @param root reference to the root node of the tree |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
195 | @type ast.AST |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
196 | @param level nesting level (defaults to 0) |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
197 | @type int (optional) |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
198 | """ |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
199 | previousSibling = None |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
200 | for node in ast.iter_child_nodes(root): |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
201 | if level == 0: |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
202 | node.parent = root |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
203 | node.previous_sibling = previousSibling |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
204 | node.next_sibling = None |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
205 | if previousSibling: |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
206 | node.previous_sibling.next_sibling = node |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
207 | previousSibling = node |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
208 | for child in ast.iter_child_nodes(node): |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
209 | child.parent = node |
471c5a263d53
Code Style Checker
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
210 | self.__addMeta(node, level=level + 1) |