Wed, 20 Mar 2019 20:03:22 +0100
MiscellaneousChecker: fixed some Python2 related issues.
Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py | file | annotate | diff | comparison | revisions |
--- a/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py Wed Mar 20 19:41:04 2019 +0100 +++ b/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py Wed Mar 20 20:03:22 2019 +0100 @@ -1434,13 +1434,13 @@ for elt in node.elts: self.__visitAssignTarget(elt) return - + if isinstance(node, ast.Name): self.assigns[node.id].append(node.lineno) return - + self.generic_visit(node) - + def __checkFunction(self, node): """ Private method to check a function definition node. @@ -1450,22 +1450,22 @@ """ if not self.returns or not node.body: return - + if len(node.body) == 1 and isinstance(node.body[-1], ast.Return): # skip functions that consist of `return None` only return - + if not self.__resultExists(): self.__checkUnnecessaryReturnNone() return - + self.__checkImplicitReturnValue() self.__checkImplicitReturn(node.body[-1]) - + for n in self.returns: if n.value: self.__checkUnnecessaryAssign(n.value) - + def __isNone(self, node): """ Private method to check, if a node value is None. @@ -1474,7 +1474,11 @@ @type ast.AST @return flag indicating the node contains a None value """ - return isinstance(node, ast.NameConstant) and node.value is None + try: + return isinstance(node, ast.NameConstant) and node.value is None + except AttributeError: + # try Py2 + return isinstance(node, ast.Name) and node.id == "None" def __resultExists(self): """ @@ -1489,7 +1493,7 @@ return True return False - + def __checkImplicitReturnValue(self): """ Private method to check for implicit return values. @@ -1497,7 +1501,7 @@ for node in self.returns: if not node.value: self.violations.append((node, "M832")) - + def __checkUnnecessaryReturnNone(self): """ Private method to check for an unnecessary 'return None' statement. @@ -1505,7 +1509,7 @@ for node in self.returns: if self.__isNone(node.value): self.violations.append((node, "M831")) - + def __checkImplicitReturn(self, node): """ Private method to check for an implicit return statement. @@ -1517,23 +1521,27 @@ if not node.body or not node.orelse: self.violations.append((node, "M833")) return - + self.__checkImplicitReturn(node.body[-1]) self.__checkImplicitReturn(node.orelse[-1]) return - + if isinstance(node, ast.For) and node.orelse: self.__checkImplicitReturn(node.orelse[-1]) return - + if isinstance(node, ast.With): self.__checkImplicitReturn(node.body[-1]) return - - if not isinstance(node, - (ast.Return, ast.Raise, ast.While, ast.Try)): + + try: + okNodes = (ast.Return, ast.Raise, ast.While, ast.Try) + except AttributeError: + # Py2 + okNodes = (ast.Return, ast.Raise, ast.While) + if not isinstance(node, okNodes): self.violations.append((node, "M833")) - + def __checkUnnecessaryAssign(self, node): """ Private method to check for an unnecessary assign statement. @@ -1543,20 +1551,20 @@ """ if not isinstance(node, ast.Name): return - + varname = node.id returnLineno = node.lineno - + if varname not in self.assigns: return - + if varname not in self.refs: self.violations.append((node, "M834")) return - + if self.__hasRefsBeforeNextAssign(varname, returnLineno): return - + self.violations.append((node, "M834")) def __hasRefsBeforeNextAssign(self, varname, returnLineno): @@ -1573,26 +1581,26 @@ """ beforeAssign = 0 afterAssign = None - + for lineno in sorted(self.assigns[varname]): if lineno > returnLineno: afterAssign = lineno break - + if lineno <= returnLineno: beforeAssign = lineno - + for lineno in self.refs[varname]: if lineno == returnLineno: continue - + if afterAssign: if beforeAssign < lineno <= afterAssign: return True - + elif beforeAssign < lineno: return True - + return False # # eflag: noqa = M702