src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py

branch
eric7
changeset 10085
b5808c3a9967
parent 10048
df836ff707fd
child 10119
64147a7e6393
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py	Mon May 29 16:18:38 2023 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Miscellaneous/MiscellaneousChecker.py	Tue May 30 17:25:17 2023 +0200
@@ -76,6 +76,15 @@
         "M198",
         ## Dictionaries with sorted keys
         "M201",
+        ## Property
+        "M210",
+        "M211",
+        "M212",
+        "M213",
+        "M214",
+        "M215",
+        "M216",
+        "M217",
         ## Naive datetime usage
         "M301",
         "M302",
@@ -160,7 +169,7 @@
         "M702",
         ## Gettext
         "M711",
-        ## print
+        ## print() statements
         "M801",
         ## one element tuple
         "M811",
@@ -258,6 +267,10 @@
             ),
             (self.__checkDictWithSortedKeys, ("M201",)),
             (
+                self.__checkProperties,
+                ("M210", "M211", "M212", "M213", "M214", "M215", "M216", "M217"),
+            ),
+            (
                 self.__checkDateTime,
                 (
                     "M301",
@@ -1202,6 +1215,90 @@
             reason = violation[1]
             self.__error(node.lineno - 1, node.col_offset, reason)
 
+    def __checkProperties(self):
+        """
+        Private method to check for issue with property related methods.
+        """
+        properties = []
+        for node in ast.walk(self.__tree):
+            if isinstance(node, ast.ClassDef):
+                properties.clear()
+
+            elif isinstance(node, ast.FunctionDef):
+                propertyCount = 0
+                for decorator in node.decorator_list:
+                    # property getter method
+                    if isinstance(decorator, ast.Name) and decorator.id == "property":
+                        propertyCount += 1
+                        properties.append(node.name)
+                        if len(node.args.args) != 1:
+                            self.__error(
+                                node.lineno - 1,
+                                node.col_offset,
+                                "M210",
+                                len(node.args.args),
+                            )
+
+                    if isinstance(decorator, ast.Attribute):
+                        # property setter method
+                        if decorator.attr == "setter":
+                            propertyCount += 1
+                            if node.name != decorator.value.id:
+                                if node.name in properties:
+                                    self.__error(
+                                        node.lineno - 1,
+                                        node.col_offset,
+                                        "M215",
+                                        node.name,
+                                        decorator.value.id,
+                                    )
+                                else:
+                                    self.__error(
+                                        node.lineno - 1,
+                                        node.col_offset,
+                                        "M213",
+                                        decorator.value.id,
+                                        node.name,
+                                    )
+                            if len(node.args.args) != 2:
+                                self.__error(
+                                    node.lineno - 1,
+                                    node.col_offset,
+                                    "M211",
+                                    len(node.args.args),
+                                )
+
+                        # property deleter method
+                        if decorator.attr == "deleter":
+                            propertyCount += 1
+                            if node.name != decorator.value.id:
+                                if node.name in properties:
+                                    self.__error(
+                                        node.lineno - 1,
+                                        node.col_offset,
+                                        "M216",
+                                        node.name,
+                                        decorator.value.id,
+                                    )
+                                else:
+                                    self.__error(
+                                        node.lineno - 1,
+                                        node.col_offset,
+                                        "M214",
+                                        decorator.value.id,
+                                        node.name,
+                                    )
+                            if len(node.args.args) != 1:
+                                self.__error(
+                                    node.lineno - 1,
+                                    node.col_offset,
+                                    "M212",
+                                    len(node.args.args),
+                                )
+
+                if propertyCount > 1:
+                    self.__error(node.lineno - 1, node.col_offset, "M217", node.name)
+
 
 class TextVisitor(ast.NodeVisitor):
     """

eric ide

mercurial