64 @type func |
64 @type func |
65 """ |
65 """ |
66 super(SimplifyNodeVisitor, self).__init__() |
66 super(SimplifyNodeVisitor, self).__init__() |
67 |
67 |
68 self.__error = errorCallback |
68 self.__error = errorCallback |
|
69 |
|
70 self.__classDefinitionStack = [] |
69 |
71 |
70 def visit_Expr(self, node): |
72 def visit_Expr(self, node): |
71 """ |
73 """ |
72 Public method to process an Expr node. |
74 Public method to process an Expr node. |
73 |
75 |
199 Public method to process a ClassDef node. |
202 Public method to process a ClassDef node. |
200 |
203 |
201 @param node reference to the ClassDef node |
204 @param node reference to the ClassDef node |
202 @type ast.ClassDef |
205 @type ast.ClassDef |
203 """ |
206 """ |
|
207 # register the name of the class being defined |
|
208 self.__classDefinitionStack.append(node.name) |
|
209 |
204 self.__check119(node) |
210 self.__check119(node) |
205 self.__check120(node) |
211 self.__check120(node) |
206 |
212 |
207 self.generic_visit(node) |
213 self.generic_visit(node) |
|
214 |
|
215 self.__classDefinitionStack.pop() |
208 |
216 |
209 def visit_UnaryOp(self, node): |
217 def visit_UnaryOp(self, node): |
210 """ |
218 """ |
211 Public method to process a UnaryOp node. |
219 Public method to process a UnaryOp node. |
212 |
220 |
1065 isinstance(node.bases[0], ast.Name) and |
1073 isinstance(node.bases[0], ast.Name) and |
1066 node.bases[0].id == "object" |
1074 node.bases[0].id == "object" |
1067 ): |
1075 ): |
1068 self.__error(node.lineno - 1, node.col_offset, "Y120", |
1076 self.__error(node.lineno - 1, node.col_offset, "Y120", |
1069 node.name) |
1077 node.name) |
1070 |
1078 # TODO: extend to cover 'class FooBar(Foo, object):' |
1071 def __check121(self, node): |
1079 # => class FooBar(Foo): |
|
1080 |
|
1081 def __check181(self, node): |
1072 """ |
1082 """ |
1073 Private method to check for assignments that could be converted into |
1083 Private method to check for assignments that could be converted into |
1074 an augmented assignment. |
1084 an augmented assignment. |
1075 |
1085 |
1076 @param node reference to the AST node to be checked |
1086 @param node reference to the AST node to be checked |
1085 node.value.left.id == node.targets[0].id and |
1095 node.value.left.id == node.targets[0].id and |
1086 not isinstance(node.value.right, ast.Tuple) |
1096 not isinstance(node.value.right, ast.Tuple) |
1087 ): |
1097 ): |
1088 newNode = ast.AugAssign(node.targets[0], node.value.op, |
1098 newNode = ast.AugAssign(node.targets[0], node.value.op, |
1089 node.value.right) |
1099 node.value.right) |
1090 self.__error(node.lineno - 1, node.col_offset, "Y121", |
1100 self.__error(node.lineno - 1, node.col_offset, "Y181", |
1091 unparse(newNode), unparse(node)) |
1101 unparse(newNode), unparse(node)) |
|
1102 |
|
1103 def __check182(self, node): |
|
1104 """ |
|
1105 Private method to check for calls of type 'super(Foo, self)' that could |
|
1106 be shortened to 'super()'. |
|
1107 |
|
1108 @param node reference to the AST node to be checked |
|
1109 @type ast.Call |
|
1110 """ |
|
1111 # super(Foo, self) |
|
1112 if ( |
|
1113 self.__classDefinitionStack and |
|
1114 isinstance(node.func, ast.Name) and |
|
1115 node.func.id == "super" and |
|
1116 len(node.args) == 2 and |
|
1117 all(isinstance(arg, ast.Name) for arg in node.args) and |
|
1118 node.args[0].id == self.__classDefinitionStack[-1] and |
|
1119 node.args[1].id == "self" |
|
1120 ): |
|
1121 self.__error(node.lineno - 1, node.col_offset, "Y182", |
|
1122 unparse(node)) |
1092 |
1123 |
1093 def __check201(self, node): |
1124 def __check201(self, node): |
1094 """ |
1125 """ |
1095 Private method to check for calls where an unary 'not' is used for |
1126 Private method to check for calls where an unary 'not' is used for |
1096 an unequality. |
1127 an unequality. |