Skip to content

Commit

Permalink
Fix line numbers for variables bound by except (python#8628)
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored Apr 3, 2020
1 parent d28b396 commit d54fc8e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def visit(self, node: Optional[AST]) -> Any:
self.visitor_cache[typeobj] = visitor
return visitor(node)

def set_line(self, node: N, n: Union[ast3.expr, ast3.stmt]) -> N:
def set_line(self, node: N, n: Union[ast3.expr, ast3.stmt, ast3.ExceptHandler]) -> N:
node.line = n.lineno
node.column = n.col_offset
node.end_line = getattr(n, "end_lineno", None) if isinstance(n, ast3.expr) else None
Expand Down Expand Up @@ -838,7 +838,9 @@ def visit_Raise(self, n: ast3.Raise) -> RaiseStmt:

# Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
def visit_Try(self, n: ast3.Try) -> TryStmt:
vs = [NameExpr(h.name) if h.name is not None else None for h in n.handlers]
vs = [
self.set_line(NameExpr(h.name), h) if h.name is not None else None for h in n.handlers
]
types = [self.visit(h.type) for h in n.handlers]
handlers = [self.as_required_block(h.body, h.lineno) for h in n.handlers]

Expand Down
4 changes: 2 additions & 2 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def visit(self, node: Optional[AST]) -> Any: # same as in typed_ast stub
self.visitor_cache[typeobj] = visitor
return visitor(node)

def set_line(self, node: N, n: Union[ast27.expr, ast27.stmt]) -> N:
def set_line(self, node: N, n: Union[ast27.expr, ast27.stmt, ast27.ExceptHandler]) -> N:
node.line = n.lineno
node.column = n.col_offset
return node
Expand Down Expand Up @@ -694,7 +694,7 @@ def try_handler(self,
if item.name is None:
vs.append(None)
elif isinstance(item.name, Name):
vs.append(NameExpr(item.name.id))
vs.append(self.set_line(NameExpr(item.name.id), item))
else:
self.fail("Sorry, `except <expr>, <anything but a name>` is not supported",
item.lineno, item.col_offset)
Expand Down

0 comments on commit d54fc8e

Please sign in to comment.