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

branch
eric7
changeset 10884
2be906d60ffb
parent 10754
6faecb62f3a4
child 10997
d470b58626d2
equal deleted inserted replaced
10883:1fe731ca7078 10884:2be906d60ffb
94 "M195", 94 "M195",
95 "M196", 95 "M196",
96 "M197", 96 "M197",
97 "M198", 97 "M198",
98 "M199", 98 "M199",
99 "M200",
99 ## Dictionaries with sorted keys 100 ## Dictionaries with sorted keys
100 "M201", 101 "M251",
101 ## Property 102 ## Property
102 "M210", 103 "M260",
103 "M211", 104 "M261",
104 "M212", 105 "M262",
105 "M213", 106 "M263",
106 "M214", 107 "M264",
107 "M215", 108 "M265",
108 "M216", 109 "M266",
109 "M217", 110 "M267",
110 ## Naive datetime usage 111 ## Naive datetime usage
111 "M301", 112 "M301",
112 "M302", 113 "M302",
113 "M303", 114 "M303",
114 "M304", 115 "M304",
299 "M195", 300 "M195",
300 "M196", 301 "M196",
301 "M197", 302 "M197",
302 "M198", 303 "M198",
303 "M199", 304 "M199",
305 "M200",
304 ), 306 ),
305 ), 307 ),
306 (self.__checkDictWithSortedKeys, ("M201",)), 308 (self.__checkDictWithSortedKeys, ("M251",)),
307 ( 309 (
308 self.__checkProperties, 310 self.__checkProperties,
309 ("M210", "M211", "M212", "M213", "M214", "M215", "M216", "M217"), 311 ("M260", "M261", "M262", "M263", "M264", "M265", "M266", "M267"),
310 ), 312 ),
311 ( 313 (
312 self.__checkDateTime, 314 self.__checkDateTime,
313 ( 315 (
314 "M301", 316 "M301",
885 887
886 def __checkComprehensions(self): 888 def __checkComprehensions(self):
887 """ 889 """
888 Private method to check some comprehension related things. 890 Private method to check some comprehension related things.
889 891
890 This method is adapted from: flake8-comprehensions v3.14.0 892 This method is adapted from: flake8-comprehensions v3.15.0
891 Original: Copyright (c) 2017 Adam Johnson 893 Original: Copyright (c) 2017 Adam Johnson
892 """ 894 """
895 compType = {
896 ast.DictComp: "dict",
897 ast.ListComp: "list",
898 ast.SetComp: "set",
899 }
900
893 visitedMapCalls = set() 901 visitedMapCalls = set()
894 902
895 for node in ast.walk(self.__tree): 903 for node in ast.walk(self.__tree):
896 if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): 904 if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
897 numPositionalArgs = len(node.args) 905 numPositionalArgs = len(node.args)
1134 comprehensionType = f"{node.func.id} comprehension" 1142 comprehensionType = f"{node.func.id} comprehension"
1135 self.__error( 1143 self.__error(
1136 node.lineno - 1, node.col_offset, "M197", comprehensionType 1144 node.lineno - 1, node.col_offset, "M197", comprehensionType
1137 ) 1145 )
1138 1146
1139 elif isinstance(node, (ast.DictComp, ast.ListComp, ast.SetComp)) and ( 1147 elif isinstance(node, (ast.DictComp, ast.ListComp, ast.SetComp)) and (
1140 len(node.generators) == 1 1148 len(node.generators) == 1
1141 and not node.generators[0].ifs 1149 and not node.generators[0].ifs
1142 and not node.generators[0].is_async 1150 and not node.generators[0].is_async
1143 and ( 1151 ):
1144 ( 1152 if (
1145 isinstance(node, (ast.ListComp, ast.SetComp)) 1153 isinstance(node, (ast.ListComp, ast.SetComp))
1146 and isinstance(node.elt, ast.Name) 1154 and isinstance(node.elt, ast.Name)
1147 and isinstance(node.generators[0].target, ast.Name) 1155 and isinstance(node.generators[0].target, ast.Name)
1148 and node.elt.id == node.generators[0].target.id 1156 and node.elt.id == node.generators[0].target.id
1149 ) 1157 ) or (
1150 or ( 1158 isinstance(node, ast.DictComp)
1151 isinstance(node, ast.DictComp) 1159 and isinstance(node.key, ast.Name)
1152 and isinstance(node.key, ast.Name) 1160 and isinstance(node.value, ast.Name)
1153 and isinstance(node.value, ast.Name) 1161 and isinstance(node.generators[0].target, ast.Tuple)
1154 and isinstance(node.generators[0].target, ast.Tuple) 1162 and len(node.generators[0].target.elts) == 2
1155 and len(node.generators[0].target.elts) == 2 1163 and isinstance(node.generators[0].target.elts[0], ast.Name)
1156 and isinstance(node.generators[0].target.elts[0], ast.Name) 1164 and node.generators[0].target.elts[0].id == node.key.id
1157 and node.generators[0].target.elts[0].id == node.key.id 1165 and isinstance(node.generators[0].target.elts[1], ast.Name)
1158 and isinstance(node.generators[0].target.elts[1], ast.Name) 1166 and node.generators[0].target.elts[1].id == node.value.id
1159 and node.generators[0].target.elts[1].id == node.value.id 1167 ):
1160 ) 1168 self.__error(
1169 node.lineno - 1,
1170 node.col_offset,
1171 "M196",
1172 compType[node.__class__],
1161 ) 1173 )
1174
1175 elif (
1176 isinstance(node, ast.DictComp)
1177 and isinstance(node.key, ast.Name)
1178 and isinstance(node.value, ast.Constant)
1179 and isinstance(node.generators[0].target, ast.Name)
1180 and node.key.id == node.generators[0].target.id
1162 ): 1181 ):
1163 compType = { 1182 self.__error(
1164 ast.DictComp: "dict", 1183 node.lineno - 1,
1165 ast.ListComp: "list", 1184 node.col_offset,
1166 ast.SetComp: "set", 1185 "M200",
1167 }[node.__class__] 1186 compType[node.__class__],
1168 1187 )
1169 self.__error(node.lineno - 1, node.col_offset, "M196", compType)
1170 1188
1171 def __checkMutableDefault(self): 1189 def __checkMutableDefault(self):
1172 """ 1190 """
1173 Private method to check for use of mutable types as default arguments. 1191 Private method to check for use of mutable types as default arguments.
1174 """ 1192 """
1254 if not all(AstUtilities.isString(key) for key in node.keys): 1272 if not all(AstUtilities.isString(key) for key in node.keys):
1255 return False 1273 return False
1256 1274
1257 if ( 1275 if (
1258 "__IGNORE_WARNING__" in self.__source[node.lineno - 1] 1276 "__IGNORE_WARNING__" in self.__source[node.lineno - 1]
1259 or "__IGNORE_WARNING_M201__" in self.__source[node.lineno - 1] 1277 or "__IGNORE_WARNING_M251__" in self.__source[node.lineno - 1]
1260 ): 1278 ):
1261 return False 1279 return False
1262 1280
1263 lineNumbers = [key.lineno for key in node.keys] 1281 lineNumbers = [key.lineno for key in node.keys]
1264 return len(lineNumbers) == len(set(lineNumbers)) 1282 return len(lineNumbers) == len(set(lineNumbers))
1272 for key1, key2 in zip(node.keys, node.keys[1:]): 1290 for key1, key2 in zip(node.keys, node.keys[1:]):
1273 if key2.value < key1.value: 1291 if key2.value < key1.value:
1274 self.__error( 1292 self.__error(
1275 key2.lineno - 1, 1293 key2.lineno - 1,
1276 key2.col_offset, 1294 key2.col_offset,
1277 "M201", 1295 "M251",
1278 key2.value, 1296 key2.value,
1279 key1.value, 1297 key1.value,
1280 ) 1298 )
1281 1299
1282 def __checkGettext(self): 1300 def __checkGettext(self):
1362 properties.append(node.name) 1380 properties.append(node.name)
1363 if len(node.args.args) != 1: 1381 if len(node.args.args) != 1:
1364 self.__error( 1382 self.__error(
1365 node.lineno - 1, 1383 node.lineno - 1,
1366 node.col_offset, 1384 node.col_offset,
1367 "M210", 1385 "M260",
1368 len(node.args.args), 1386 len(node.args.args),
1369 ) 1387 )
1370 1388
1371 if isinstance(decorator, ast.Attribute): 1389 if isinstance(decorator, ast.Attribute):
1372 # property setter method 1390 # property setter method
1375 if node.name != decorator.value.id: 1393 if node.name != decorator.value.id:
1376 if node.name in properties: 1394 if node.name in properties:
1377 self.__error( 1395 self.__error(
1378 node.lineno - 1, 1396 node.lineno - 1,
1379 node.col_offset, 1397 node.col_offset,
1380 "M215", 1398 "M265",
1381 node.name, 1399 node.name,
1382 decorator.value.id, 1400 decorator.value.id,
1383 ) 1401 )
1384 else: 1402 else:
1385 self.__error( 1403 self.__error(
1386 node.lineno - 1, 1404 node.lineno - 1,
1387 node.col_offset, 1405 node.col_offset,
1388 "M213", 1406 "M263",
1389 decorator.value.id, 1407 decorator.value.id,
1390 node.name, 1408 node.name,
1391 ) 1409 )
1392 if len(node.args.args) != 2: 1410 if len(node.args.args) != 2:
1393 self.__error( 1411 self.__error(
1394 node.lineno - 1, 1412 node.lineno - 1,
1395 node.col_offset, 1413 node.col_offset,
1396 "M211", 1414 "M261",
1397 len(node.args.args), 1415 len(node.args.args),
1398 ) 1416 )
1399 1417
1400 # property deleter method 1418 # property deleter method
1401 if decorator.attr == "deleter": 1419 if decorator.attr == "deleter":
1403 if node.name != decorator.value.id: 1421 if node.name != decorator.value.id:
1404 if node.name in properties: 1422 if node.name in properties:
1405 self.__error( 1423 self.__error(
1406 node.lineno - 1, 1424 node.lineno - 1,
1407 node.col_offset, 1425 node.col_offset,
1408 "M216", 1426 "M266",
1409 node.name, 1427 node.name,
1410 decorator.value.id, 1428 decorator.value.id,
1411 ) 1429 )
1412 else: 1430 else:
1413 self.__error( 1431 self.__error(
1414 node.lineno - 1, 1432 node.lineno - 1,
1415 node.col_offset, 1433 node.col_offset,
1416 "M214", 1434 "M264",
1417 decorator.value.id, 1435 decorator.value.id,
1418 node.name, 1436 node.name,
1419 ) 1437 )
1420 if len(node.args.args) != 1: 1438 if len(node.args.args) != 1:
1421 self.__error( 1439 self.__error(
1422 node.lineno - 1, 1440 node.lineno - 1,
1423 node.col_offset, 1441 node.col_offset,
1424 "M212", 1442 "M262",
1425 len(node.args.args), 1443 len(node.args.args),
1426 ) 1444 )
1427 1445
1428 if propertyCount > 1: 1446 if propertyCount > 1:
1429 self.__error(node.lineno - 1, node.col_offset, "M217", node.name) 1447 self.__error(node.lineno - 1, node.col_offset, "M267", node.name)
1430 1448
1431 ####################################################################### 1449 #######################################################################
1432 ## The following methods check for implicitly concatenated strings. 1450 ## The following methods check for implicitly concatenated strings.
1433 ## 1451 ##
1434 ## These methods are adapted from: flake8-implicit-str-concat v0.4.0 1452 ## These methods are adapted from: flake8-implicit-str-concat v0.4.0

eric ide

mercurial