44
55from mypy .nodes import (
66 Expression , NameExpr , MemberExpr , IndexExpr , RefExpr , TupleExpr , IntExpr , FloatExpr , UnaryExpr ,
7- ComplexExpr , ListExpr , StrExpr , BytesExpr , UnicodeExpr , EllipsisExpr , CallExpr ,
7+ ComplexExpr , ListExpr , StrExpr , BytesExpr , UnicodeExpr , EllipsisExpr , CallExpr , OpExpr ,
88 get_member_expr_fullname
99)
1010from mypy .fastparse import parse_type_string
1111from mypy .types import (
1212 Type , UnboundType , TypeList , EllipsisType , AnyType , CallableArgument , TypeOfAny ,
13- RawExpressionType , ProperType
13+ RawExpressionType , ProperType , UnionType
1414)
15+ from mypy .options import Options
1516
1617
1718class TypeTranslationError (Exception ):
@@ -29,7 +30,9 @@ def _extract_argument_name(expr: Expression) -> Optional[str]:
2930 raise TypeTranslationError ()
3031
3132
32- def expr_to_unanalyzed_type (expr : Expression , _parent : Optional [Expression ] = None ) -> ProperType :
33+ def expr_to_unanalyzed_type (expr : Expression ,
34+ options : Optional [Options ] = None ,
35+ _parent : Optional [Expression ] = None ) -> ProperType :
3336 """Translate an expression to the corresponding type.
3437
3538 The result is not semantically analyzed. It can be UnboundType or TypeList.
@@ -53,7 +56,7 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No
5356 else :
5457 raise TypeTranslationError ()
5558 elif isinstance (expr , IndexExpr ):
56- base = expr_to_unanalyzed_type (expr .base , expr )
59+ base = expr_to_unanalyzed_type (expr .base , options , expr )
5760 if isinstance (base , UnboundType ):
5861 if base .args :
5962 raise TypeTranslationError ()
@@ -69,14 +72,20 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No
6972 # of the Annotation definition and only returning the type information,
7073 # losing all the annotations.
7174
72- return expr_to_unanalyzed_type (args [0 ], expr )
75+ return expr_to_unanalyzed_type (args [0 ], options , expr )
7376 else :
74- base .args = tuple (expr_to_unanalyzed_type (arg , expr ) for arg in args )
77+ base .args = tuple (expr_to_unanalyzed_type (arg , options , expr ) for arg in args )
7578 if not base .args :
7679 base .empty_tuple_index = True
7780 return base
7881 else :
7982 raise TypeTranslationError ()
83+ elif (isinstance (expr , OpExpr )
84+ 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 )])
8089 elif isinstance (expr , CallExpr ) and isinstance (_parent , ListExpr ):
8190 c = expr .callee
8291 names = []
@@ -109,19 +118,19 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No
109118 if typ is not default_type :
110119 # Two types
111120 raise TypeTranslationError ()
112- typ = expr_to_unanalyzed_type (arg , expr )
121+ typ = expr_to_unanalyzed_type (arg , options , expr )
113122 continue
114123 else :
115124 raise TypeTranslationError ()
116125 elif i == 0 :
117- typ = expr_to_unanalyzed_type (arg , expr )
126+ typ = expr_to_unanalyzed_type (arg , options , expr )
118127 elif i == 1 :
119128 name = _extract_argument_name (arg )
120129 else :
121130 raise TypeTranslationError ()
122131 return CallableArgument (typ , name , arg_const , expr .line , expr .column )
123132 elif isinstance (expr , ListExpr ):
124- return TypeList ([expr_to_unanalyzed_type (t , expr ) for t in expr .items ],
133+ return TypeList ([expr_to_unanalyzed_type (t , options , expr ) for t in expr .items ],
125134 line = expr .line , column = expr .column )
126135 elif isinstance (expr , StrExpr ):
127136 return parse_type_string (expr .value , 'builtins.str' , expr .line , expr .column ,
@@ -133,7 +142,7 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No
133142 return parse_type_string (expr .value , 'builtins.unicode' , expr .line , expr .column ,
134143 assume_str_is_unicode = True )
135144 elif isinstance (expr , UnaryExpr ):
136- typ = expr_to_unanalyzed_type (expr .expr )
145+ typ = expr_to_unanalyzed_type (expr .expr , options )
137146 if isinstance (typ , RawExpressionType ):
138147 if isinstance (typ .literal_value , int ) and expr .op == '-' :
139148 typ .literal_value *= - 1
0 commit comments