Skip to content

Commit 3b5a62e

Browse files
authored
Fix typechecking on Python 3.8 (#7953)
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.
1 parent f86d08e commit 3b5a62e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mypy/fastparse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,16 @@ def set_line(self, node: N, n: Union[ast3.expr, ast3.stmt]) -> N:
321321
node.end_line = getattr(n, "end_lineno", None) if isinstance(n, ast3.expr) else None
322322
return node
323323

324-
def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]:
325-
res = [] # type: List[Expression]
324+
def translate_opt_expr_list(self, l: Sequence[Optional[AST]]) -> List[Optional[Expression]]:
325+
res = [] # type: List[Optional[Expression]]
326326
for e in l:
327327
exp = self.visit(e)
328328
res.append(exp)
329329
return res
330330

331+
def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]:
332+
return cast(List[Expression], self.translate_opt_expr_list(l))
333+
331334
def get_lineno(self, node: Union[ast3.expr, ast3.stmt]) -> int:
332335
if (isinstance(node, (ast3.AsyncFunctionDef, ast3.ClassDef, ast3.FunctionDef))
333336
and node.decorator_list):
@@ -988,7 +991,7 @@ def visit_IfExp(self, n: ast3.IfExp) -> ConditionalExpr:
988991

989992
# Dict(expr* keys, expr* values)
990993
def visit_Dict(self, n: ast3.Dict) -> DictExpr:
991-
e = DictExpr(list(zip(self.translate_expr_list(n.keys),
994+
e = DictExpr(list(zip(self.translate_opt_expr_list(n.keys),
992995
self.translate_expr_list(n.values))))
993996
return self.set_line(e, n)
994997

0 commit comments

Comments
 (0)