994 """ |
994 """ |
995 if sys.version_info >= (3, 8, 0): |
995 if sys.version_info >= (3, 8, 0): |
996 if AstUtilities.isBaseString(node): |
996 if AstUtilities.isBaseString(node): |
997 self.__addNode(node) |
997 self.__addNode(node) |
998 else: |
998 else: |
999 super(TextVisitor, self).generic_visit(node) |
999 super().generic_visit(node) |
1000 else: |
1000 else: |
1001 super(TextVisitor, self).generic_visit(node) |
1001 super().generic_visit(node) |
1002 |
1002 |
1003 def __visitDefinition(self, node): |
1003 def __visitDefinition(self, node): |
1004 """ |
1004 """ |
1005 Private method handling class and function definitions. |
1005 Private method handling class and function definitions. |
1006 |
1006 |
1092 node.func.value.id == 'str' and |
1092 node.func.value.id == 'str' and |
1093 node.args and |
1093 node.args and |
1094 AstUtilities.isBaseString(node.args[0]) |
1094 AstUtilities.isBaseString(node.args[0]) |
1095 ): |
1095 ): |
1096 self.calls[node.args[0]] = (node, True) |
1096 self.calls[node.args[0]] = (node, True) |
1097 super(TextVisitor, self).generic_visit(node) |
1097 super().generic_visit(node) |
1098 |
1098 |
1099 |
1099 |
1100 class LoggingVisitor(ast.NodeVisitor): |
1100 class LoggingVisitor(ast.NodeVisitor): |
1101 """ |
1101 """ |
1102 Class implementing a node visitor to check logging statements. |
1102 Class implementing a node visitor to check logging statements. |
1200 """ |
1200 """ |
1201 # we are in a logging statement |
1201 # we are in a logging statement |
1202 if self.__withinLoggingStatement(): |
1202 if self.__withinLoggingStatement(): |
1203 if self.__withinLoggingArgument() and self.__isFormatCall(node): |
1203 if self.__withinLoggingArgument() and self.__isFormatCall(node): |
1204 self.violations.append((node, "M651")) |
1204 self.violations.append((node, "M651")) |
1205 super(LoggingVisitor, self).generic_visit(node) |
1205 super().generic_visit(node) |
1206 return |
1206 return |
1207 |
1207 |
1208 loggingLevel = self.__detectLoggingLevel(node) |
1208 loggingLevel = self.__detectLoggingLevel(node) |
1209 |
1209 |
1210 if loggingLevel and self.__currentLoggingLevel is None: |
1210 if loggingLevel and self.__currentLoggingLevel is None: |
1211 self.__currentLoggingLevel = loggingLevel |
1211 self.__currentLoggingLevel = loggingLevel |
1212 |
1212 |
1213 # we are in some other statement |
1213 # we are in some other statement |
1214 if loggingLevel is None: |
1214 if loggingLevel is None: |
1215 super(LoggingVisitor, self).generic_visit(node) |
1215 super().generic_visit(node) |
1216 return |
1216 return |
1217 |
1217 |
1218 # we are entering a new logging statement |
1218 # we are entering a new logging statement |
1219 self.__currentLoggingCall = node |
1219 self.__currentLoggingCall = node |
1220 |
1220 |
1229 isinstance(child, ast.keyword) and |
1229 isinstance(child, ast.keyword) and |
1230 child.arg == "extra" |
1230 child.arg == "extra" |
1231 ): |
1231 ): |
1232 self.__currentExtraKeyword = child |
1232 self.__currentExtraKeyword = child |
1233 |
1233 |
1234 super(LoggingVisitor, self).visit(child) |
1234 super().visit(child) |
1235 |
1235 |
1236 self.__currentLoggingArgument = None |
1236 self.__currentLoggingArgument = None |
1237 self.__currentExtraKeyword = None |
1237 self.__currentExtraKeyword = None |
1238 |
1238 |
1239 self.__currentLoggingCall = None |
1239 self.__currentLoggingCall = None |
1254 |
1254 |
1255 # handle string concat |
1255 # handle string concat |
1256 if isinstance(node.op, ast.Add): |
1256 if isinstance(node.op, ast.Add): |
1257 self.violations.append((node, "M653")) |
1257 self.violations.append((node, "M653")) |
1258 |
1258 |
1259 super(LoggingVisitor, self).generic_visit(node) |
1259 super().generic_visit(node) |
1260 |
1260 |
1261 def visit_JoinedStr(self, node): |
1261 def visit_JoinedStr(self, node): |
1262 """ |
1262 """ |
1263 Public method to handle f-string arguments. |
1263 Public method to handle f-string arguments. |
1264 |
1264 |
1268 if self.__withinLoggingStatement(): |
1268 if self.__withinLoggingStatement(): |
1269 if any(isinstance(i, ast.FormattedValue) for i in node.values): |
1269 if any(isinstance(i, ast.FormattedValue) for i in node.values): |
1270 if self.__withinLoggingArgument(): |
1270 if self.__withinLoggingArgument(): |
1271 self.violations.append((node, "M654")) |
1271 self.violations.append((node, "M654")) |
1272 |
1272 |
1273 super(LoggingVisitor, self).generic_visit(node) |
1273 super().generic_visit(node) |
1274 |
1274 |
1275 |
1275 |
1276 class BugBearVisitor(ast.NodeVisitor): |
1276 class BugBearVisitor(ast.NodeVisitor): |
1277 """ |
1277 """ |
1278 Class implementing a node visitor to check for various topics. |
1278 Class implementing a node visitor to check for various topics. |
1303 """ |
1303 """ |
1304 self.__nodeStack.append(node) |
1304 self.__nodeStack.append(node) |
1305 self.__nodeWindow.append(node) |
1305 self.__nodeWindow.append(node) |
1306 self.__nodeWindow = self.__nodeWindow[-BugBearVisitor.NodeWindowSize:] |
1306 self.__nodeWindow = self.__nodeWindow[-BugBearVisitor.NodeWindowSize:] |
1307 |
1307 |
1308 super(BugBearVisitor, self).visit(node) |
1308 super().visit(node) |
1309 |
1309 |
1310 self.__nodeStack.pop() |
1310 self.__nodeStack.pop() |
1311 |
1311 |
1312 def visit_UAdd(self, node): |
1312 def visit_UAdd(self, node): |
1313 """ |
1313 """ |