--- 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