Code Style Checker eric7

Mon, 19 May 2025 14:58:16 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 19 May 2025 14:58:16 +0200
branch
eric7
changeset 11298
4d690ea28e0f
parent 11297
2c773823fb7d
child 11299
363ae9455751

Code Style Checker
- Updated the annotations checker to `flake8-annotations-complexity` v0.1.0.

docs/ThirdParty.md file | annotate | diff | comparison | revisions
docs/changelog.md file | annotate | diff | comparison | revisions
src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py file | annotate | diff | comparison | revisions
--- a/docs/ThirdParty.md	Mon May 19 14:33:49 2025 +0200
+++ b/docs/ThirdParty.md	Mon May 19 14:58:16 2025 +0200
@@ -22,7 +22,7 @@
 | flake8-2020                   |   1.8.1   | MIT License (MIT)                  |
 | flake8-alphabetize            |   0.0.21  | MIT License (MIT No Attribution)   |
 | flake8-annotations            |   3.1.1   | MIT License (MIT)                  |
-| flake8-annotations-complexity |   0.0.8   | MIT License (MIT)                  |
+| flake8-annotations-complexity |   0.1.0   | MIT License (MIT)                  |
 | flake8-annotations-coverage   |   0.0.6   | MIT License (MIT)                  |
 | flake8-async                  |  22.11.14 | MIT License (MIT)                  |
 | flake8-bugbear                |  24.12.12 | MIT License (MIT)                  |
--- a/docs/changelog.md	Mon May 19 14:33:49 2025 +0200
+++ b/docs/changelog.md	Mon May 19 14:58:16 2025 +0200
@@ -4,6 +4,7 @@
 - bug fixes
 - Code Style Checker
     - Updated these checkers.
+        - Annotations to `flake8-annotations-complexity` v0.1.0
         - Security to `bandit` v1.8.3
 - conda Manager
     - Removed the conda interface and changed it to a plug-in available via the
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Mon May 19 14:33:49 2025 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Mon May 19 14:58:16 2025 +0200
@@ -461,7 +461,7 @@
     #######################################################################
     ## Annotations Complexity
     ##
-    ## adapted from: flake8-annotations-complexity v0.0.8
+    ## adapted from: flake8-annotations-complexity v0.1.0
     #######################################################################
 
     def __checkAnnotationComplexity(self):
@@ -509,11 +509,15 @@
         """
         Private method to determine the annotation complexity.
 
+        It recursively counts the complexity of annotation nodes. When
+        annotations are written as strings, it additionally parses them
+        to 'ast' nodes.
+
         @param annotationNode reference to the node to determine the annotation
             complexity for
         @type ast.AST
-        @param defaultComplexity default complexity value
-        @type int
+        @param defaultComplexity default complexity value (defaults to 1)
+        @type int (optional)
         @return annotation complexity
         @rtype = int
         """
@@ -523,51 +527,45 @@
             except (IndexError, SyntaxError):
                 return defaultComplexity
 
-        complexity = defaultComplexity
         if isinstance(annotationNode, ast.Subscript):
-            if sys.version_info >= (3, 9):
-                complexity = defaultComplexity + self.__getAnnotationComplexity(
+                return defaultComplexity + self.__getAnnotationComplexity(
                     annotationNode.slice
                 )
-            else:
-                complexity = defaultComplexity + self.__getAnnotationComplexity(
-                    annotationNode.slice.value
-                )
 
-        if isinstance(annotationNode, ast.Tuple):
-            complexity = max(
+        if isinstance(annotationNode, (ast.Tuple, ast.List)):
+            return max(
                 (self.__getAnnotationComplexity(n) for n in annotationNode.elts),
                 default=defaultComplexity,
             )
 
-        return complexity
+        return defaultComplexity
 
     def __getAnnotationLength(self, annotationNode):
         """
         Private method to determine the annotation length.
 
+        It recursively counts the length of annotation nodes. When annotations
+        are written as strings, it additionally parses them to 'ast' nodes.
+
         @param annotationNode reference to the node to determine the annotation
             length for
         @type ast.AST
         @return annotation length
         @rtype = int
         """
-        annotationLength = 0
         if AstUtilities.isString(annotationNode):
+            # try to parse string-wrapped annotations
             try:
                 annotationNode = ast.parse(annotationNode.value).body[0].value
             except (IndexError, SyntaxError):
-                return annotationLength
+                return 0
 
         if isinstance(annotationNode, ast.Subscript):
             with contextlib.suppress(AttributeError):
-                annotationLength = (
-                    len(annotationNode.slice.elts)
-                    if sys.version_info >= (3, 9)
-                    else len(annotationNode.slice.value.elts)
-                )
+                return len(annotationNode.slice.elts)
 
-        return annotationLength
+        return 0
+
 
     #######################################################################
     ## 'from __future__ import annotations' check

eric ide

mercurial