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

branch
eric7
changeset 10753
031cfa81992a
parent 10439
21c28b0f9e41
child 10754
6faecb62f3a4
diff -r d4dbb6b75bdc -r 031cfa81992a src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Fri Jun 07 13:51:43 2024 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsChecker.py	Sat Jun 08 15:01:47 2024 +0200
@@ -201,6 +201,13 @@
         """
         from .AnnotationsFunctionVisitor import FunctionVisitor
 
+        # Type ignores are provided by ast at the module level & we'll need them later
+        # when deciding whether or not to emit errors for a given function
+        typeIgnoreLineno = {ti.lineno for ti in self.__tree.type_ignores}
+        hasMypyIgnoreErrors = (
+            any("# mypy: ignore-errors" in line for line in self.__source[:5])
+        )
+
         suppressNoneReturning = self.__args.get(
             "SuppressNoneReturning",
             AnnotationsCheckerDefaultArgs["SuppressNoneReturning"],
@@ -220,6 +227,9 @@
         allowStarArgAny = self.__args.get(
             "AllowStarArgAny", AnnotationsCheckerDefaultArgs["AllowStarArgAny"]
         )
+        respectTypeIgnore = self.__args.get(
+            "RespectTypeIgnore", AnnotationsCheckerDefaultArgs["RespectTypeIgnore"]
+        )
 
         # Store decorator lists as sets for easier lookup
         dispatchDecorators = set(
@@ -285,6 +295,21 @@
             if function.hasDecorator(overloadDecorators):
                 lastOverloadDecoratedFunctionName = function.name
 
+            # Optionally respect a 'type: ignore' comment
+            # These are considered at the function level & tags are not considered
+            if respectTypeIgnore:
+                if function.lineno in typeIgnoreLineno:
+                    # function-level ignore
+                    continue
+                elif (
+                    any(lineno in typeIgnoreLineno for lineno in range(1, 6))
+                    or hasMypyIgnoreErrors
+                ):
+                    # module-level ignore
+                    # lineno from ast is 1-indexed
+                    # check first five lines
+                    continue
+
             # Record explicit errors for arguments that are missing annotations
             for arg in function.getMissedAnnotations():
                 # Check for type comments here since we're not considering them as

eric ide

mercurial