507 |
507 |
508 def __getAnnotationComplexity(self, annotationNode, defaultComplexity=1): |
508 def __getAnnotationComplexity(self, annotationNode, defaultComplexity=1): |
509 """ |
509 """ |
510 Private method to determine the annotation complexity. |
510 Private method to determine the annotation complexity. |
511 |
511 |
|
512 It recursively counts the complexity of annotation nodes. When |
|
513 annotations are written as strings, it additionally parses them |
|
514 to 'ast' nodes. |
|
515 |
512 @param annotationNode reference to the node to determine the annotation |
516 @param annotationNode reference to the node to determine the annotation |
513 complexity for |
517 complexity for |
514 @type ast.AST |
518 @type ast.AST |
515 @param defaultComplexity default complexity value |
519 @param defaultComplexity default complexity value (defaults to 1) |
516 @type int |
520 @type int (optional) |
517 @return annotation complexity |
521 @return annotation complexity |
518 @rtype = int |
522 @rtype = int |
519 """ |
523 """ |
520 if AstUtilities.isString(annotationNode): |
524 if AstUtilities.isString(annotationNode): |
521 try: |
525 try: |
522 annotationNode = ast.parse(annotationNode.value).body[0].value |
526 annotationNode = ast.parse(annotationNode.value).body[0].value |
523 except (IndexError, SyntaxError): |
527 except (IndexError, SyntaxError): |
524 return defaultComplexity |
528 return defaultComplexity |
525 |
529 |
526 complexity = defaultComplexity |
|
527 if isinstance(annotationNode, ast.Subscript): |
530 if isinstance(annotationNode, ast.Subscript): |
528 if sys.version_info >= (3, 9): |
531 return defaultComplexity + self.__getAnnotationComplexity( |
529 complexity = defaultComplexity + self.__getAnnotationComplexity( |
|
530 annotationNode.slice |
532 annotationNode.slice |
531 ) |
533 ) |
532 else: |
534 |
533 complexity = defaultComplexity + self.__getAnnotationComplexity( |
535 if isinstance(annotationNode, (ast.Tuple, ast.List)): |
534 annotationNode.slice.value |
536 return max( |
535 ) |
|
536 |
|
537 if isinstance(annotationNode, ast.Tuple): |
|
538 complexity = max( |
|
539 (self.__getAnnotationComplexity(n) for n in annotationNode.elts), |
537 (self.__getAnnotationComplexity(n) for n in annotationNode.elts), |
540 default=defaultComplexity, |
538 default=defaultComplexity, |
541 ) |
539 ) |
542 |
540 |
543 return complexity |
541 return defaultComplexity |
544 |
542 |
545 def __getAnnotationLength(self, annotationNode): |
543 def __getAnnotationLength(self, annotationNode): |
546 """ |
544 """ |
547 Private method to determine the annotation length. |
545 Private method to determine the annotation length. |
|
546 |
|
547 It recursively counts the length of annotation nodes. When annotations |
|
548 are written as strings, it additionally parses them to 'ast' nodes. |
548 |
549 |
549 @param annotationNode reference to the node to determine the annotation |
550 @param annotationNode reference to the node to determine the annotation |
550 length for |
551 length for |
551 @type ast.AST |
552 @type ast.AST |
552 @return annotation length |
553 @return annotation length |
553 @rtype = int |
554 @rtype = int |
554 """ |
555 """ |
555 annotationLength = 0 |
|
556 if AstUtilities.isString(annotationNode): |
556 if AstUtilities.isString(annotationNode): |
|
557 # try to parse string-wrapped annotations |
557 try: |
558 try: |
558 annotationNode = ast.parse(annotationNode.value).body[0].value |
559 annotationNode = ast.parse(annotationNode.value).body[0].value |
559 except (IndexError, SyntaxError): |
560 except (IndexError, SyntaxError): |
560 return annotationLength |
561 return 0 |
561 |
562 |
562 if isinstance(annotationNode, ast.Subscript): |
563 if isinstance(annotationNode, ast.Subscript): |
563 with contextlib.suppress(AttributeError): |
564 with contextlib.suppress(AttributeError): |
564 annotationLength = ( |
565 return len(annotationNode.slice.elts) |
565 len(annotationNode.slice.elts) |
566 |
566 if sys.version_info >= (3, 9) |
567 return 0 |
567 else len(annotationNode.slice.value.elts) |
568 |
568 ) |
|
569 |
|
570 return annotationLength |
|
571 |
569 |
572 ####################################################################### |
570 ####################################################################### |
573 ## 'from __future__ import annotations' check |
571 ## 'from __future__ import annotations' check |
574 ## |
572 ## |
575 ## adapted from: flake8-future-annotations v1.1.0 |
573 ## adapted from: flake8-future-annotations v1.1.0 |