eric6/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py

changeset 8218
7c09585bd960
parent 8207
d359172d11be
child 8222
5994b80b8760
equal deleted inserted replaced
8217:385f60c94548 8218:7c09585bd960
950 950
951 def __init__(self): 951 def __init__(self):
952 """ 952 """
953 Constructor 953 Constructor
954 """ 954 """
955 super(TextVisitor, self).__init__() 955 super().__init__()
956 self.nodes = [] 956 self.nodes = []
957 self.calls = {} 957 self.calls = {}
958 958
959 def __addNode(self, node): 959 def __addNode(self, node):
960 """ 960 """
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.
1112 1112
1113 def __init__(self): 1113 def __init__(self):
1114 """ 1114 """
1115 Constructor 1115 Constructor
1116 """ 1116 """
1117 super(LoggingVisitor, self).__init__() 1117 super().__init__()
1118 1118
1119 self.__currentLoggingCall = None 1119 self.__currentLoggingCall = None
1120 self.__currentLoggingArgument = None 1120 self.__currentLoggingArgument = None
1121 self.__currentLoggingLevel = None 1121 self.__currentLoggingLevel = None
1122 self.__currentExtraKeyword = None 1122 self.__currentExtraKeyword = None
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.
1286 1286
1287 def __init__(self): 1287 def __init__(self):
1288 """ 1288 """
1289 Constructor 1289 Constructor
1290 """ 1290 """
1291 super(BugBearVisitor, self).__init__() 1291 super().__init__()
1292 1292
1293 self.__nodeStack = [] 1293 self.__nodeStack = []
1294 self.__nodeWindow = [] 1294 self.__nodeWindow = []
1295 self.violations = [] 1295 self.violations = []
1296 1296
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 """
1528 """ 1528 """
1529 def __init__(self): 1529 def __init__(self):
1530 """ 1530 """
1531 Constructor 1531 Constructor
1532 """ 1532 """
1533 super(NameFinder, self).__init__() 1533 super().__init__()
1534 1534
1535 self.__names = {} 1535 self.__names = {}
1536 1536
1537 def visit_Name(self, node): 1537 def visit_Name(self, node):
1538 """ 1538 """
1550 @param node AST node to be traversed 1550 @param node AST node to be traversed
1551 @type ast.Node 1551 @type ast.Node
1552 """ 1552 """
1553 if isinstance(node, list): 1553 if isinstance(node, list):
1554 for elem in node: 1554 for elem in node:
1555 super(NameFinder, self).visit(elem) 1555 super().visit(elem)
1556 else: 1556 else:
1557 super(NameFinder, self).visit(node) 1557 super().visit(node)
1558 1558
1559 def getNames(self): 1559 def getNames(self):
1560 """ 1560 """
1561 Public method to return the extracted names and Name nodes. 1561 Public method to return the extracted names and Name nodes.
1562 1562
1576 1576
1577 def __init__(self): 1577 def __init__(self):
1578 """ 1578 """
1579 Constructor 1579 Constructor
1580 """ 1580 """
1581 super(ReturnVisitor, self).__init__() 1581 super().__init__()
1582 1582
1583 self.__stack = [] 1583 self.__stack = []
1584 self.violations = [] 1584 self.violations = []
1585 self.__loopCount = 0 1585 self.__loopCount = 0
1586 1586
1937 """ 1937 """
1938 def __init__(self): 1938 def __init__(self):
1939 """ 1939 """
1940 Constructor 1940 Constructor
1941 """ 1941 """
1942 super(DateTimeVisitor, self).__init__() 1942 super().__init__()
1943 1943
1944 self.violations = [] 1944 self.violations = []
1945 1945
1946 def __getFromKeywords(self, keywords, name): 1946 def __getFromKeywords(self, keywords, name):
1947 """ 1947 """
2157 """ 2157 """
2158 def __init__(self): 2158 def __init__(self):
2159 """ 2159 """
2160 Constructor 2160 Constructor
2161 """ 2161 """
2162 super(SysVersionVisitor, self).__init__() 2162 super().__init__()
2163 2163
2164 self.violations = [] 2164 self.violations = []
2165 self.__fromImports = {} 2165 self.__fromImports = {}
2166 2166
2167 def visit_ImportFrom(self, node): 2167 def visit_ImportFrom(self, node):

eric ide

mercurial