51 ## Annotation Coverage |
51 ## Annotation Coverage |
52 "A881", |
52 "A881", |
53 ## Annotation Complexity |
53 ## Annotation Complexity |
54 "A891", |
54 "A891", |
55 "A892", |
55 "A892", |
56 ## PEP 604 (use of typing.Union) |
56 ## use of typing.Union (PEP 604) |
57 "A901", |
57 "A901", |
|
58 ## deprecated 'typing' symbols (PEP 585) |
|
59 "A911", |
58 ] |
60 ] |
59 |
61 |
60 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): |
62 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): |
61 """ |
63 """ |
62 Constructor |
64 Constructor |
114 ), |
116 ), |
115 (self.__checkAnnotationsFuture, ("A871", "A872", "A873")), |
117 (self.__checkAnnotationsFuture, ("A871", "A872", "A873")), |
116 (self.__checkAnnotationsCoverage, ("A881",)), |
118 (self.__checkAnnotationsCoverage, ("A881",)), |
117 (self.__checkAnnotationComplexity, ("A891", "A892")), |
119 (self.__checkAnnotationComplexity, ("A891", "A892")), |
118 (self.__checkAnnotationPep604, ("A901",)), |
120 (self.__checkAnnotationPep604, ("A901",)), |
|
121 (self.__checkDeprecatedTypingSymbols, ("A911",)), |
119 ] |
122 ] |
120 |
123 |
121 self.__checkers = [] |
124 self.__checkers = [] |
122 for checker, codes in checkersWithCodes: |
125 for checker, codes in checkersWithCodes: |
123 if any(not (code and self.__ignoreCode(code)) for code in codes): |
126 if any(not (code and self.__ignoreCode(code)) for code in codes): |
627 """ |
630 """ |
628 Private method to check the use of __future__ and typing imports. |
631 Private method to check the use of __future__ and typing imports. |
629 """ |
632 """ |
630 from .AnnotationsFutureVisitor import AnnotationsFutureVisitor |
633 from .AnnotationsFutureVisitor import AnnotationsFutureVisitor |
631 |
634 |
|
635 if sys.version_info >= (3, 9): |
|
636 # the __future__ typing import is only needed before Python 3.9 |
|
637 return |
|
638 |
632 forceFutureAnnotations = self.__args.get( |
639 forceFutureAnnotations = self.__args.get( |
633 "ForceFutureAnnotations", |
640 "ForceFutureAnnotations", |
634 AnnotationsCheckerDefaultArgs["ForceFutureAnnotations"], |
641 AnnotationsCheckerDefaultArgs["ForceFutureAnnotations"], |
635 ) |
642 ) |
636 checkFutureAnnotations = self.__args.get( |
643 checkFutureAnnotations = self.__args.get( |
664 """ |
671 """ |
665 Private method to check the use of typing.Union. |
672 Private method to check the use of typing.Union. |
666 """ |
673 """ |
667 from .AnnotationsUnionVisitor import AnnotationsUnionVisitor |
674 from .AnnotationsUnionVisitor import AnnotationsUnionVisitor |
668 |
675 |
|
676 if sys.version_info < (3, 10): |
|
677 # the union operator was introduced with Python 3.10 |
|
678 return |
|
679 |
669 visitor = AnnotationsUnionVisitor() |
680 visitor = AnnotationsUnionVisitor() |
670 visitor.visit(self.__tree) |
681 visitor.visit(self.__tree) |
671 |
682 |
672 for node in visitor.getIssues(): |
683 for node in visitor.getIssues(): |
673 self.__error(node.lineno - 1, node.col_offset, "A901") |
684 self.__error(node.lineno - 1, node.col_offset, "A901") |
|
685 |
|
686 ####################################################################### |
|
687 ## check use of 'typing.Union' (see PEP 604) |
|
688 ## |
|
689 ## adapted from: flake8-pep585 v0.1.7 |
|
690 ####################################################################### |
|
691 |
|
692 def __checkDeprecatedTypingSymbols(self): |
|
693 """ |
|
694 Private method to check the use of deprecated 'typing' symbols. |
|
695 """ |
|
696 from .AnnotationsDeprecationsVisitor import ( |
|
697 AnnotationsDeprecationsVisitor, |
|
698 AnnotationsFutureImportVisitor, |
|
699 ) |
|
700 |
|
701 if sys.version_info < (3, 7): # play it safe |
|
702 return |
|
703 elif sys.version_info < (3, 9): |
|
704 # py 3.8: only if activated via __future__ import |
|
705 visitor = AnnotationsFutureImportVisitor() |
|
706 visitor.visit(self.__tree) |
|
707 if not visitor.futureImportPresent(): |
|
708 return |
|
709 |
|
710 visitor = AnnotationsDeprecationsVisitor( |
|
711 self.__args.get( |
|
712 "ExemptedTypingSymbols", |
|
713 AnnotationsCheckerDefaultArgs["ExemptedTypingSymbols"], |
|
714 ) |
|
715 ) |
|
716 visitor.visit(self.__tree) |
|
717 |
|
718 for node, (name, replacement) in visitor.getIssues(): |
|
719 self.__error(node.lineno - 1, node.col_offset, "A911", name, replacement) |