Skip to content

Commit 814f34d

Browse files
authored
Add typing to _ast (#1296)
1 parent dddf2da commit 814f34d

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

astroid/_ast.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import ast
22
import sys
3+
import types
34
from collections import namedtuple
45
from functools import partial
56
from typing import Dict, Optional
67

78
from astroid.const import PY38_PLUS, Context
89

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):
1611
# 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
1918

2019
FunctionType = namedtuple("FunctionType", ["argtypes", "returns"])
2120

@@ -51,16 +50,14 @@ def parse_function_type_comment(type_comment: str) -> Optional[FunctionType]:
5150
if _ast_py3 is None:
5251
return None
5352

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]
5554
return FunctionType(argtypes=func_type.argtypes, returns=func_type.returns)
5655

5756

5857
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:
6260
parser_module = _ast_py3
63-
parser_module = parser_module or ast
6461

6562
unary_op_classes = _unary_operators_from_module(parser_module)
6663
cmp_op_classes = _compare_operators_from_module(parser_module)

0 commit comments

Comments
 (0)