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 |