mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-02 00:27:51 +00:00
Report precise location for invalid conversion flag (#7809)
## Summary This PR updates the parser definition to use the precise location when reporting an invalid f-string conversion flag error. Taking the following example code: ```python f"{foo!x}" ``` On earlier version, ``` Error: f-string: invalid conversion character at byte offset 6 ``` Now, ``` Error: f-string: invalid conversion character at byte offset 7 ``` This becomes more useful when there's whitespace between `!` and the flag value although that is not valid but we can't detect that now. ## Test Plan As mentioned above.
This commit is contained in:
parent
adb6580270
commit
17fba99ed4
2 changed files with 15 additions and 4 deletions
|
@ -1662,14 +1662,14 @@ FStringFormatSpec: ast::Expr = {
|
|||
};
|
||||
|
||||
FStringConversion: (TextSize, ast::ConversionFlag) = {
|
||||
<location:@L> "!" <s:name> =>? {
|
||||
<location:@L> "!" <name_location:@L> <s:name> =>? {
|
||||
let conversion = match s.as_str() {
|
||||
"s" => ast::ConversionFlag::Str,
|
||||
"r" => ast::ConversionFlag::Repr,
|
||||
"a" => ast::ConversionFlag::Ascii,
|
||||
_ => Err(LexicalError {
|
||||
error: LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag),
|
||||
location,
|
||||
location: name_location,
|
||||
})?
|
||||
};
|
||||
Ok((location, conversion))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue