From a66762e8115838ef0da12cb236ce9b620eaf3455 Mon Sep 17 00:00:00 2001 From: "Michael J. Sullivan" Date: Thu, 14 Nov 2019 12:09:18 -0800 Subject: [PATCH] Fix typechecking on Python 3.8 The Python 3.8 ast stubs now (correctly!) indicate that a key in Dict can be None. Fix fastparse to deal with this. Really the typed_ast stubs should say this also. --- mypy/fastparse.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 402853bec113..812defbc1452 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -321,13 +321,16 @@ def set_line(self, node: N, n: Union[ast3.expr, ast3.stmt]) -> N: node.end_line = getattr(n, "end_lineno", None) if isinstance(n, ast3.expr) else None return node - def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]: - res = [] # type: List[Expression] + def translate_opt_expr_list(self, l: Sequence[Optional[AST]]) -> List[Optional[Expression]]: + res = [] # type: List[Optional[Expression]] for e in l: exp = self.visit(e) res.append(exp) return res + def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]: + return cast(List[Expression], self.translate_opt_expr_list(l)) + def get_lineno(self, node: Union[ast3.expr, ast3.stmt]) -> int: if (isinstance(node, (ast3.AsyncFunctionDef, ast3.ClassDef, ast3.FunctionDef)) and node.decorator_list): @@ -988,7 +991,7 @@ def visit_IfExp(self, n: ast3.IfExp) -> ConditionalExpr: # Dict(expr* keys, expr* values) def visit_Dict(self, n: ast3.Dict) -> DictExpr: - e = DictExpr(list(zip(self.translate_expr_list(n.keys), + e = DictExpr(list(zip(self.translate_opt_expr_list(n.keys), self.translate_expr_list(n.values)))) return self.set_line(e, n)