2
2
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
3
3
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4
4
5
+ from __future__ import annotations
6
+
5
7
import ast
6
8
import sys
7
9
import types
8
10
from functools import partial
9
- from typing import Dict , List , NamedTuple , Optional , Type
11
+ from typing import NamedTuple
10
12
11
13
from astroid .const import PY38_PLUS , Context
12
14
13
15
if sys .version_info >= (3 , 8 ):
14
16
# On Python 3.8, typed_ast was merged back into `ast`
15
- _ast_py3 : Optional [ types .ModuleType ] = ast
17
+ _ast_py3 : types .ModuleType | None = ast
16
18
else :
17
19
try :
18
20
import typed_ast .ast3 as _ast_py3
21
23
22
24
23
25
class FunctionType (NamedTuple ):
24
- argtypes : List [ast .expr ]
26
+ argtypes : list [ast .expr ]
25
27
returns : ast .expr
26
28
27
29
28
30
class ParserModule (NamedTuple ):
29
31
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 ]
32
+ unary_op_classes : dict [ type [ast .unaryop ], str ]
33
+ cmp_op_classes : dict [ type [ast .cmpop ], str ]
34
+ bool_op_classes : dict [ type [ast .boolop ], str ]
35
+ bin_op_classes : dict [ type [ast .operator ], str ]
36
+ context_classes : dict [ type [ast .expr_context ], Context ]
35
37
36
38
def parse (self , string : str , type_comments : bool = True ) -> ast .Module :
37
39
if self .module is _ast_py3 :
@@ -46,7 +48,7 @@ def parse(self, string: str, type_comments: bool = True) -> ast.Module:
46
48
return parse_func (string )
47
49
48
50
49
- def parse_function_type_comment (type_comment : str ) -> Optional [ FunctionType ] :
51
+ def parse_function_type_comment (type_comment : str ) -> FunctionType | None :
50
52
"""Given a correct type comment, obtain a FunctionType object"""
51
53
if _ast_py3 is None :
52
54
return None
@@ -78,13 +80,13 @@ def get_parser_module(type_comments: bool = True) -> ParserModule:
78
80
79
81
def _unary_operators_from_module (
80
82
module : types .ModuleType ,
81
- ) -> Dict [ Type [ast .unaryop ], str ]:
83
+ ) -> dict [ type [ast .unaryop ], str ]:
82
84
return {module .UAdd : "+" , module .USub : "-" , module .Not : "not" , module .Invert : "~" }
83
85
84
86
85
87
def _binary_operators_from_module (
86
88
module : types .ModuleType ,
87
- ) -> Dict [ Type [ast .operator ], str ]:
89
+ ) -> dict [ type [ast .operator ], str ]:
88
90
binary_operators = {
89
91
module .Add : "+" ,
90
92
module .BitAnd : "&" ,
@@ -105,13 +107,13 @@ def _binary_operators_from_module(
105
107
106
108
def _bool_operators_from_module (
107
109
module : types .ModuleType ,
108
- ) -> Dict [ Type [ast .boolop ], str ]:
110
+ ) -> dict [ type [ast .boolop ], str ]:
109
111
return {module .And : "and" , module .Or : "or" }
110
112
111
113
112
114
def _compare_operators_from_module (
113
115
module : types .ModuleType ,
114
- ) -> Dict [ Type [ast .cmpop ], str ]:
116
+ ) -> dict [ type [ast .cmpop ], str ]:
115
117
return {
116
118
module .Eq : "==" ,
117
119
module .Gt : ">" ,
@@ -128,7 +130,7 @@ def _compare_operators_from_module(
128
130
129
131
def _contexts_from_module (
130
132
module : types .ModuleType ,
131
- ) -> Dict [ Type [ast .expr_context ], Context ]:
133
+ ) -> dict [ type [ast .expr_context ], Context ]:
132
134
return {
133
135
module .Load : Context .Load ,
134
136
module .Store : Context .Store ,
0 commit comments