|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a checker for pydantic related issues. |
|
8 """ |
|
9 |
|
10 import copy |
|
11 |
|
12 |
|
13 class PydanticChecker: |
|
14 """ |
|
15 Class implementing a checker for pydantic related issues. |
|
16 """ |
|
17 |
|
18 Codes = [ |
|
19 "PYD001", |
|
20 "PYD002", |
|
21 "PYD003", |
|
22 "PYD004", |
|
23 "PYD005", |
|
24 "PYD006", |
|
25 "PYD010", |
|
26 ] |
|
27 Prefix = "PYD" |
|
28 |
|
29 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): |
|
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 tree AST tree of the source code |
|
38 @type ast.Module |
|
39 @param select list of selected codes |
|
40 @type list of str |
|
41 @param ignore list of codes to be ignored |
|
42 @type list of str |
|
43 @param expected list of expected codes |
|
44 @type list of str |
|
45 @param repeat flag indicating to report each occurrence of a code |
|
46 @type bool |
|
47 @param args dictionary of arguments for the various checks |
|
48 @type dict |
|
49 """ |
|
50 self.__select = tuple(x for x in select if x.startswith(PydanticChecker.Prefix)) |
|
51 self.__ignore = tuple(x for x in ignore if x.startswith(PydanticChecker.Prefix)) |
|
52 self.__expected = expected[:] |
|
53 self.__repeat = repeat |
|
54 self.__filename = filename |
|
55 self.__source = source[:] |
|
56 self.__tree = copy.deepcopy(tree) |
|
57 self.__args = args |
|
58 |
|
59 # statistics counters |
|
60 self.counters = {} |
|
61 |
|
62 # collection of detected errors |
|
63 self.errors = [] |
|
64 |
|
65 checkersWithCodes = [ |
|
66 ( |
|
67 self.__checkPydantic, |
|
68 ( |
|
69 "PYD001", |
|
70 "PYD002", |
|
71 "PYD003", |
|
72 "PYD004", |
|
73 "PYD005", |
|
74 "PYD006", |
|
75 "PYD010", |
|
76 ), |
|
77 ), |
|
78 ] |
|
79 |
|
80 self.__checkers = [] |
|
81 for checker, codes in checkersWithCodes: |
|
82 if any(not (code and self.__ignoreCode(code)) for code in codes): |
|
83 self.__checkers.append(checker) |
|
84 |
|
85 def __ignoreCode(self, code): |
|
86 """ |
|
87 Private method to check if the message code should be ignored. |
|
88 |
|
89 @param code message code to check for |
|
90 @type str |
|
91 @return flag indicating to ignore the given code |
|
92 @rtype bool |
|
93 """ |
|
94 return code in self.__ignore or ( |
|
95 code.startswith(self.__ignore) and not code.startswith(self.__select) |
|
96 ) |
|
97 |
|
98 def __error(self, lineNumber, offset, code, *args): |
|
99 """ |
|
100 Private method to record an issue. |
|
101 |
|
102 @param lineNumber line number of the issue |
|
103 @type int |
|
104 @param offset position within line of the issue |
|
105 @type int |
|
106 @param code message code |
|
107 @type str |
|
108 @param args arguments for the message |
|
109 @type list |
|
110 """ |
|
111 if self.__ignoreCode(code): |
|
112 return |
|
113 |
|
114 if code in self.counters: |
|
115 self.counters[code] += 1 |
|
116 else: |
|
117 self.counters[code] = 1 |
|
118 |
|
119 # Don't care about expected codes |
|
120 if code in self.__expected: |
|
121 return |
|
122 |
|
123 if code and (self.counters[code] == 1 or self.__repeat): |
|
124 # record the issue with one based line number |
|
125 self.errors.append( |
|
126 { |
|
127 "file": self.__filename, |
|
128 "line": lineNumber + 1, |
|
129 "offset": offset, |
|
130 "code": code, |
|
131 "args": args, |
|
132 } |
|
133 ) |
|
134 |
|
135 def run(self): |
|
136 """ |
|
137 Public method to check the given source against miscellaneous |
|
138 conditions. |
|
139 """ |
|
140 if not self.__filename: |
|
141 # don't do anything, if essential data is missing |
|
142 return |
|
143 |
|
144 if not self.__checkers: |
|
145 # don't do anything, if no codes were selected |
|
146 return |
|
147 |
|
148 for check in self.__checkers: |
|
149 check() |
|
150 |
|
151 def __checkPydantic(self): |
|
152 """ |
|
153 Private method to check pydantic related topics. |
|
154 """ |
|
155 from .PydanticVisitor import PydanticVisitor |
|
156 |
|
157 visitor = PydanticVisitor(errorCallback=self.__error) |
|
158 visitor.visit(self.__tree) |