Skip to content

Commit 6638ce8

Browse files
authored
fastparse: add line and column information for extended slices (#8743)
Helps with Python 3.9 test failures. Co-authored-by: hauntsaninja <>
1 parent 3b27904 commit 6638ce8

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

mypy/fastparse.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,14 @@ def visit_Attribute(self, n: Attribute) -> Union[MemberExpr, SuperExpr]:
12051205
def visit_Subscript(self, n: ast3.Subscript) -> IndexExpr:
12061206
e = IndexExpr(self.visit(n.value), self.visit(n.slice))
12071207
self.set_line(e, n)
1208-
if isinstance(e.index, SliceExpr):
1209-
# Slice has no line/column in the raw ast.
1208+
if (
1209+
isinstance(n.slice, ast3.Slice) or
1210+
(sys.version_info < (3, 9) and isinstance(n.slice, ast3.ExtSlice))
1211+
):
1212+
# Before Python 3.9, Slice has no line/column in the raw ast. To avoid incompatibility
1213+
# visit_Slice doesn't set_line, even in Python 3.9 on.
1214+
# ExtSlice also has no line/column info. In Python 3.9 on, line/column is set for
1215+
# e.index when visiting n.slice.
12101216
e.index.line = e.line
12111217
e.index.column = e.column
12121218
return e
@@ -1233,7 +1239,7 @@ def visit_List(self, n: ast3.List) -> Union[ListExpr, TupleExpr]:
12331239

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

12391245
# --- slice ---

test-data/unit/parse.test

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,7 @@ MypyFile:1(
31513151
ExpressionStmt:1(
31523152
IndexExpr:1(
31533153
NameExpr(a)
3154-
TupleExpr:-1(
3154+
TupleExpr:1(
31553155
SliceExpr:-1(
31563156
<empty>
31573157
<empty>)
@@ -3166,7 +3166,7 @@ MypyFile:1(
31663166
ExpressionStmt:1(
31673167
IndexExpr:1(
31683168
NameExpr(a)
3169-
TupleExpr:-1(
3169+
TupleExpr:1(
31703170
SliceExpr:-1(
31713171
IntExpr(1)
31723172
IntExpr(2))
@@ -3181,7 +3181,7 @@ MypyFile:1(
31813181
ExpressionStmt:1(
31823182
IndexExpr:1(
31833183
NameExpr(a)
3184-
TupleExpr:-1(
3184+
TupleExpr:1(
31853185
SliceExpr:-1(
31863186
IntExpr(1)
31873187
IntExpr(2)
@@ -3426,25 +3426,25 @@ y = 30
34263426
f'Hello {x!s:<{y+y}}'
34273427
[out]
34283428
MypyFile:1(
3429-
AssignmentStmt:1(
3430-
NameExpr(x)
3431-
StrExpr(mypy))
3432-
AssignmentStmt:2(
3433-
NameExpr(y)
3434-
IntExpr(30))
3435-
ExpressionStmt:3(
3436-
CallExpr:3(
3437-
MemberExpr:3(
3438-
StrExpr()
3439-
join)
3440-
Args(
3441-
ListExpr:3(
3442-
StrExpr(Hello )
3443-
CallExpr:3(
3444-
MemberExpr:3(
3445-
StrExpr({!s:{}})
3446-
format)
3447-
Args(
3429+
AssignmentStmt:1(
3430+
NameExpr(x)
3431+
StrExpr(mypy))
3432+
AssignmentStmt:2(
3433+
NameExpr(y)
3434+
IntExpr(30))
3435+
ExpressionStmt:3(
3436+
CallExpr:3(
3437+
MemberExpr:3(
3438+
StrExpr()
3439+
join)
3440+
Args(
3441+
ListExpr:3(
3442+
StrExpr(Hello )
3443+
CallExpr:3(
3444+
MemberExpr:3(
3445+
StrExpr({!s:{}})
3446+
format)
3447+
Args(
34483448
NameExpr(x)
34493449
CallExpr:3(
34503450
MemberExpr:3(

0 commit comments

Comments
 (0)