Sat, 10 Apr 2021 12:34:29 +0200
Code Style Checker
- added a check to help simplifying 'super(Foo, self)' to just 'super()'
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py Fri Apr 09 21:14:51 2021 +0200 +++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py Sat Apr 10 12:34:29 2021 +0200 @@ -21,7 +21,10 @@ # Python-specifics "Y101", "Y102", "Y103", "Y104", "Y105", "Y106", "Y107", "Y108", "Y109", "Y110", "Y111", "Y112", "Y113", "Y114", "Y115", "Y116", - "Y117", "Y118", "Y119", "Y120", "Y121", + "Y117", "Y118", "Y119", "Y120", + + # Python-specifics not part of flake8-simplify + "Y181", "Y182", # Comparations "Y201", "Y202", "Y203", "Y204", "Y205", "Y206", "Y207", "Y208",
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py Fri Apr 09 21:14:51 2021 +0200 +++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py Sat Apr 10 12:34:29 2021 +0200 @@ -66,6 +66,8 @@ super(SimplifyNodeVisitor, self).__init__() self.__error = errorCallback + + self.__classDefinitionStack = [] def visit_Expr(self, node): """ @@ -85,7 +87,7 @@ @param node reference to the Assign node @type ast.Assign """ - self.__check121(node) + self.__check181(node) self.generic_visit(node) @@ -168,6 +170,7 @@ @type ast.Call """ self.__check115(node) + self.__check182(node) self.generic_visit(node) @@ -201,10 +204,15 @@ @param node reference to the ClassDef node @type ast.ClassDef """ + # register the name of the class being defined + self.__classDefinitionStack.append(node.name) + self.__check119(node) self.__check120(node) self.generic_visit(node) + + self.__classDefinitionStack.pop() def visit_UnaryOp(self, node): """ @@ -1067,8 +1075,10 @@ ): self.__error(node.lineno - 1, node.col_offset, "Y120", node.name) + # TODO: extend to cover 'class FooBar(Foo, object):' + # => class FooBar(Foo): - def __check121(self, node): + def __check181(self, node): """ Private method to check for assignments that could be converted into an augmented assignment. @@ -1087,9 +1097,30 @@ ): newNode = ast.AugAssign(node.targets[0], node.value.op, node.value.right) - self.__error(node.lineno - 1, node.col_offset, "Y121", + self.__error(node.lineno - 1, node.col_offset, "Y181", unparse(newNode), unparse(node)) + def __check182(self, node): + """ + Private method to check for calls of type 'super(Foo, self)' that could + be shortened to 'super()'. + + @param node reference to the AST node to be checked + @type ast.Call + """ + # super(Foo, self) + if ( + self.__classDefinitionStack and + isinstance(node.func, ast.Name) and + node.func.id == "super" and + len(node.args) == 2 and + all(isinstance(arg, ast.Name) for arg in node.args) and + node.args[0].id == self.__classDefinitionStack[-1] and + node.args[1].id == "self" + ): + self.__error(node.lineno - 1, node.col_offset, "Y182", + unparse(node)) + def __check201(self, node): """ Private method to check for calls where an unary 'not' is used for
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/translations.py Fri Apr 09 21:14:51 2021 +0200 +++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/translations.py Sat Apr 10 12:34:29 2021 +0200 @@ -76,9 +76,14 @@ "Y120": QCoreApplication.translate( "SimplifyChecker", '''Use "class {0}:" instead of "class {0}(object):"'''), - "Y121": QCoreApplication.translate( + + # Python-specifics not part of flake8-simplify + "Y181": QCoreApplication.translate( "SimplifyChecker", '''Use "{0}" instead of "{1}"'''), + "Y182": QCoreApplication.translate( + "SimplifyChecker", + '''Use "super()" instead of "{0}"'''), # Comparations "Y201": QCoreApplication.translate( @@ -152,7 +157,10 @@ "Y118": ["foo", "bar_dict"], "Y119": ["Foo"], "Y120": ["Foo"], - "Y121": ["foo += 42", "foo = foo + 42"], + + # Python-specifics not part of flake8-simplify + "Y181": ["foo += 42", "foo = foo + 42"], + "Y182": ["super(Foo, self)"], # Comparations "Y201": ["foo", "bar"],