61
61
Expression as ast3_Expression ,
62
62
Str ,
63
63
Bytes ,
64
- Index ,
65
64
Num ,
66
65
UnaryOp ,
67
66
USub ,
@@ -89,7 +88,6 @@ def ast3_parse(source: Union[str, bytes], filename: str, mode: str,
89
88
Expression as ast3_Expression ,
90
89
Str ,
91
90
Bytes ,
92
- Index ,
93
91
Num ,
94
92
UnaryOp ,
95
93
USub ,
@@ -1200,6 +1198,7 @@ def visit_Attribute(self, n: Attribute) -> Union[MemberExpr, SuperExpr]:
1200
1198
1201
1199
# Subscript(expr value, slice slice, expr_context ctx)
1202
1200
def visit_Subscript (self , n : ast3 .Subscript ) -> IndexExpr :
1201
+ # Note this logic does the right thing for Python 3.9's AST changes to subscription
1203
1202
e = IndexExpr (self .visit (n .value ), self .visit (n .slice ))
1204
1203
self .set_line (e , n )
1205
1204
if isinstance (e .index , SliceExpr ):
@@ -1230,7 +1229,7 @@ def visit_List(self, n: ast3.List) -> Union[ListExpr, TupleExpr]:
1230
1229
1231
1230
# Tuple(expr* elts, expr_context ctx)
1232
1231
def visit_Tuple (self , n : ast3 .Tuple ) -> TupleExpr :
1233
- e = TupleExpr ([ self .visit ( e ) for e in n .elts ] )
1232
+ e = TupleExpr (self .translate_expr_list ( n .elts ) )
1234
1233
return self .set_line (e , n )
1235
1234
1236
1235
# --- slice ---
@@ -1246,7 +1245,7 @@ def visit_ExtSlice(self, n: ast3.ExtSlice) -> TupleExpr:
1246
1245
return TupleExpr (self .translate_expr_list (n .dims ))
1247
1246
1248
1247
# Index(expr value)
1249
- def visit_Index (self , n : Index ) -> Node :
1248
+ def visit_Index (self , n : ast3 . Index ) -> Node :
1250
1249
return self .visit (n .value )
1251
1250
1252
1251
@@ -1504,19 +1503,25 @@ def visit_Bytes(self, n: Bytes) -> Type:
1504
1503
1505
1504
# Subscript(expr value, slice slice, expr_context ctx)
1506
1505
def visit_Subscript (self , n : ast3 .Subscript ) -> Type :
1507
- if not isinstance (n .slice , Index ):
1508
- self .fail (TYPE_COMMENT_SYNTAX_ERROR , self .line , getattr (n , 'col_offset' , - 1 ))
1509
- return AnyType (TypeOfAny .from_error )
1510
-
1511
- empty_tuple_index = False
1512
- if isinstance (n .slice .value , ast3 .Tuple ):
1513
- params = self .translate_expr_list (n .slice .value .elts )
1514
- if len (n .slice .value .elts ) == 0 :
1515
- empty_tuple_index = True
1506
+ if sys .version_info >= (3 , 9 ):
1507
+ if isinstance (n .slice , ast3 .Slice ):
1508
+ self .fail (TYPE_COMMENT_SYNTAX_ERROR , self .line , getattr (n , 'col_offset' , - 1 ))
1509
+ return AnyType (TypeOfAny .from_error )
1510
+ slice = n .slice
1516
1511
else :
1517
- params = [self .visit (n .slice .value )]
1512
+ if not isinstance (n .slice , ast3 .Index ):
1513
+ self .fail (TYPE_COMMENT_SYNTAX_ERROR , self .line , getattr (n , 'col_offset' , - 1 ))
1514
+ return AnyType (TypeOfAny .from_error )
1515
+ slice = n .slice .value
1518
1516
1519
1517
value = self .visit (n .value )
1518
+ if isinstance (slice , ast3 .Tuple ):
1519
+ params = self .translate_expr_list (slice .elts )
1520
+ empty_tuple_index = len (slice .elts ) == 0
1521
+ else :
1522
+ params = [self .visit (slice )]
1523
+ empty_tuple_index = False
1524
+
1520
1525
if isinstance (value , UnboundType ) and not value .args :
1521
1526
return UnboundType (value .name , params , line = self .line , column = value .column ,
1522
1527
empty_tuple_index = empty_tuple_index )
0 commit comments