src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py

branch
eric7
changeset 10085
b5808c3a9967
parent 10048
df836ff707fd
child 10119
64147a7e6393
equal deleted inserted replaced
10084:125166c6b66c 10085:b5808c3a9967
74 "M196", 74 "M196",
75 "M197", 75 "M197",
76 "M198", 76 "M198",
77 ## Dictionaries with sorted keys 77 ## Dictionaries with sorted keys
78 "M201", 78 "M201",
79 ## Property
80 "M210",
81 "M211",
82 "M212",
83 "M213",
84 "M214",
85 "M215",
86 "M216",
87 "M217",
79 ## Naive datetime usage 88 ## Naive datetime usage
80 "M301", 89 "M301",
81 "M302", 90 "M302",
82 "M303", 91 "M303",
83 "M304", 92 "M304",
158 ## Future statements 167 ## Future statements
159 "M701", 168 "M701",
160 "M702", 169 "M702",
161 ## Gettext 170 ## Gettext
162 "M711", 171 "M711",
163 ## print 172 ## print() statements
164 "M801", 173 "M801",
165 ## one element tuple 174 ## one element tuple
166 "M811", 175 "M811",
167 ## Mutable Defaults 176 ## Mutable Defaults
168 "M821", 177 "M821",
255 "M197", 264 "M197",
256 "M198", 265 "M198",
257 ), 266 ),
258 ), 267 ),
259 (self.__checkDictWithSortedKeys, ("M201",)), 268 (self.__checkDictWithSortedKeys, ("M201",)),
269 (
270 self.__checkProperties,
271 ("M210", "M211", "M212", "M213", "M214", "M215", "M216", "M217"),
272 ),
260 ( 273 (
261 self.__checkDateTime, 274 self.__checkDateTime,
262 ( 275 (
263 "M301", 276 "M301",
264 "M302", 277 "M302",
1200 for violation in visitor.violations: 1213 for violation in visitor.violations:
1201 node = violation[0] 1214 node = violation[0]
1202 reason = violation[1] 1215 reason = violation[1]
1203 self.__error(node.lineno - 1, node.col_offset, reason) 1216 self.__error(node.lineno - 1, node.col_offset, reason)
1204 1217
1218 def __checkProperties(self):
1219 """
1220 Private method to check for issue with property related methods.
1221 """
1222 properties = []
1223 for node in ast.walk(self.__tree):
1224 if isinstance(node, ast.ClassDef):
1225 properties.clear()
1226
1227 elif isinstance(node, ast.FunctionDef):
1228 propertyCount = 0
1229 for decorator in node.decorator_list:
1230 # property getter method
1231 if isinstance(decorator, ast.Name) and decorator.id == "property":
1232 propertyCount += 1
1233 properties.append(node.name)
1234 if len(node.args.args) != 1:
1235 self.__error(
1236 node.lineno - 1,
1237 node.col_offset,
1238 "M210",
1239 len(node.args.args),
1240 )
1241
1242 if isinstance(decorator, ast.Attribute):
1243 # property setter method
1244 if decorator.attr == "setter":
1245 propertyCount += 1
1246 if node.name != decorator.value.id:
1247 if node.name in properties:
1248 self.__error(
1249 node.lineno - 1,
1250 node.col_offset,
1251 "M215",
1252 node.name,
1253 decorator.value.id,
1254 )
1255 else:
1256 self.__error(
1257 node.lineno - 1,
1258 node.col_offset,
1259 "M213",
1260 decorator.value.id,
1261 node.name,
1262 )
1263 if len(node.args.args) != 2:
1264 self.__error(
1265 node.lineno - 1,
1266 node.col_offset,
1267 "M211",
1268 len(node.args.args),
1269 )
1270
1271 # property deleter method
1272 if decorator.attr == "deleter":
1273 propertyCount += 1
1274 if node.name != decorator.value.id:
1275 if node.name in properties:
1276 self.__error(
1277 node.lineno - 1,
1278 node.col_offset,
1279 "M216",
1280 node.name,
1281 decorator.value.id,
1282 )
1283 else:
1284 self.__error(
1285 node.lineno - 1,
1286 node.col_offset,
1287 "M214",
1288 decorator.value.id,
1289 node.name,
1290 )
1291 if len(node.args.args) != 1:
1292 self.__error(
1293 node.lineno - 1,
1294 node.col_offset,
1295 "M212",
1296 len(node.args.args),
1297 )
1298
1299 if propertyCount > 1:
1300 self.__error(node.lineno - 1, node.col_offset, "M217", node.name)
1301
1205 1302
1206 class TextVisitor(ast.NodeVisitor): 1303 class TextVisitor(ast.NodeVisitor):
1207 """ 1304 """
1208 Class implementing a node visitor for bytes and str instances. 1305 Class implementing a node visitor for bytes and str instances.
1209 1306

eric ide

mercurial