Skip to content

Update W605 to check in f-strings #7295

@dhruvmanila

Description

@dhruvmanila

W605 checks for invalid escape sequences in a string.

For f-strings, we'll be looking for the same in a FStringMiddle token which is the non-expression part of the f-string. The rule extracts the body part which excludes the quotes and also skips the validation if it's a raw string:

let text = locator.slice(range);
// Determine whether the string is single- or triple-quoted.
let Some(leading_quote) = leading_quote(text) else {
return;
};
let Some(trailing_quote) = trailing_quote(text) else {
return;
};
let body = &text[leading_quote.len()..text.len() - trailing_quote.len()];
if leading_quote.contains(['r', 'R']) {
return;
}

The FStringMiddle token contains both the above information for free. We just need to check if the token is a FStringMiddle or a String.

Metadata

Metadata

Assignees

Labels

python312Related to Python 3.12ruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions