Skip to content

fastparse: add line and column information for extended slices #8743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,14 @@ def visit_Attribute(self, n: Attribute) -> Union[MemberExpr, SuperExpr]:
def visit_Subscript(self, n: ast3.Subscript) -> IndexExpr:
e = IndexExpr(self.visit(n.value), self.visit(n.slice))
self.set_line(e, n)
if isinstance(e.index, SliceExpr):
# Slice has no line/column in the raw ast.
if (
isinstance(n.slice, ast3.Slice) or
(sys.version_info < (3, 9) and isinstance(n.slice, ast3.ExtSlice))
):
# Before Python 3.9, Slice has no line/column in the raw ast. To avoid incompatibility
# visit_Slice doesn't set_line, even in Python 3.9 on.
# ExtSlice also has no line/column info. In Python 3.9 on, line/column is set for
# e.index when visiting n.slice.
e.index.line = e.line
e.index.column = e.column
return e
Expand All @@ -1233,7 +1239,7 @@ def visit_List(self, n: ast3.List) -> Union[ListExpr, TupleExpr]:

# Tuple(expr* elts, expr_context ctx)
def visit_Tuple(self, n: ast3.Tuple) -> TupleExpr:
e = TupleExpr([self.visit(e) for e in n.elts])
e = TupleExpr(self.translate_expr_list(n.elts))
return self.set_line(e, n)

# --- slice ---
Expand Down
44 changes: 22 additions & 22 deletions test-data/unit/parse.test
Original file line number Diff line number Diff line change
Expand Up @@ -3151,7 +3151,7 @@ MypyFile:1(
ExpressionStmt:1(
IndexExpr:1(
NameExpr(a)
TupleExpr:-1(
TupleExpr:1(
SliceExpr:-1(
<empty>
<empty>)
Expand All @@ -3166,7 +3166,7 @@ MypyFile:1(
ExpressionStmt:1(
IndexExpr:1(
NameExpr(a)
TupleExpr:-1(
TupleExpr:1(
SliceExpr:-1(
IntExpr(1)
IntExpr(2))
Expand All @@ -3181,7 +3181,7 @@ MypyFile:1(
ExpressionStmt:1(
IndexExpr:1(
NameExpr(a)
TupleExpr:-1(
TupleExpr:1(
SliceExpr:-1(
IntExpr(1)
IntExpr(2)
Expand Down Expand Up @@ -3426,25 +3426,25 @@ y = 30
f'Hello {x!s:<{y+y}}'
[out]
MypyFile:1(
AssignmentStmt:1(
NameExpr(x)
StrExpr(mypy))
AssignmentStmt:2(
NameExpr(y)
IntExpr(30))
ExpressionStmt:3(
CallExpr:3(
MemberExpr:3(
StrExpr()
join)
Args(
ListExpr:3(
StrExpr(Hello )
CallExpr:3(
MemberExpr:3(
StrExpr({!s:{}})
format)
Args(
AssignmentStmt:1(
NameExpr(x)
StrExpr(mypy))
AssignmentStmt:2(
NameExpr(y)
IntExpr(30))
ExpressionStmt:3(
CallExpr:3(
MemberExpr:3(
StrExpr()
join)
Args(
ListExpr:3(
StrExpr(Hello )
CallExpr:3(
MemberExpr:3(
StrExpr({!s:{}})
format)
Args(
NameExpr(x)
CallExpr:3(
MemberExpr:3(
Expand Down