@@ -32,11 +32,15 @@ def _extract_argument_name(expr: Expression) -> Optional[str]:
32
32
33
33
def expr_to_unanalyzed_type (expr : Expression ,
34
34
options : Optional [Options ] = None ,
35
+ allow_new_syntax : bool = False ,
35
36
_parent : Optional [Expression ] = None ) -> ProperType :
36
37
"""Translate an expression to the corresponding type.
37
38
38
39
The result is not semantically analyzed. It can be UnboundType or TypeList.
39
40
Raise TypeTranslationError if the expression cannot represent a type.
41
+
42
+ If allow_new_syntax is True, allow all type syntax independent of the target
43
+ Python version (used in stubs).
40
44
"""
41
45
# The `parent` parameter is used in recursive calls to provide context for
42
46
# understanding whether an CallableArgument is ok.
@@ -56,7 +60,7 @@ def expr_to_unanalyzed_type(expr: Expression,
56
60
else :
57
61
raise TypeTranslationError ()
58
62
elif isinstance (expr , IndexExpr ):
59
- base = expr_to_unanalyzed_type (expr .base , options , expr )
63
+ base = expr_to_unanalyzed_type (expr .base , options , allow_new_syntax , expr )
60
64
if isinstance (base , UnboundType ):
61
65
if base .args :
62
66
raise TypeTranslationError ()
@@ -72,20 +76,20 @@ def expr_to_unanalyzed_type(expr: Expression,
72
76
# of the Annotation definition and only returning the type information,
73
77
# losing all the annotations.
74
78
75
- return expr_to_unanalyzed_type (args [0 ], options , expr )
79
+ return expr_to_unanalyzed_type (args [0 ], options , allow_new_syntax , expr )
76
80
else :
77
- base .args = tuple (expr_to_unanalyzed_type (arg , options , expr ) for arg in args )
81
+ base .args = tuple (expr_to_unanalyzed_type (arg , options , allow_new_syntax , expr )
82
+ for arg in args )
78
83
if not base .args :
79
84
base .empty_tuple_index = True
80
85
return base
81
86
else :
82
87
raise TypeTranslationError ()
83
88
elif (isinstance (expr , OpExpr )
84
89
and expr .op == '|'
85
- and options
86
- and options .python_version >= (3 , 10 )):
87
- return UnionType ([expr_to_unanalyzed_type (expr .left , options ),
88
- expr_to_unanalyzed_type (expr .right , options )])
90
+ and ((options and options .python_version >= (3 , 10 )) or allow_new_syntax )):
91
+ return UnionType ([expr_to_unanalyzed_type (expr .left , options , allow_new_syntax ),
92
+ expr_to_unanalyzed_type (expr .right , options , allow_new_syntax )])
89
93
elif isinstance (expr , CallExpr ) and isinstance (_parent , ListExpr ):
90
94
c = expr .callee
91
95
names = []
@@ -118,19 +122,20 @@ def expr_to_unanalyzed_type(expr: Expression,
118
122
if typ is not default_type :
119
123
# Two types
120
124
raise TypeTranslationError ()
121
- typ = expr_to_unanalyzed_type (arg , options , expr )
125
+ typ = expr_to_unanalyzed_type (arg , options , allow_new_syntax , expr )
122
126
continue
123
127
else :
124
128
raise TypeTranslationError ()
125
129
elif i == 0 :
126
- typ = expr_to_unanalyzed_type (arg , options , expr )
130
+ typ = expr_to_unanalyzed_type (arg , options , allow_new_syntax , expr )
127
131
elif i == 1 :
128
132
name = _extract_argument_name (arg )
129
133
else :
130
134
raise TypeTranslationError ()
131
135
return CallableArgument (typ , name , arg_const , expr .line , expr .column )
132
136
elif isinstance (expr , ListExpr ):
133
- return TypeList ([expr_to_unanalyzed_type (t , options , expr ) for t in expr .items ],
137
+ return TypeList ([expr_to_unanalyzed_type (t , options , allow_new_syntax , expr )
138
+ for t in expr .items ],
134
139
line = expr .line , column = expr .column )
135
140
elif isinstance (expr , StrExpr ):
136
141
return parse_type_string (expr .value , 'builtins.str' , expr .line , expr .column ,
@@ -142,7 +147,7 @@ def expr_to_unanalyzed_type(expr: Expression,
142
147
return parse_type_string (expr .value , 'builtins.unicode' , expr .line , expr .column ,
143
148
assume_str_is_unicode = True )
144
149
elif isinstance (expr , UnaryExpr ):
145
- typ = expr_to_unanalyzed_type (expr .expr , options )
150
+ typ = expr_to_unanalyzed_type (expr .expr , options , allow_new_syntax )
146
151
if isinstance (typ , RawExpressionType ):
147
152
if isinstance (typ .literal_value , int ) and expr .op == '-' :
148
153
typ .literal_value *= - 1
0 commit comments