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

changeset 8243
cc717c2ae956
parent 8228
772103b14c18
child 8273
698ae46f40a4
equal deleted inserted replaced
8242:aa713ac50c0d 8243:cc717c2ae956
13 import itertools 13 import itertools
14 from string import Formatter 14 from string import Formatter
15 from collections import defaultdict 15 from collections import defaultdict
16 import tokenize 16 import tokenize
17 import copy 17 import copy
18 import contextlib
18 19
19 import AstUtilities 20 import AstUtilities
20 21
21 from .eradicate import Eradicator 22 from .eradicate import Eradicator
22 23
609 def __checkBuiltins(self): 610 def __checkBuiltins(self):
610 """ 611 """
611 Private method to check, if built-ins are shadowed. 612 Private method to check, if built-ins are shadowed.
612 """ 613 """
613 functionDefs = [ast.FunctionDef] 614 functionDefs = [ast.FunctionDef]
614 try: 615 with contextlib.suppress(AttributeError):
615 functionDefs.append(ast.AsyncFunctionDef) 616 functionDefs.append(ast.AsyncFunctionDef)
616 except AttributeError:
617 pass
618 617
619 ignoreBuiltinAssignments = self.__args.get( 618 ignoreBuiltinAssignments = self.__args.get(
620 "BuiltinsChecker", 619 "BuiltinsChecker",
621 MiscellaneousCheckerDefaultArgs["BuiltinsChecker"]) 620 MiscellaneousCheckerDefaultArgs["BuiltinsChecker"])
622 621
797 immutableCalls = ( 796 immutableCalls = (
798 "tuple", 797 "tuple",
799 "frozenset", 798 "frozenset",
800 ) 799 )
801 functionDefs = [ast.FunctionDef] 800 functionDefs = [ast.FunctionDef]
802 try: 801 with contextlib.suppress(AttributeError):
803 functionDefs.append(ast.AsyncFunctionDef) 802 functionDefs.append(ast.AsyncFunctionDef)
804 except AttributeError:
805 pass
806 803
807 for node in ast.walk(self.__tree): 804 for node in ast.walk(self.__tree):
808 if any(isinstance(node, functionDef) 805 if any(isinstance(node, functionDef)
809 for functionDef in functionDefs): 806 for functionDef in functionDefs):
810 defaults = node.args.defaults[:] 807 defaults = node.args.defaults[:]
811 try: 808 with contextlib.suppress(AttributeError):
812 defaults += node.args.kw_defaults[:] 809 defaults += node.args.kw_defaults[:]
813 except AttributeError:
814 pass
815 for default in defaults: 810 for default in defaults:
816 if any(isinstance(default, mutableType) 811 if any(isinstance(default, mutableType)
817 for mutableType in mutableTypes): 812 for mutableType in mutableTypes):
818 typeName = type(default).__name__ 813 typeName = type(default).__name__
819 if isinstance(default, ast.Call): 814 if isinstance(default, ast.Call):
1159 @param node reference to the node to be processed 1154 @param node reference to the node to be processed
1160 @type ast.Call 1155 @type ast.Call
1161 @return logging level 1156 @return logging level
1162 @rtype str or None 1157 @rtype str or None
1163 """ 1158 """
1164 try: 1159 with contextlib.suppress(AttributeError):
1165 if node.func.value.id == "warnings": 1160 if node.func.value.id == "warnings":
1166 return None 1161 return None
1167 1162
1168 if node.func.attr in LoggingVisitor.LoggingLevels: 1163 if node.func.attr in LoggingVisitor.LoggingLevels:
1169 return node.func.attr 1164 return node.func.attr
1170 except AttributeError:
1171 pass
1172 1165
1173 return None 1166 return None
1174 1167
1175 def __isFormatCall(self, node): 1168 def __isFormatCall(self, node):
1176 """ 1169 """
1348 self.violations.append((node, code)) 1341 self.violations.append((node, code))
1349 break 1342 break
1350 else: 1343 else:
1351 self.__checkForM502(node) 1344 self.__checkForM502(node)
1352 else: 1345 else:
1353 try: 1346 with contextlib.suppress(AttributeError, IndexError):
1354 # bad super() call 1347 # bad super() call
1355 if isinstance(node.func, ast.Name) and node.func.id == "super": 1348 if isinstance(node.func, ast.Name) and node.func.id == "super":
1356 args = node.args 1349 args = node.args
1357 if ( 1350 if (
1358 len(args) == 2 and 1351 len(args) == 2 and
1379 node.func.id == "setattr" and 1372 node.func.id == "setattr" and
1380 len(node.args) == 3 and 1373 len(node.args) == 3 and
1381 AstUtilities.isString(node.args[1]) 1374 AstUtilities.isString(node.args[1])
1382 ): 1375 ):
1383 self.violations.append((node, "M513")) 1376 self.violations.append((node, "M513"))
1384 except (AttributeError, IndexError):
1385 pass
1386 1377
1387 self.generic_visit(node) 1378 self.generic_visit(node)
1388 1379
1389 def visit_Attribute(self, node): 1380 def visit_Attribute(self, node):
1390 """ 1381 """

eric ide

mercurial