src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py

branch
eric7
changeset 10087
65b7354b0d4c
parent 10086
c8854a6300d1
child 10119
64147a7e6393
diff -r c8854a6300d1 -r 65b7354b0d4c src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Wed May 31 13:53:05 2023 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Thu Jun 01 11:44:06 2023 +0200
@@ -53,8 +53,10 @@
         ## Annotation Complexity
         "A891",
         "A892",
-        ## PEP 604 (use of typing.Union)
+        ## use of typing.Union (PEP 604)
         "A901",
+        ## deprecated 'typing' symbols (PEP 585)
+        "A911",
     ]
 
     def __init__(self, source, filename, tree, select, ignore, expected, repeat, args):
@@ -116,6 +118,7 @@
             (self.__checkAnnotationsCoverage, ("A881",)),
             (self.__checkAnnotationComplexity, ("A891", "A892")),
             (self.__checkAnnotationPep604, ("A901",)),
+            (self.__checkDeprecatedTypingSymbols, ("A911",)),
         ]
 
         self.__checkers = []
@@ -629,6 +632,10 @@
         """
         from .AnnotationsFutureVisitor import AnnotationsFutureVisitor
 
+        if sys.version_info >= (3, 9):
+            # the __future__ typing import is only needed before Python 3.9
+            return
+
         forceFutureAnnotations = self.__args.get(
             "ForceFutureAnnotations",
             AnnotationsCheckerDefaultArgs["ForceFutureAnnotations"],
@@ -666,8 +673,47 @@
         """
         from .AnnotationsUnionVisitor import AnnotationsUnionVisitor
 
+        if sys.version_info < (3, 10):
+            # the union operator was introduced with Python 3.10
+            return
+
         visitor = AnnotationsUnionVisitor()
         visitor.visit(self.__tree)
 
         for node in visitor.getIssues():
             self.__error(node.lineno - 1, node.col_offset, "A901")
+
+    #######################################################################
+    ## check use of 'typing.Union' (see PEP 604)
+    ##
+    ## adapted from: flake8-pep585 v0.1.7
+    #######################################################################
+
+    def __checkDeprecatedTypingSymbols(self):
+        """
+        Private method to check the use of deprecated 'typing' symbols.
+        """
+        from .AnnotationsDeprecationsVisitor import (
+            AnnotationsDeprecationsVisitor,
+            AnnotationsFutureImportVisitor,
+        )
+
+        if sys.version_info < (3, 7):  # play it safe
+            return
+        elif sys.version_info < (3, 9):
+            # py 3.8: only if activated via __future__ import
+            visitor = AnnotationsFutureImportVisitor()
+            visitor.visit(self.__tree)
+            if not visitor.futureImportPresent():
+                return
+
+        visitor = AnnotationsDeprecationsVisitor(
+            self.__args.get(
+                "ExemptedTypingSymbols",
+                AnnotationsCheckerDefaultArgs["ExemptedTypingSymbols"],
+            )
+        )
+        visitor.visit(self.__tree)
+
+        for node, (name, replacement) in visitor.getIssues():
+            self.__error(node.lineno - 1, node.col_offset, "A911", name, replacement)

eric ide

mercurial