src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py

branch
eric7
changeset 10169
0f70a4ef4592
parent 10116
4a619fb7bd09
child 10175
57ed3cb66e9a
diff -r 8312e0e76795 -r 0f70a4ef4592 src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Tue Aug 29 16:55:01 2023 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Tue Aug 29 16:55:18 2023 +0200
@@ -19,6 +19,8 @@
     # Python < 3.9
     from ast_unparse import unparse
 
+import AstUtilities
+
 ###############################################################################
 ## The following code is derived from the flake8-simplify package (v0.20.0).
 ##
@@ -47,10 +49,6 @@
 ## IN THE SOFTWARE.
 ###############################################################################
 
-BOOL_CONST_TYPES = (ast.Constant, ast.NameConstant)
-AST_CONST_TYPES = (ast.Constant, ast.NameConstant, ast.Str, ast.Num)
-STR_TYPES = (ast.Constant, ast.Str)
-
 
 class SimplifyNodeVisitor(ast.NodeVisitor):
     """
@@ -295,7 +293,6 @@
         """
         return isinstance(expr.op, ast.Add) and (
             (isinstance(expr.value, ast.Constant) and expr.value.value == 1)
-            or (isinstance(expr.value, ast.Num) and expr.value.n == 1)
         )
 
     def __getIfBodyPairs(self, node):
@@ -526,13 +523,13 @@
         if not (
             len(node.body) != 1
             or not isinstance(node.body[0], ast.Return)
-            or not isinstance(node.body[0].value, BOOL_CONST_TYPES)
+            or not isinstance(node.body[0].value, ast.Constant)
             or not (
                 node.body[0].value.value is True or node.body[0].value.value is False
             )
             or len(node.orelse) != 1
             or not isinstance(node.orelse[0], ast.Return)
-            or not isinstance(node.orelse[0].value, BOOL_CONST_TYPES)
+            or not isinstance(node.orelse[0].value, ast.Constant)
             or not (
                 node.orelse[0].value.value is True
                 or node.orelse[0].value.value is False
@@ -780,7 +777,7 @@
             and isinstance(node.body[0], ast.If)
             and len(node.body[0].body) == 1
             and isinstance(node.body[0].body[0], ast.Return)
-            and isinstance(node.body[0].body[0].value, BOOL_CONST_TYPES)
+            and isinstance(node.body[0].body[0].value, ast.Constant)
             and hasattr(node.body[0].body[0].value, "value")
             and isinstance(node.next_sibling, ast.Return)
         ):
@@ -824,7 +821,7 @@
             and (
                 (
                     isinstance(node.value.slice, ast.Index)
-                    and isinstance(node.value.slice.value, STR_TYPES)
+                    and isinstance(node.value.slice.value, ast.Constant)
                 )
                 or isinstance(node.value.slice, ast.Constant)
             )
@@ -835,10 +832,7 @@
             if isinstance(slice_, ast.Index):
                 # Python < 3.9
                 stringPart = slice_.value  # type: ignore
-                if isinstance(stringPart, ast.Str):
-                    envName = stringPart.s  # Python 3.6 / 3.7 fallback
-                else:
-                    envName = stringPart.value
+                envName = stringPart.value
             elif isinstance(slice_, ast.Constant):
                 # Python 3.9
                 envName = slice_.value
@@ -855,15 +849,12 @@
             and node.value.func.value.attr == "environ"
             and node.value.func.attr == "get"
             and len(node.value.args) in [1, 2]
-            and isinstance(node.value.args[0], STR_TYPES)
+            and isinstance(node.value.args[0], ast.Constant)
         )
         if isGetCall:
             call = node.value
             stringPart = call.args[0]
-            if isinstance(stringPart, ast.Str):
-                envName = stringPart.s  # Python 3.6 / 3.7 fallback
-            else:
-                envName = stringPart.value
+            envName = stringPart.value
             # Check if this has a change
             hasChange = envName != envName.upper()
         if not (isIndexCall or isGetCall) or not hasChange:
@@ -1006,7 +997,7 @@
             and len(node.test.ops) == 1
             and isinstance(node.test.ops[0], ast.Eq)
             and len(node.test.comparators) == 1
-            and isinstance(node.test.comparators[0], AST_CONST_TYPES)
+            and isinstance(node.test.comparators[0], ast.Constant)
             and len(node.body) == 1
             and isinstance(node.body[0], ast.Return)
             and len(node.orelse) == 1
@@ -1019,15 +1010,13 @@
                 bodyValueStr = unparse(node.body[0].value).strip("'")
             else:
                 bodyValueStr = "None"
-            if isinstance(node.test.comparators[0], ast.Str):
+            if AstUtilities.isString(node.test.comparators[0]):
                 value = (
                     bodyValueStr
                     if bodyValueStr[0] == '"' and bodyValueStr[-1] == '"'
                     else bodyValueStr[1:-1]
                 )
                 keyValuePairs = {node.test.comparators[0].s: value}
-            elif isinstance(node.test.comparators[0], ast.Num):
-                keyValuePairs = {node.test.comparators[0].n: bodyValueStr}
             else:
                 keyValuePairs = {node.test.comparators[0].value: bodyValueStr}
             while child:
@@ -1038,7 +1027,7 @@
                     and len(child.test.ops) == 1
                     and isinstance(child.test.ops[0], ast.Eq)
                     and len(child.test.comparators) == 1
-                    and isinstance(child.test.comparators[0], AST_CONST_TYPES)
+                    and isinstance(child.test.comparators[0], ast.Constant)
                     and len(child.body) == 1
                     and isinstance(child.body[0], ast.Return)
                     and len(child.orelse) <= 1
@@ -1049,12 +1038,7 @@
                 if isinstance(returnCall.value, ast.Call):
                     return
 
-                if isinstance(child.test.comparators[0], ast.Str):
-                    key = child.test.comparators[0].s
-                elif isinstance(child.test.comparators[0], ast.Num):
-                    key = child.test.comparators[0].n
-                else:
-                    key = child.test.comparators[0].value
+                key = child.test.comparators[0].value
 
                 value = unparse(child.body[0].value)
                 if value[0] == '"' and value[-1] == '"':
@@ -1537,9 +1521,9 @@
         """
         # True if a else False
         if (
-            isinstance(node.body, BOOL_CONST_TYPES)
+            isinstance(node.body, ast.Constant)
             and node.body.value is True
-            and isinstance(node.orelse, BOOL_CONST_TYPES)
+            and isinstance(node.orelse, ast.Constant)
             and node.orelse.value is False
         ):
             cond = unparse(node.test)
@@ -1558,9 +1542,9 @@
         """
         # False if a else True
         if (
-            isinstance(node.body, BOOL_CONST_TYPES)
+            isinstance(node.body, ast.Constant)
             and node.body.value is False
-            and isinstance(node.orelse, BOOL_CONST_TYPES)
+            and isinstance(node.orelse, ast.Constant)
             and node.orelse.value is True
         ):
             cond = unparse(node.test)
@@ -1648,7 +1632,7 @@
         # a or True
         if isinstance(node.op, ast.Or):
             for exp in node.values:
-                if isinstance(exp, BOOL_CONST_TYPES) and exp.value is True:
+                if isinstance(exp, ast.Constant) and exp.value is True:
                     self.__error(node.lineno - 1, node.col_offset, "Y223")
 
     def __check224(self, node):
@@ -1661,7 +1645,7 @@
         # a and False
         if isinstance(node.op, ast.And):
             for exp in node.values:
-                if isinstance(exp, BOOL_CONST_TYPES) and exp.value is False:
+                if isinstance(exp, ast.Constant) and exp.value is False:
                     self.__error(node.lineno - 1, node.col_offset, "Y224")
 
     def __check301(self, node):
@@ -1673,16 +1657,15 @@
         """
         # 42 == age
         if (
-            isinstance(node.left, AST_CONST_TYPES)
+            isinstance(node.left, ast.Constant)
             and len(node.ops) == 1
             and isinstance(node.ops[0], ast.Eq)
         ):
             left = unparse(node.left)
-            isPy37Str = isinstance(node.left, ast.Str)
-            isPy38Str = isinstance(node.left, ast.Constant) and isinstance(
+            isStr = isinstance(node.left, ast.Constant) and isinstance(
                 node.left.value, str
             )
-            if isPy37Str or isPy38Str:
+            if isStr:
                 left = f"'{left}'"
             right = unparse(node.comparators[0])
             self.__error(node.lineno - 1, node.col_offset, "Y301", left, right)
@@ -1777,12 +1760,9 @@
         if (
             isinstance(node.func, ast.Attribute)
             and node.func.attr == "split"
-            and isinstance(node.func.value, (ast.Str, ast.Constant))
+            and isinstance(node.func.value, ast.Constant)
         ):
-            if isinstance(node.func.value, ast.Constant):
-                value = node.func.value.value
-            else:
-                value = node.func.value.s
+            value = node.func.value.value
 
             expected = json.dumps(value.split())
             actual = unparse(node.func.value) + ".split()"
@@ -1811,8 +1791,8 @@
                     names += getOsPathJoinArgs(arg)
                 elif isinstance(arg, ast.Name):
                     names.append(arg.id)
-                elif isinstance(arg, ast.Str):
-                    names.append(f"'{arg.s}'")
+                elif AstUtilities.isString(arg):
+                    names.append(f"'{arg.value}'")
             return names
 
         if (
@@ -1864,7 +1844,7 @@
             hasNone = False
             others = []
             for elt in tupleVar.elts:
-                if isinstance(elt, BOOL_CONST_TYPES) and elt.value is None:
+                if isinstance(elt, ast.Constant) and elt.value is None:
                     hasNone = True
                 else:
                     others.append(elt)
@@ -1909,7 +1889,7 @@
         # check the argument value
         if not (
             len(node.args) == 2
-            and isinstance(node.args[1], BOOL_CONST_TYPES)
+            and isinstance(node.args[1], ast.Constant)
             and node.args[1].value is None
         ):
             return

eric ide

mercurial