src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Pydantic/PydanticChecker.py

branch
eric7
changeset 11150
73d80859079c
parent 11143
ef75c265ab47
equal deleted inserted replaced
11149:fc45672fae42 11150:73d80859079c
5 5
6 """ 6 """
7 Module implementing a checker for pydantic related issues. 7 Module implementing a checker for pydantic related issues.
8 """ 8 """
9 9
10 import copy 10 from CodeStyleTopicChecker import CodeStyleTopicChecker
11 11
12 12
13 class PydanticChecker: 13 class PydanticChecker(CodeStyleTopicChecker):
14 """ 14 """
15 Class implementing a checker for pydantic related issues. 15 Class implementing a checker for pydantic related issues.
16 """ 16 """
17 17
18 Codes = [ 18 Codes = [
19 "PYD001", 19 "PYD-001",
20 "PYD002", 20 "PYD-002",
21 "PYD003", 21 "PYD-003",
22 "PYD004", 22 "PYD-004",
23 "PYD005", 23 "PYD-005",
24 "PYD006", 24 "PYD-006",
25 "PYD010", 25 "PYD-010",
26 ] 26 ]
27 Prefix = "PYD" 27 Category = "PYD"
28 28
29 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): 29 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args):
30 """ 30 """
31 Constructor 31 Constructor
32 32
45 @param repeat flag indicating to report each occurrence of a code 45 @param repeat flag indicating to report each occurrence of a code
46 @type bool 46 @type bool
47 @param args dictionary of arguments for the various checks 47 @param args dictionary of arguments for the various checks
48 @type dict 48 @type dict
49 """ 49 """
50 self.__select = tuple(x for x in select if x.startswith(PydanticChecker.Prefix)) 50 super().__init__(
51 self.__ignore = tuple(x for x in ignore if x.startswith(PydanticChecker.Prefix)) 51 PydanticChecker.Category,
52 self.__expected = expected[:] 52 source,
53 self.__repeat = repeat 53 filename,
54 self.__filename = filename 54 tree,
55 self.__source = source[:] 55 select,
56 self.__tree = copy.deepcopy(tree) 56 ignore,
57 self.__args = args 57 expected,
58 58 repeat,
59 # statistics counters 59 args,
60 self.counters = {} 60 )
61
62 # collection of detected errors
63 self.errors = []
64 61
65 checkersWithCodes = [ 62 checkersWithCodes = [
66 ( 63 (
67 self.__checkPydantic, 64 self.__checkPydantic,
68 ( 65 (
69 "PYD001", 66 "PYD-001",
70 "PYD002", 67 "PYD-002",
71 "PYD003", 68 "PYD-003",
72 "PYD004", 69 "PYD-004",
73 "PYD005", 70 "PYD-005",
74 "PYD006", 71 "PYD-006",
75 "PYD010", 72 "PYD-010",
76 ), 73 ),
77 ), 74 ),
78 ] 75 ]
79 76 self._initializeCheckers(checkersWithCodes)
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 77
151 def __checkPydantic(self): 78 def __checkPydantic(self):
152 """ 79 """
153 Private method to check pydantic related topics. 80 Private method to check pydantic related topics.
154 """ 81 """
155 from .PydanticVisitor import PydanticVisitor 82 from .PydanticVisitor import PydanticVisitor
156 83
157 visitor = PydanticVisitor(errorCallback=self.__error) 84 visitor = PydanticVisitor(errorCallback=self.addErrorFromNode)
158 visitor.visit(self.__tree) 85 visitor.visit(self.tree)

eric ide

mercurial