Skip to content

Commit

Permalink
Fix parser when using Python 3.9
Browse files Browse the repository at this point in the history
The structure of the AST for subscripts was changed in
python/cpython#9605

Fixes #8627
  • Loading branch information
gvanrossum committed Apr 23, 2020
1 parent 22c67da commit 43cb4eb
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import ast as ast3
assert 'kind' in ast3.Constant._fields, \
"This 3.8.0 alpha (%s) is too old; 3.8.0a3 required" % sys.version.split()[0]
# TODO: Num, Str, Bytes, NameConstant, Ellipsis are deprecated in 3.8.
# TODO: Index, ExtSlice are deprecated in 3.9.
from ast import (
AST,
Call,
Expand Down Expand Up @@ -1505,17 +1507,22 @@ def visit_Bytes(self, n: Bytes) -> Type:

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

empty_tuple_index = False
if isinstance(n.slice.value, ast3.Tuple):
params = self.translate_expr_list(n.slice.value.elts)
if len(n.slice.value.elts) == 0:
if isinstance(sliceval, ast3.Tuple):
params = self.translate_expr_list(sliceval.elts)
if len(sliceval.elts) == 0:
empty_tuple_index = True
else:
params = [self.visit(n.slice.value)]
params = [self.visit(sliceval)]

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

0 comments on commit 43cb4eb

Please sign in to comment.