--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py Thu Apr 01 17:23:35 2021 +0200 +++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py Thu Apr 01 19:48:36 2021 +0200 @@ -8,14 +8,9 @@ """ import ast -import collections import sys -try: - from ast import unparse -except AttributeError: - # Python < 3.9 - from .ast_unparse import unparse +from .SimplifyNodeVisitor import SimplifyNodeVisitor class SimplifyChecker(object): @@ -23,7 +18,11 @@ Class implementing a checker for to help simplifying Python code. """ Codes = [ - "Y101", + # Python-specifics + "Y101", "Y102", "Y103", "Y104", "Y105", "Y106", "Y107", "Y108", + "Y109", "Y110", "Y111", "Y112", + + # Comparations ] def __init__(self, source, filename, selected, ignored, expected, repeat): @@ -150,100 +149,5 @@ self.__reportInvalidSyntax() return - visitor = SimplifyVisitor(self.__error) + visitor = SimplifyNodeVisitor(self.__error) visitor.visit(self.__tree) - -###################################################################### -## The following code is derived from the flake8-simplify package. -## -## Original License: -## -## MIT License -## -## Copyright (c) 2020 Martin Thoma -## -## Permission is hereby granted, free of charge, to any person obtaining a copy -## of this software and associated documentation files (the "Software"), to -## deal in the Software without restriction, including without limitation the -## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -## sell copies of the Software, and to permit persons to whom the Software is -## furnished to do so, subject to the following conditions: -## -## The above copyright notice and this permission notice shall be included in -## all copies or substantial portions of the Software. -## -## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -## IN THE SOFTWARE. -###################################################################### - - -class SimplifyVisitor(ast.NodeVisitor): - """ - Class to traverse the AST node tree and check for code that can be - simplified. - """ - def __init__(self, errorCallback): - """ - Constructor - - @param checkCallback callback function taking a reference to the - AST node and the resolved name - @type func - """ - super(SimplifyVisitor, self).__init__() - - self.__error = errorCallback - - def visit_BoolOp(self, node): - """ - Public method to process a BoolOp node. - - @param node reference to the BoolOp node - @type ast.BoolOp - """ - self.__check101(node) - - ############################################################# - ## Methods to check for possible code simplifications below - ############################################################# - - def __getDuplicatedIsinstanceCall(self, node): - """ - Private method to get a list of isinstance arguments which could - be combined. - - @param node reference to the AST node to be inspected - @type ast.BoolOp - """ - counter = collections.defaultdict(int) - - for call in node.values: - # Ensure this is a call of the built-in isinstance() function. - if not isinstance(call, ast.Call) or len(call.args) != 2: - continue - functionName = call.func.id - if functionName != "isinstance": - continue - - arg0Name = unparse(call.args[0]) - counter[arg0Name] += 1 - - return [name for name, count in counter.items() if count > 1] - - def __check101(self, node): - """ - Private method to check for duplicate isinstance() calls. - - @param node reference to the AST node to be checked - @type ast.BoolOp - """ - if not isinstance(node.op, ast.Or): - return - - for variable in self.__getDuplicatedIsinstanceCall(node): - self.__error(node.lineno - 1, node.col_offset, "Y101", variable)