mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 13:30:49 -05: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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user