mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 01:20:24 +00:00
Avoid curly brace escape in f-string format spec (#7780)
## Summary This PR fixes a bug in the lexer for f-string format spec where it would consider the `{{` (double curly braces) as an escape pattern. This is not the case as evident by the [PEP](https://peps.python.org/pep-0701/#how-to-produce-these-new-tokens) as well but I missed the part: > [..] > * **If in “format specifier mode” (see step 3), an opening brace ({) or a closing brace (}).** > * If not in “format specifier mode” (see step 3), an opening brace ({) or a closing brace (}) that is not immediately followed by another opening/closing brace. ## Test Plan Add a test case to verify the fix and update the snapshot. fixes: #7778
This commit is contained in:
parent
c040fac12f
commit
69b8136463
2 changed files with 69 additions and 4 deletions
|
@ -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())]);
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue