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 """ |
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 |