Skip to content

Commit 37af21b

Browse files
authored
bpo-40334: Fix shifting of nested f-strings in the new parser (GH-19771)
`JoinedStr`s and `FormattedValue also needs to be shifted, in order to correctly compute the location information of nested f-strings.
1 parent ae00a5a commit 37af21b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Lib/test/test_fstring.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ def test_ast_line_numbers_nested(self):
207207
call = binop.right.values[1].value
208208
self.assertEqual(type(call), ast.Call)
209209
self.assertEqual(call.lineno, 3)
210-
if support.use_old_parser():
211-
self.assertEqual(call.col_offset, 11)
210+
self.assertEqual(call.col_offset, 11)
212211

213212
def test_ast_line_numbers_duplicate_expression(self):
214213
"""Duplicate expression

Parser/pegen/parse_string.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ static void fstring_shift_children_locations(expr_ty n, int lineno, int col_offs
449449
case Tuple_kind:
450450
fstring_shift_seq_locations(n, n->v.Tuple.elts, lineno, col_offset);
451451
break;
452+
case JoinedStr_kind:
453+
fstring_shift_seq_locations(n, n->v.JoinedStr.values, lineno, col_offset);
454+
break;
455+
case FormattedValue_kind:
456+
shift_expr(n, n->v.FormattedValue.value, lineno, col_offset);
457+
if (n->v.FormattedValue.format_spec) {
458+
shift_expr(n, n->v.FormattedValue.format_spec, lineno, col_offset);
459+
}
460+
break;
452461
default:
453462
return;
454463
}

0 commit comments

Comments
 (0)