mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-01 17:32:25 +00:00
[syntax-errors] PEP 701 f-strings before Python 3.12 (#16543)
## Summary This PR detects the use of PEP 701 f-strings before 3.12. This one sounded difficult and ended up being pretty easy, so I think there's a good chance I've over-simplified things. However, from experimenting in the Python REPL and checking with [pyright], I think this is correct. pyright actually doesn't even flag the comment case, but Python does. I also checked pyright's implementation for [quotes](98dc4469cc/packages/pyright-internal/src/analyzer/checker.ts (L1379-L1398)
) and [escapes](98dc4469cc/packages/pyright-internal/src/analyzer/checker.ts (L1365-L1377)
) and think I've approximated how they do it. Python's error messages also point to the simple approach of these characters simply not being allowed: ```pycon Python 3.11.11 (main, Feb 12 2025, 14:51:05) [Clang 19.1.6 ] on linux Type "help", "copyright", "credits" or "license" for more information. >>> f'''multiline { ... expression # comment ... }''' File "<stdin>", line 3 }''' ^ SyntaxError: f-string expression part cannot include '#' >>> f'''{not a line \ ... continuation}''' File "<stdin>", line 2 continuation}''' ^ SyntaxError: f-string expression part cannot include a backslash >>> f'hello {'world'}' File "<stdin>", line 1 f'hello {'world'}' ^^^^^ SyntaxError: f-string: expecting '}' ``` And since escapes aren't allowed, I don't think there are any tricky cases where nested quotes or comments can sneak in. It's also slightly annoying that the error is repeated for every nested quote character, but that also mirrors pyright, although they highlight the whole nested string, which is a little nicer. However, their check is in the analysis phase, so I don't think we have such easy access to the quoted range, at least without adding another mini visitor. ## Test Plan New inline tests [pyright]: https://pyright-play.net/?pythonVersion=3.11&strict=true&code=EYQw5gBAvBAmCWBjALgCgO4gHaygRgEoAoEaCAIgBpyiiBiCLAUwGdknYIBHAVwHt2LIgDMA5AFlwSCJhwAuCAG8IoMAG1Rs2KIC6EAL6iIxosbPmLlq5foRWiEAAcmERAAsQAJxAomnltY2wuSKogA6WKIAdABWfPBYqCAE%2BuSBVqbpWVm2iHwAtvlMWMgB2ekiolUAgq4FjgA2TAAeEMieSADWCsoV5qoaqrrGDJ5MiDz%2B8ABuLqosAIREhlXlaybrmyYMXsDw7V4AnoysyAmQ5SIhwYo3d9cheADUeKlv5O%2BpQA
This commit is contained in:
parent
4ab529803f
commit
dcf31c9348
11 changed files with 2223 additions and 3 deletions
|
@ -166,6 +166,21 @@ impl<'src> TokenSource<'src> {
|
|||
self.tokens.truncate(tokens_position);
|
||||
}
|
||||
|
||||
/// Returns a slice of [`Token`] that are within the given `range`.
|
||||
pub(crate) fn in_range(&self, range: TextRange) -> &[Token] {
|
||||
let start = self
|
||||
.tokens
|
||||
.iter()
|
||||
.rposition(|tok| tok.start() == range.start());
|
||||
let end = self.tokens.iter().rposition(|tok| tok.end() == range.end());
|
||||
|
||||
let (Some(start), Some(end)) = (start, end) else {
|
||||
return &self.tokens;
|
||||
};
|
||||
|
||||
&self.tokens[start..=end]
|
||||
}
|
||||
|
||||
/// Consumes the token source, returning the collected tokens, comment ranges, and any errors
|
||||
/// encountered during lexing. The token collection includes both the trivia and non-trivia
|
||||
/// tokens.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue