@@ -32,11 +32,15 @@ def _extract_argument_name(expr: Expression) -> Optional[str]:
3232
3333def expr_to_unanalyzed_type (expr : Expression ,
3434 options : Optional [Options ] = None ,
35+ allow_new_syntax : bool = False ,
3536 _parent : Optional [Expression ] = None ) -> ProperType :
3637 """Translate an expression to the corresponding type.
3738
3839 The result is not semantically analyzed. It can be UnboundType or TypeList.
3940 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).
4044 """
4145 # The `parent` parameter is used in recursive calls to provide context for
4246 # understanding whether an CallableArgument is ok.
@@ -56,7 +60,7 @@ def expr_to_unanalyzed_type(expr: Expression,
5660 else :
5761 raise TypeTranslationError ()
5862 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 )
6064 if isinstance (base , UnboundType ):
6165 if base .args :
6266 raise TypeTranslationError ()
@@ -72,20 +76,20 @@ def expr_to_unanalyzed_type(expr: Expression,
7276 # of the Annotation definition and only returning the type information,
7377 # losing all the annotations.
7478
75- return expr_to_unanalyzed_type (args [0 ], options , expr )
79+ return expr_to_unanalyzed_type (args [0 ], options , allow_new_syntax , expr )
7680 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 )
7883 if not base .args :
7984 base .empty_tuple_index = True
8085 return base
8186 else :
8287 raise TypeTranslationError ()
8388 elif (isinstance (expr , OpExpr )
8489 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 )])
8993 elif isinstance (expr , CallExpr ) and isinstance (_parent , ListExpr ):
9094 c = expr .callee
9195 names = []
@@ -118,19 +122,20 @@ def expr_to_unanalyzed_type(expr: Expression,
118122 if typ is not default_type :
119123 # Two types
120124 raise TypeTranslationError ()
121- typ = expr_to_unanalyzed_type (arg , options , expr )
125+ typ = expr_to_unanalyzed_type (arg , options , allow_new_syntax , expr )
122126 continue
123127 else :
124128 raise TypeTranslationError ()
125129 elif i == 0 :
126- typ = expr_to_unanalyzed_type (arg , options , expr )
130+ typ = expr_to_unanalyzed_type (arg , options , allow_new_syntax , expr )
127131 elif i == 1 :
128132 name = _extract_argument_name (arg )
129133 else :
130134 raise TypeTranslationError ()
131135 return CallableArgument (typ , name , arg_const , expr .line , expr .column )
132136 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 ],
134139 line = expr .line , column = expr .column )
135140 elif isinstance (expr , StrExpr ):
136141 return parse_type_string (expr .value , 'builtins.str' , expr .line , expr .column ,
@@ -142,7 +147,7 @@ def expr_to_unanalyzed_type(expr: Expression,
142147 return parse_type_string (expr .value , 'builtins.unicode' , expr .line , expr .column ,
143148 assume_str_is_unicode = True )
144149 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 )
146151 if isinstance (typ , RawExpressionType ):
147152 if isinstance (typ .literal_value , int ) and expr .op == '-' :
148153 typ .literal_value *= - 1
0 commit comments