eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py

changeset 8194
b925628bf91f
parent 8192
e1157bd8b4c2
child 8195
db7f2badd374
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Sat Apr 03 09:47:25 2021 +0200
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Sat Apr 03 10:44:07 2021 +0200
@@ -115,7 +115,7 @@
         self.__check104(node)
         self.__check110_111(node)
         self.__check113(node)
-        self.__check118b(node)
+        self.__check118(node)
         
         self.generic_visit(node)
     
@@ -160,7 +160,7 @@
         @param node reference to the Compare node
         @type ast.Compare
         """
-        self.__check118a(node)
+        self.__check118(node)
         
         self.generic_visit(node)
     
@@ -172,6 +172,7 @@
         @type ast.ClassDef
         """
         self.__check119(node)
+        self.__check120(node)
         
         self.generic_visit(node)
     
@@ -487,6 +488,14 @@
         #     b = c
         # else:
         #     b = d
+        #
+        # but not:
+        # if a:
+        #     b = c
+        # elif c:
+        #     b = e
+        # else:
+        #     b = d
         if (
             len(node.body) == 1 and
             len(node.orelse) == 1 and
@@ -496,7 +505,8 @@
             len(node.orelse[0].targets) == 1 and
             isinstance(node.body[0].targets[0], ast.Name) and
             isinstance(node.orelse[0].targets[0], ast.Name) and
-            node.body[0].targets[0].id == node.orelse[0].targets[0].id
+            node.body[0].targets[0].id == node.orelse[0].targets[0].id and
+            not isinstance(node.parent, ast.If)
         ):
             assign = unparse(node.body[0].targets[0])
             body = unparse(node.body[0].value)
@@ -504,10 +514,9 @@
             orelse = unparse(node.orelse[0].value)
             if len(
                 "{0} = {1} if {2} else {3}".format(assign, body, cond, orelse)
-            ) > 79:
-                self.__error(node.lineno - 1, node.col_offset, "Y108a")
-            else:
-                self.__error(node.lineno - 1, node.col_offset, "Y108b",
+            ) < 79:
+                # don't flag an issue, if the ternary would get too complicated
+                self.__error(node.lineno - 1, node.col_offset, "Y108",
                              assign, body, cond, orelse)
     
     def __check109(self, node):
@@ -848,44 +857,36 @@
             mergedWith = f"with {', '.join(withItems)}:"
             self.__error(node.lineno - 1, node.col_offset, "Y117", mergedWith)
     
-    def __check118a(self, node):
+    def __check118(self, node):
         """
         Private method to check for usages of "key in dict.keys()".
         
         @param node reference to the AST node to be checked
-        @type ast.Compare
+        @type ast.Compare or ast.For
         """
-        # key in dict.keys()
+        # Pattern 1:
+        #
+        # if key in dict.keys():
+        #     # do something
+        #
+        # Pattern 2:
+        #
+        # for key in dict.keys():
+        #     # do something
         if (
+            isinstance(node, ast.Compare) and
             len(node.ops) == 1 and
             isinstance(node.ops[0], ast.In) and
             len(node.comparators) == 1
         ):
             callNode = node.comparators[0]
-            if not isinstance(callNode, ast.Call):
-                return
-            
-            attrNode = callNode.func
-            if (
-                isinstance(callNode.func, ast.Attribute) and
-                callNode.func.attr == "keys" and
-                isinstance(callNode.func.ctx, ast.Load)
-            ):
-                keyStr = unparse(node.left)
-                dictStr = unparse(attrNode.value)
-                self.__error(node.lineno - 1, node.col_offset, "Y118",
-                             keyStr, dictStr)
-    
-    def __check118b(self, node):
-        """
-        Private method to check for usages of "key in dict.keys()".
+        elif (
+            isinstance(node, ast.For)
+        ):
+            callNode = node.iter
+        else:
+            callNode = None
         
-        @param node reference to the AST node to be checked
-        @type ast.For
-        """
-        # for key in dict.keys():
-        #     # do something
-        callNode = node.iter
         if not isinstance(callNode, ast.Call):
             return
         
@@ -895,14 +896,17 @@
             callNode.func.attr == "keys" and
             isinstance(callNode.func.ctx, ast.Load)
         ):
-            keyStr = unparse(node.target)
+            if isinstance(node, ast.Compare):
+                keyStr = unparse(node.left)
+            else:
+                keyStr = unparse(node.target)
             dictStr = unparse(attrNode.value)
             self.__error(node.lineno - 1, node.col_offset, "Y118",
                          keyStr, dictStr)
     
     def __check119(self, node):
         """
-        Public method to check for classes that should be "dataclasses".
+        Private method to check for classes that should be "dataclasses".
         
         @param node reference to the AST node to be checked
         @type ast.ClassDef
@@ -934,6 +938,23 @@
             ):
                 self.__error(node.lineno - 1, node.col_offset, "Y119",
                              node.name)
+    
+    def __check120(self, node):
+        """
+        Private method to check for classes that inherit from object.
+        
+        @param node reference to the AST node to be checked
+        @type ast.ClassDef
+        """
+        # class FooBar(object):
+        #     ...
+        if (
+            len(node.bases) == 1 and
+            isinstance(node.bases[0], ast.Name) and
+            node.bases[0].id == "object"
+        ):
+            self.__error(node.lineno - 1, node.col_offset, "Y120",
+                         node.name)
 
 #
 # eflag: noqa = M891

eric ide

mercurial