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

Wed, 13 Jul 2022 14:55:47 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 13 Jul 2022 14:55:47 +0200
branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9276
e6748a5e24b9
permissions
-rw-r--r--

Reformatted the source code using the 'Black' utility.

8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8881
54e42bc2437a Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8773
diff changeset
3 # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a node visitor for function type annotations.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 #
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
11 # The visitor and associated classes are adapted from flake8-annotations v2.7.0
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 #
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import ast
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import itertools
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
16 import sys
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from .AnnotationsEnums import AnnotationType, ClassDecoratorType, FunctionType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # The order of AST_ARG_TYPES must match Python's grammar
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 AST_ARG_TYPES = ("posonlyargs", "args", "vararg", "kwonlyargs", "kwarg")
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 class Argument:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Class representing a function argument.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
28
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
29 def __init__(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
30 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
31 argname,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
32 lineno,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
33 col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
34 annotationType,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
35 hasTypeAnnotation=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
36 has3107Annotation=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
37 hasTypeComment=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
38 ):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
41
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 @param argname name of the argument
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @type str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @param lineno line number
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @type int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @param col_offset column number
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @type int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @param annotationType type of annotation
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @type AnnotationType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @param hasTypeAnnotation flag indicating the presence of a type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 annotation (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 @param has3107Annotation flag indicating the presence of a PEP 3107
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 annotation (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 @param hasTypeComment flag indicating the presence of a type comment
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.argname = argname
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.lineno = lineno
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.col_offset = col_offset
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.annotationType = annotationType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 self.hasTypeAnnotation = hasTypeAnnotation
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.has3107Annotation = has3107Annotation
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 self.hasTypeComment = hasTypeComment
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
67
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 @classmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 def fromNode(cls, node, annotationTypeName):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 Class method to create an Argument object based on the given node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
72
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 @param node reference to the node to be converted
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 @type ast.arguments
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 @param annotationTypeName name of the annotation type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 @type str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 @return Argument object
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @rtype Argument
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 annotationType = AnnotationType[annotationTypeName]
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 newArg = cls(node.arg, node.lineno, node.col_offset, annotationType)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 newArg.hasTypeAnnotation = False
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 if node.annotation:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 newArg.hasTypeAnnotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 newArg.has3107Annotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 if node.type_comment:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 newArg.hasTypeAnnotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 newArg.hasTypeComment = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 return newArg
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 class Function:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 Class representing a function.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
99
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
100 def __init__(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
101 self,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
102 name,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
103 lineno,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
104 col_offset,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
105 functionType=FunctionType.PUBLIC,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
106 isClassMethod=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
107 classDecoratorType=None,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
108 isReturnAnnotated=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
109 hasTypeComment=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
110 hasOnlyNoneReturns=True,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
111 isNested=False,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
112 decoratorList=None,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
113 args=None,
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
114 ):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
117
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 @param name name of the function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 @type str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 @param lineno line number
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 @type int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @param col_offset column number
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 @type int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 @param functionType type of the function (defaults to
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 FunctionType.PUBLIC)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 @type FunctionType (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 @param isClassMethod flag indicating a class method (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 @param classDecoratorType type of a function decorator
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 (defaults to None)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 @type ClassDecoratorType or None (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 @param isReturnAnnotated flag indicating the presence of a return
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 type annotation (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 @param hasTypeComment flag indicating the presence of a type comment
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @param hasOnlyNoneReturns flag indicating only None return values
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 (defaults to True)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 @param isNested flag indicating a nested function (defaults to False)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @type bool (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 @param decoratorList list of decorator nodes (defaults to None)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @type list of ast.Attribute, ast.Call or ast.Name (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 @param args list of arguments (defaults to None)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 @type list of Argument (optional)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.name = name
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 self.lineno = lineno
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 self.col_offset = col_offset
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.functionType = functionType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.isClassMethod = isClassMethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 self.classDecoratorType = classDecoratorType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 self.isReturnAnnotated = isReturnAnnotated
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.hasTypeComment = hasTypeComment
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.hasOnlyNoneReturns = hasOnlyNoneReturns
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 self.isNested = isNested
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 self.decoratorList = decoratorList
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 self.args = args
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
160
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 def isFullyAnnotated(self):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 Public method to check, if the function definition is fully type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 annotated.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 Note: self.args will always include an Argument object for return.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
167
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 @return flag indicating a fully annotated function definition
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 @rtype bool
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 return all(arg.hasTypeAnnotation for arg in self.args)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
172
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 def isDynamicallyTyped(self):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 Public method to check, if a function definition is dynamically typed
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 (i.e. completely lacking hints).
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
177
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @return flag indicating a dynamically typed function definition
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 @rtype bool
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 return not any(arg.hasTypeAnnotation for arg in self.args)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 def getMissedAnnotations(self):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 Public method to provide a list of arguments with missing type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 annotations.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
187
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 @return list of arguments with missing type annotations
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @rtype list of Argument
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 return [arg for arg in self.args if not arg.hasTypeAnnotation]
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 def getAnnotatedArguments(self):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 Public method to get list of arguments with type annotations.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
196
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 @return list of arguments with type annotations.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 @rtype list of Argument
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 return [arg for arg in self.args if arg.hasTypeAnnotation]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
201
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 def hasDecorator(self, checkDecorators):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 Public method to check whether the function node is decorated by any of
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 the provided decorators.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
206
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 Decorator matching is done against the provided `checkDecorators` set.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 Decorators are assumed to be either a module attribute (e.g.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 `@typing.overload`) or name (e.g. `@overload`). For the case of a
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 module attribute, only the attribute is checked against
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 `overload_decorators`.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
212
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 Note: Deeper decorator imports (e.g. `a.b.overload`) are not explicitly
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 supported.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
215
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 @param checkDecorators set of decorators to check against
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 @type set of str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 @return flag indicating the presence of any decorators
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 @rtype bool
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 for decorator in self.decoratorList:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 # Drop to a helper to allow for simpler handling of callable
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 # decorators
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 return self.__decoratorChecker(decorator, checkDecorators)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 else:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
227
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 def __decoratorChecker(self, decorator, checkDecorators):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 Private method to check the provided decorator for a match against the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 provided set of check names.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
232
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 Decorators are assumed to be of the following form:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 * `a.name` or `a.name()`
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 * `name` or `name()`
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
236
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 Note: Deeper imports (e.g. `a.b.name`) are not explicitly supported.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
238
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 @param decorator decorator node to check
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 @type ast.Attribute, ast.Call or ast.Name
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 @param checkDecorators set of decorators to check against
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 @type set of str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 @return flag indicating the presence of any decorators
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 @rtype bool
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 if isinstance(decorator, ast.Name):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 # e.g. `@overload`, where `decorator.id` will be the name
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 if decorator.id in checkDecorators:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 return True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 elif isinstance(decorator, ast.Attribute):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 # e.g. `@typing.overload`, where `decorator.attr` will be the name
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 if decorator.attr in checkDecorators:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 return True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 elif isinstance(decorator, ast.Call):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 # e.g. `@overload()` or `@typing.overload()`, where
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 # `decorator.func` will be `ast.Name` or `ast.Attribute`,
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 # which we can check recursively
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 return self.__decoratorChecker(decorator.func, checkDecorators)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
259
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 return None
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
261
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 @classmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 def fromNode(cls, node, lines, **kwargs):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 Class method to create a Function object from ast.FunctionDef or
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 ast.AsyncFunctionDef nodes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
267
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 Accept the source code, as a list of strings, in order to get the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 column where the function definition ends.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
270
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 With exceptions, input kwargs are passed straight through to Function's
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 __init__. The following kwargs will be overridden:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 * function_type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 * class_decorator_type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 * args
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
276
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 @param node reference to the function definition node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 @param lines list of source code lines
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 @type list of str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 @keyparam **kwargs keyword arguments
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 @type dict
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 @return created Function object
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 @rtype Function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 # Extract function types from function name
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 kwargs["functionType"] = cls.getFunctionType(node.name)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
288
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 # Identify type of class method, if applicable
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 if kwargs.get("isClassMethod", False):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 kwargs["classDecoratorType"] = cls.getClassDecoratorType(node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
292
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 # Store raw decorator list for use by property methods
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 kwargs["decoratorList"] = node.decorator_list
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
295
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
296 # Instantiate empty args list here since it has no default
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
297 kwargs["args"] = []
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
298
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 newFunction = cls(node.name, node.lineno, node.col_offset, **kwargs)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
300
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 # Iterate over arguments by type & add
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 for argType in AST_ARG_TYPES:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 args = node.args.__getattribute__(argType)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 if args:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 if not isinstance(args, list):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 args = [args]
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
307
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 newFunction.args.extend(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
309 [Argument.fromNode(arg, argType.upper()) for arg in args]
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
311
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 # Create an Argument object for the return hint
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 defEndLineno, defEndColOffset = cls.colonSeeker(node, lines)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
314 returnArg = Argument(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
315 "return", defEndLineno, defEndColOffset, AnnotationType.RETURN
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
316 )
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 if node.returns:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 returnArg.hasTypeAnnotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 returnArg.has3107Annotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 newFunction.isReturnAnnotated = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
321
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 newFunction.args.append(returnArg)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
323
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 # Type comments in-line with input arguments are handled by the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 # Argument class. If a function-level type comment is present, attempt
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 # to parse for any missed type hints.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 if node.type_comment:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 newFunction.hasTypeComment = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 newFunction = cls.tryTypeComment(newFunction, node)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
330
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 # Check for the presence of non-`None` returns using the special-case
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 # return node visitor.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 returnVisitor = ReturnVisitor(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 returnVisitor.visit(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 newFunction.hasOnlyNoneReturns = returnVisitor.hasOnlyNoneReturns
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
336
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 return newFunction
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
338
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 def colonSeeker(node, lines):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 Static method to find the line & column indices of the function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 definition's closing colon.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
344
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 @param node reference to the function definition node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 @param lines list of source code lines
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 @type list of str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 @return line and column offset of the colon
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 @rtype tuple of (int, int)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 # Special case single line function definitions
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 if node.lineno == node.body[0].lineno:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
354 return Function._singleLineColonSeeker(node, lines[node.lineno - 1])
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
355
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
356 # With Python < 3.8, the function node includes the docstring and the
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
357 # body does not, so we have to rewind through any docstrings, if
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
358 # present, before looking for the def colon. We should end up with
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
359 # lines[defEndLineno - 1] having the colon.
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
360 defEndLineno = node.body[0].lineno
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
361 if sys.version_info < (3, 8, 0):
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
362 # If the docstring is on one line then no rewinding is necessary.
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
363 nTripleQuotes = lines[defEndLineno - 1].count('"""')
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
364 if nTripleQuotes == 1:
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
365 # Docstring closure, rewind until the opening is found and take
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
366 # the line prior.
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
367 while True:
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
368 defEndLineno -= 1
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
369 if '"""' in lines[defEndLineno - 1]:
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
370 # Docstring has closed
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
371 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
372
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
373 # Once we've gotten here, we've found the line where the docstring
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
374 # begins, so we have to step up one more line to get to the close of
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
375 # the def.
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
376 defEndLineno -= 1
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
377
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 # Use str.rfind() to account for annotations on the same line,
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 # definition closure should be the last : on the line
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 defEndColOffset = lines[defEndLineno - 1].rfind(":")
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
381
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 return defEndLineno, defEndColOffset
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
383
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 def _singleLineColonSeeker(node, line):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 Static method to find the line & column indices of a single line
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 function definition.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
389
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 @param node reference to the function definition node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 @param line source code line
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 @type str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 @return line and column offset of the colon
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 @rtype tuple of (int, int)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 colStart = node.col_offset
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 colEnd = node.body[0].col_offset
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 defEndColOffset = line.rfind(":", colStart, colEnd)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
400
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 return node.lineno, defEndColOffset
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
402
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 def tryTypeComment(funcObj, node):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 Static method to infer type hints from a function-level type comment.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
407
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 If a function is type commented it is assumed to have a return
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 annotation, otherwise Python will fail to parse the hint.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
410
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 @param funcObj reference to the Function object
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 @type Function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 @param node reference to the function definition node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 @return reference to the modified Function object
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 @rtype Function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 hintTree = ast.parse(node.type_comment, "<func_type>", "func_type")
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 hintTree = Function._maybeInjectClassArgument(hintTree, funcObj)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
420
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
421 for arg, hintComment in itertools.zip_longest(funcObj.args, hintTree.argtypes):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 if isinstance(hintComment, ast.Ellipsis):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 continue
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
424
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 if arg and hintComment:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 arg.hasTypeAnnotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 arg.hasTypeComment = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
428
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 # Return arg is always last
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 funcObj.args[-1].hasTypeAnnotation = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 funcObj.args[-1].hasTypeComment = True
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 funcObj.isReturnAnnotated = True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
433
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 return funcObj
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
435
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 def _maybeInjectClassArgument(hintTree, funcObj):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 Static method to inject `self` or `cls` args into a type comment to
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 align with PEP 3107-style annotations.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
441
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 Because PEP 484 does not describe a method to provide partial function-
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 level type comments, there is a potential for ambiguity in the context
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 of both class methods and classmethods when aligning type comments to
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 method arguments.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
446
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 These two class methods, for example, should lint equivalently:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
448
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 def bar(self, a):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 # type: (int) -> int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 ...
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
452
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 def bar(self, a: int) -> int
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 ...
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
455
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 When this example type comment is parsed by `ast` and then matched with
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 the method's arguments, it associates the `int` hint to `self` rather
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 than `a`, so a dummy hint needs to be provided in situations where
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 `self` or `class` are not hinted in the type comment in order to
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 achieve equivalent linting results to PEP-3107 style annotations.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
461
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 A dummy `ast.Ellipses` constant is injected if the following criteria
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 are met:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 1. The function node is either a class method or classmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 2. The number of hinted args is at least 1 less than the number
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 of function args
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
467
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 @param hintTree parsed type hint node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 @type ast.FunctionType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 @param funcObj reference to the Function object
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 @type Function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 @return reference to the hint node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 @rtype ast.FunctionType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 if not funcObj.isClassMethod:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 # Short circuit
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 return hintTree
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
479 if funcObj.classDecoratorType != ClassDecoratorType.STATICMETHOD and len(
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
480 hintTree.argtypes
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
481 ) < (len(funcObj.args) - 1):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 # Subtract 1 to skip return arg
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 hintTree.argtypes = [ast.Ellipsis()] + hintTree.argtypes
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
484
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 return hintTree
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
486
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 def getFunctionType(functionName):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 Static method to determine the function's FunctionType from its name.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 MethodType is determined by the following priority:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 1. Special: function name prefixed & suffixed by "__"
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 2. Private: function name prefixed by "__"
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 3. Protected: function name prefixed by "_"
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 4. Public: everything else
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
497
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 @param functionName function name to be checked
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 @type str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 @return type of function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 @rtype FunctionType
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 if functionName.startswith("__") and functionName.endswith("__"):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 return FunctionType.SPECIAL
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 elif functionName.startswith("__"):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 return FunctionType.PRIVATE
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 elif functionName.startswith("_"):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 return FunctionType.PROTECTED
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 else:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 return FunctionType.PUBLIC
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
511
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 @staticmethod
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 def getClassDecoratorType(functionNode):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 Static method to get the class method's decorator type from its
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 function node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
517
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 Only @classmethod and @staticmethod decorators are identified; all
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 other decorators are ignored
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
520
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 If @classmethod or @staticmethod decorators are not present, this
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 function will return None.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
523
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 @param functionNode reference to the function definition node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 @return class decorator type
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 @rtype ClassDecoratorType or None
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 # @classmethod and @staticmethod will show up as ast.Name objects,
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 # where callable decorators will show up as ast.Call, which we can
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 # ignore
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 decorators = [
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 decorator.id
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 for decorator in functionNode.decorator_list
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 if isinstance(decorator, ast.Name)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 ]
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 if "classmethod" in decorators:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 return ClassDecoratorType.CLASSMETHOD
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 elif "staticmethod" in decorators:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 return ClassDecoratorType.STATICMETHOD
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 else:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 return None
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 class FunctionVisitor(ast.NodeVisitor):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 Class implementing a node visitor to check function annotations.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
550
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
551 AstFuncTypes = (ast.FunctionDef, ast.AsyncFunctionDef)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
552
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 def __init__(self, lines):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
556
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 @param lines source code lines of the function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 @type list of str
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 self.lines = lines
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 self.functionDefinitions = []
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 self.__context = []
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
563
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 def switchContext(self, node):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 Public method implementing a context switcher as a generic function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 visitor in order to track function context.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
568
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 Without keeping track of context, it's challenging to reliably
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 differentiate class methods from "regular" functions, especially in the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 case of nested classes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
572
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 @param node reference to the function definition node to be analyzed
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 """
8773
3dd81b827455 Extended the annotations checker to check for missing 'from __future__ import annotations'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
576 if isinstance(node, FunctionVisitor.AstFuncTypes):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 # Check for non-empty context first to prevent IndexErrors for
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 # non-nested nodes
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 if self.__context:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 if isinstance(self.__context[-1], ast.ClassDef):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 # Check if current context is a ClassDef node & pass the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 # appropriate flag
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 self.functionDefinitions.append(
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 Function.fromNode(node, self.lines, isClassMethod=True)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
586 elif isinstance(self.__context[-1], FunctionVisitor.AstFuncTypes):
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 # Check for nested function & pass the appropriate flag
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 self.functionDefinitions.append(
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 Function.fromNode(node, self.lines, isNested=True)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 )
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 else:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
592 self.functionDefinitions.append(Function.fromNode(node, self.lines))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
593
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 self.__context.append(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 self.generic_visit(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 self.__context.pop()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
597
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 visit_FunctionDef = switchContext
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 visit_AsyncFunctionDef = switchContext
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 visit_ClassDef = switchContext
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 class ReturnVisitor(ast.NodeVisitor):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 Class implementing a node visitor to check the return statements of a
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 function node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
607
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 If the function node being visited has an explicit return statement of
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 anything other than `None`, the `instance.hasOnlyNoneReturns` flag will
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 be set to `False`.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
611
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 If the function node being visited has no return statement, or contains
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 only return statement(s) that explicitly return `None`, the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 `instance.hasOnlyNoneReturns` flag will be set to `True`.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
615
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 Due to the generic visiting being done, we need to keep track of the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 context in which a non-`None` return node is found. These functions are
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 added to a set that is checked to see whether nor not the parent node is
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 present.
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
621
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 def __init__(self, parentNode):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
625
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 @param parentNode reference to the function definition node to be
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 analyzed
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 self.parentNode = parentNode
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 self.__context = []
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 self.__nonNoneReturnNodes = set()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
633
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 @property
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 def hasOnlyNoneReturns(self):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 Public method indicating, that the parent node isn't in the visited
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 nodes that don't return `None`.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
639
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 @return flag indicating, that the parent node isn't in the visited
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 nodes that don't return `None`
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 @rtype bool
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 return self.parentNode not in self.__nonNoneReturnNodes
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
645
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 def visit_Return(self, node):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 Public method to check each Return node to see if it returns anything
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 other than `None`.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
650
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 If the node being visited returns anything other than `None`, its
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 parent context is added to the set of non-returning child nodes of
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 the parent node.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
654
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 @param node reference to the AST Return node
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 @type ast.Return
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 if node.value is not None:
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 # In the event of an explicit `None` return (`return None`), the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 # node body will be an instance of either `ast.Constant` (3.8+) or
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 # `ast.NameConstant`, which we need to check to see if it's
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 # actually `None`
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 if (
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
664 isinstance(node.value, (ast.Constant, ast.NameConstant))
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
665 and node.value.value is None
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 ):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 return
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
668
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 self.__nonNoneReturnNodes.add(self.__context[-1])
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 def switchContext(self, node):
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 Public method implementing a context switcher as a generic function
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 visitor in order to track function context.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
675
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 Without keeping track of context, it's challenging to reliably
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 differentiate class methods from "regular" functions, especially in the
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 case of nested classes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
679
8244
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 @param node reference to the function definition node to be analyzed
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 @type ast.AsyncFunctionDef or ast.FunctionDef
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 """
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 self.__context.append(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 self.generic_visit(node)
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 self.__context.pop()
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 visit_FunctionDef = switchContext
ed8cb108b27b Code Style Checker: reworked the type annotations checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 visit_AsyncFunctionDef = switchContext

eric ide

mercurial