Skip to content

Commit b6eb380

Browse files
authored
Add annotations for ParserModule (#1526)
1 parent 23cb69a commit b6eb380

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

astroid/_ast.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import ast
66
import sys
77
import types
8-
from collections import namedtuple
98
from functools import partial
10-
from typing import Dict, Optional
9+
from typing import Dict, List, NamedTuple, Optional, Type
1110

1211
from astroid.const import PY38_PLUS, Context
1312

@@ -20,23 +19,21 @@
2019
except ImportError:
2120
_ast_py3 = None
2221

23-
FunctionType = namedtuple("FunctionType", ["argtypes", "returns"])
2422

23+
class FunctionType(NamedTuple):
24+
argtypes: List[ast.expr]
25+
returns: ast.expr
2526

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:
4037
if self.module is _ast_py3:
4138
if PY38_PLUS:
4239
parse_func = partial(self.module.parse, type_comments=type_comments)
@@ -58,7 +55,7 @@ def parse_function_type_comment(type_comment: str) -> Optional[FunctionType]:
5855
return FunctionType(argtypes=func_type.argtypes, returns=func_type.returns)
5956

6057

61-
def get_parser_module(type_comments=True) -> ParserModule:
58+
def get_parser_module(type_comments: bool = True) -> ParserModule:
6259
parser_module = ast
6360
if type_comments and _ast_py3:
6461
parser_module = _ast_py3
@@ -79,11 +76,15 @@ def get_parser_module(type_comments=True) -> ParserModule:
7976
)
8077

8178

82-
def _unary_operators_from_module(module):
79+
def _unary_operators_from_module(
80+
module: types.ModuleType,
81+
) -> Dict[Type[ast.unaryop], str]:
8382
return {module.UAdd: "+", module.USub: "-", module.Not: "not", module.Invert: "~"}
8483

8584

86-
def _binary_operators_from_module(module):
85+
def _binary_operators_from_module(
86+
module: types.ModuleType,
87+
) -> Dict[Type[ast.operator], str]:
8788
binary_operators = {
8889
module.Add: "+",
8990
module.BitAnd: "&",
@@ -102,11 +103,15 @@ def _binary_operators_from_module(module):
102103
return binary_operators
103104

104105

105-
def _bool_operators_from_module(module):
106+
def _bool_operators_from_module(
107+
module: types.ModuleType,
108+
) -> Dict[Type[ast.boolop], str]:
106109
return {module.And: "and", module.Or: "or"}
107110

108111

109-
def _compare_operators_from_module(module):
112+
def _compare_operators_from_module(
113+
module: types.ModuleType,
114+
) -> Dict[Type[ast.cmpop], str]:
110115
return {
111116
module.Eq: "==",
112117
module.Gt: ">",
@@ -121,7 +126,9 @@ def _compare_operators_from_module(module):
121126
}
122127

123128

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]:
125132
return {
126133
module.Load: Context.Load,
127134
module.Store: Context.Store,

0 commit comments

Comments
 (0)