Skip to content

Commit

Permalink
Avoid curly brace escape in f-string format spec
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Oct 3, 2023
1 parent c6d0bdd commit 6d6c8e1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl<'source> Lexer<'source> {
}
}
'{' => {
if self.cursor.second() == '{' {
if self.cursor.second() == '{' && !fstring.is_in_format_spec(self.nesting) {
self.cursor.bump();
normalized
.push_str(&self.source[TextRange::new(last_offset, self.offset())]);
Expand Down Expand Up @@ -2047,7 +2047,7 @@ def f(arg=%timeit a = b):

#[test]
fn test_fstring_with_format_spec() {
let source = r#"f"{foo:} {x=!s:.3f} {x:.{y}f} {'':*^{1:{1}}}""#;
let source = r#"f"{foo:} {x=!s:.3f} {x:.{y}f} {'':*^{1:{1}}} {x:{{1}.pop()}}""#;
assert_debug_snapshot!(lex_source(source));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,76 @@ expression: lex_source(source)
43..44,
),
(
FStringEnd,
FStringMiddle {
value: " ",
is_raw: false,
},
44..45,
),
(
Lbrace,
45..46,
),
(
Name {
name: "x",
},
46..47,
),
(
Colon,
47..48,
),
(
Lbrace,
48..49,
),
(
Lbrace,
49..50,
),
(
Int {
value: 1,
},
50..51,
),
(
Rbrace,
51..52,
),
(
Dot,
52..53,
),
(
Name {
name: "pop",
},
53..56,
),
(
Lpar,
56..57,
),
(
Rpar,
57..58,
),
(
Rbrace,
58..59,
),
(
Rbrace,
59..60,
),
(
FStringEnd,
60..61,
),
(
Newline,
45..45,
61..61,
),
]

0 comments on commit 6d6c8e1

Please sign in to comment.