File tree 1 file changed +11
-14
lines changed
1 file changed +11
-14
lines changed Original file line number Diff line number Diff line change 1
1
import ast
2
2
import sys
3
+ import types
3
4
from collections import namedtuple
4
5
from functools import partial
5
6
from typing import Dict , Optional
6
7
7
8
from astroid .const import PY38_PLUS , Context
8
9
9
- try :
10
- import typed_ast .ast3 as _ast_py3
11
- except ImportError :
12
- _ast_py3 = None
13
-
14
-
15
- if PY38_PLUS :
10
+ if sys .version_info >= (3 , 8 ):
16
11
# On Python 3.8, typed_ast was merged back into `ast`
17
- _ast_py3 = ast
18
-
12
+ _ast_py3 : Optional [types .ModuleType ] = ast
13
+ else :
14
+ try :
15
+ import typed_ast .ast3 as _ast_py3
16
+ except ImportError :
17
+ _ast_py3 = None
19
18
20
19
FunctionType = namedtuple ("FunctionType" , ["argtypes" , "returns" ])
21
20
@@ -51,16 +50,14 @@ def parse_function_type_comment(type_comment: str) -> Optional[FunctionType]:
51
50
if _ast_py3 is None :
52
51
return None
53
52
54
- func_type = _ast_py3 .parse (type_comment , "<type_comment>" , "func_type" )
53
+ func_type = _ast_py3 .parse (type_comment , "<type_comment>" , "func_type" ) # type: ignore[attr-defined]
55
54
return FunctionType (argtypes = func_type .argtypes , returns = func_type .returns )
56
55
57
56
58
57
def get_parser_module (type_comments = True ) -> ParserModule :
59
- if not type_comments :
60
- parser_module = ast
61
- else :
58
+ parser_module = ast
59
+ if type_comments and _ast_py3 :
62
60
parser_module = _ast_py3
63
- parser_module = parser_module or ast
64
61
65
62
unary_op_classes = _unary_operators_from_module (parser_module )
66
63
cmp_op_classes = _compare_operators_from_module (parser_module )
You can’t perform that action at this time.
0 commit comments