5
5
import ast
6
6
import sys
7
7
import types
8
- from collections import namedtuple
9
8
from functools import partial
10
- from typing import Dict , Optional
9
+ from typing import Dict , List , NamedTuple , Optional , Type
11
10
12
11
from astroid .const import PY38_PLUS , Context
13
12
20
19
except ImportError :
21
20
_ast_py3 = None
22
21
23
- FunctionType = namedtuple ("FunctionType" , ["argtypes" , "returns" ])
24
22
23
+ class FunctionType (NamedTuple ):
24
+ argtypes : List [ast .expr ]
25
+ returns : ast .expr
25
26
26
- class ParserModule (
27
- namedtuple (
28
- "ParserModule" ,
29
- [
30
- "module" ,
31
- "unary_op_classes" ,
32
- "cmp_op_classes" ,
33
- "bool_op_classes" ,
34
- "bin_op_classes" ,
35
- "context_classes" ,
36
- ],
37
- )
38
- ):
39
- def parse (self , string : str , type_comments = True ):
27
+
28
+ class ParserModule (NamedTuple ):
29
+ module : types .ModuleType
30
+ unary_op_classes : Dict [Type [ast .unaryop ], str ]
31
+ cmp_op_classes : Dict [Type [ast .cmpop ], str ]
32
+ bool_op_classes : Dict [Type [ast .boolop ], str ]
33
+ bin_op_classes : Dict [Type [ast .operator ], str ]
34
+ context_classes : Dict [Type [ast .expr_context ], Context ]
35
+
36
+ def parse (self , string : str , type_comments : bool = True ) -> ast .Module :
40
37
if self .module is _ast_py3 :
41
38
if PY38_PLUS :
42
39
parse_func = partial (self .module .parse , type_comments = type_comments )
@@ -58,7 +55,7 @@ def parse_function_type_comment(type_comment: str) -> Optional[FunctionType]:
58
55
return FunctionType (argtypes = func_type .argtypes , returns = func_type .returns )
59
56
60
57
61
- def get_parser_module (type_comments = True ) -> ParserModule :
58
+ def get_parser_module (type_comments : bool = True ) -> ParserModule :
62
59
parser_module = ast
63
60
if type_comments and _ast_py3 :
64
61
parser_module = _ast_py3
@@ -79,11 +76,15 @@ def get_parser_module(type_comments=True) -> ParserModule:
79
76
)
80
77
81
78
82
- def _unary_operators_from_module (module ):
79
+ def _unary_operators_from_module (
80
+ module : types .ModuleType ,
81
+ ) -> Dict [Type [ast .unaryop ], str ]:
83
82
return {module .UAdd : "+" , module .USub : "-" , module .Not : "not" , module .Invert : "~" }
84
83
85
84
86
- def _binary_operators_from_module (module ):
85
+ def _binary_operators_from_module (
86
+ module : types .ModuleType ,
87
+ ) -> Dict [Type [ast .operator ], str ]:
87
88
binary_operators = {
88
89
module .Add : "+" ,
89
90
module .BitAnd : "&" ,
@@ -102,11 +103,15 @@ def _binary_operators_from_module(module):
102
103
return binary_operators
103
104
104
105
105
- def _bool_operators_from_module (module ):
106
+ def _bool_operators_from_module (
107
+ module : types .ModuleType ,
108
+ ) -> Dict [Type [ast .boolop ], str ]:
106
109
return {module .And : "and" , module .Or : "or" }
107
110
108
111
109
- def _compare_operators_from_module (module ):
112
+ def _compare_operators_from_module (
113
+ module : types .ModuleType ,
114
+ ) -> Dict [Type [ast .cmpop ], str ]:
110
115
return {
111
116
module .Eq : "==" ,
112
117
module .Gt : ">" ,
@@ -121,7 +126,9 @@ def _compare_operators_from_module(module):
121
126
}
122
127
123
128
124
- def _contexts_from_module (module ) -> Dict [ast .expr_context , Context ]:
129
+ def _contexts_from_module (
130
+ module : types .ModuleType ,
131
+ ) -> Dict [Type [ast .expr_context ], Context ]:
125
132
return {
126
133
module .Load : Context .Load ,
127
134
module .Store : Context .Store ,
0 commit comments