Skip to content

Commit 43cb4eb

Browse files
committed
Fix parser when using Python 3.9
The structure of the AST for subscripts was changed in python/cpython#9605 Fixes #8627
1 parent 22c67da commit 43cb4eb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

mypy/fastparse.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import ast as ast3
5050
assert 'kind' in ast3.Constant._fields, \
5151
"This 3.8.0 alpha (%s) is too old; 3.8.0a3 required" % sys.version.split()[0]
52+
# TODO: Num, Str, Bytes, NameConstant, Ellipsis are deprecated in 3.8.
53+
# TODO: Index, ExtSlice are deprecated in 3.9.
5254
from ast import (
5355
AST,
5456
Call,
@@ -1505,17 +1507,22 @@ def visit_Bytes(self, n: Bytes) -> Type:
15051507

15061508
# Subscript(expr value, slice slice, expr_context ctx)
15071509
def visit_Subscript(self, n: ast3.Subscript) -> Type:
1508-
if not isinstance(n.slice, Index):
1509-
self.fail(TYPE_COMMENT_SYNTAX_ERROR, self.line, getattr(n, 'col_offset', -1))
1510-
return AnyType(TypeOfAny.from_error)
1510+
if sys.version_info >= (3, 9): # Really 3.9a5 or later
1511+
sliceval = n.slice # type: Any
1512+
else:
1513+
# Python 3.8 or earlier use a different AST structure for subscripts
1514+
if not isinstance(n.slice, Index):
1515+
self.fail(TYPE_COMMENT_SYNTAX_ERROR, self.line, getattr(n, 'col_offset', -1))
1516+
return AnyType(TypeOfAny.from_error)
1517+
sliceval = n.slice.value
15111518

15121519
empty_tuple_index = False
1513-
if isinstance(n.slice.value, ast3.Tuple):
1514-
params = self.translate_expr_list(n.slice.value.elts)
1515-
if len(n.slice.value.elts) == 0:
1520+
if isinstance(sliceval, ast3.Tuple):
1521+
params = self.translate_expr_list(sliceval.elts)
1522+
if len(sliceval.elts) == 0:
15161523
empty_tuple_index = True
15171524
else:
1518-
params = [self.visit(n.slice.value)]
1525+
params = [self.visit(sliceval)]
15191526

15201527
value = self.visit(n.value)
15211528
if isinstance(value, UnboundType) and not value.args:

0 commit comments

Comments
 (0)