Skip to content

Commit 9c985ba

Browse files
author
hauntsaninja
committed
fastparse: fix for AST subscript changes in py39
1 parent d54fc8e commit 9c985ba

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

mypy/fastparse.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
Expression as ast3_Expression,
6262
Str,
6363
Bytes,
64-
Index,
6564
Num,
6665
UnaryOp,
6766
USub,
@@ -89,7 +88,6 @@ def ast3_parse(source: Union[str, bytes], filename: str, mode: str,
8988
Expression as ast3_Expression,
9089
Str,
9190
Bytes,
92-
Index,
9391
Num,
9492
UnaryOp,
9593
USub,
@@ -1200,6 +1198,7 @@ def visit_Attribute(self, n: Attribute) -> Union[MemberExpr, SuperExpr]:
12001198

12011199
# Subscript(expr value, slice slice, expr_context ctx)
12021200
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
12031202
e = IndexExpr(self.visit(n.value), self.visit(n.slice))
12041203
self.set_line(e, n)
12051204
if isinstance(e.index, SliceExpr):
@@ -1230,7 +1229,7 @@ def visit_List(self, n: ast3.List) -> Union[ListExpr, TupleExpr]:
12301229

12311230
# Tuple(expr* elts, expr_context ctx)
12321231
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))
12341233
return self.set_line(e, n)
12351234

12361235
# --- slice ---
@@ -1246,7 +1245,7 @@ def visit_ExtSlice(self, n: ast3.ExtSlice) -> TupleExpr:
12461245
return TupleExpr(self.translate_expr_list(n.dims))
12471246

12481247
# Index(expr value)
1249-
def visit_Index(self, n: Index) -> Node:
1248+
def visit_Index(self, n: ast3.Index) -> Node:
12501249
return self.visit(n.value)
12511250

12521251

@@ -1504,19 +1503,25 @@ def visit_Bytes(self, n: Bytes) -> Type:
15041503

15051504
# Subscript(expr value, slice slice, expr_context ctx)
15061505
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
15161511
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
15181516

15191517
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+
15201525
if isinstance(value, UnboundType) and not value.args:
15211526
return UnboundType(value.name, params, line=self.line, column=value.column,
15221527
empty_tuple_index=empty_tuple_index)

0 commit comments

Comments
 (0)