mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Place 'r' prefix before 'f' for raw format strings (#8464)
## Summary Currently, `UP032` applied to raw strings results in format strings with the prefix 'fr'. This gets changed to 'rf' by Ruff format (or Black). In order to avoid that, this PR uses the prefix 'rf' to begin with. ## Test Plan Updated the expectation on an existing test.
This commit is contained in:
parent
7c12eaf322
commit
f56bc1983b
2 changed files with 12 additions and 3 deletions
|
@ -191,15 +191,21 @@ fn try_convert_to_f_string(
|
||||||
summary: &mut FormatSummaryValues,
|
summary: &mut FormatSummaryValues,
|
||||||
locator: &Locator,
|
locator: &Locator,
|
||||||
) -> Result<Option<String>> {
|
) -> Result<Option<String>> {
|
||||||
|
let contents = locator.slice(range);
|
||||||
|
|
||||||
// Strip the unicode prefix. It's redundant in Python 3, and invalid when used
|
// Strip the unicode prefix. It's redundant in Python 3, and invalid when used
|
||||||
// with f-strings.
|
// with f-strings.
|
||||||
let contents = locator.slice(range);
|
|
||||||
let contents = if contents.starts_with('U') || contents.starts_with('u') {
|
let contents = if contents.starts_with('U') || contents.starts_with('u') {
|
||||||
&contents[1..]
|
&contents[1..]
|
||||||
} else {
|
} else {
|
||||||
contents
|
contents
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Temporarily strip the raw prefix, if present. It will be prepended to the result, before the
|
||||||
|
// 'f', to match the prefix order both the Ruff formatter (and Black) use when formatting code.
|
||||||
|
let raw = contents.starts_with('R') || contents.starts_with('r');
|
||||||
|
let contents = if raw { &contents[1..] } else { contents };
|
||||||
|
|
||||||
// Remove the leading and trailing quotes.
|
// Remove the leading and trailing quotes.
|
||||||
let leading_quote = leading_quote(contents).context("Unable to identify leading quote")?;
|
let leading_quote = leading_quote(contents).context("Unable to identify leading quote")?;
|
||||||
let trailing_quote = trailing_quote(contents).context("Unable to identify trailing quote")?;
|
let trailing_quote = trailing_quote(contents).context("Unable to identify trailing quote")?;
|
||||||
|
@ -291,7 +297,10 @@ fn try_convert_to_f_string(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the format string.
|
// Construct the format string.
|
||||||
let mut contents = String::with_capacity(1 + converted.len());
|
let mut contents = String::with_capacity(usize::from(raw) + 1 + converted.len());
|
||||||
|
if raw {
|
||||||
|
contents.push('r');
|
||||||
|
}
|
||||||
contents.push('f');
|
contents.push('f');
|
||||||
contents.push_str(leading_quote);
|
contents.push_str(leading_quote);
|
||||||
contents.push_str(&converted);
|
contents.push_str(&converted);
|
||||||
|
|
|
@ -353,7 +353,7 @@ UP032_0.py:37:1: UP032 [*] Use f-string instead of `format` call
|
||||||
35 35 | "foo{}".format(1)
|
35 35 | "foo{}".format(1)
|
||||||
36 36 |
|
36 36 |
|
||||||
37 |-r"foo{}".format(1)
|
37 |-r"foo{}".format(1)
|
||||||
37 |+fr"foo{1}"
|
37 |+rf"foo{1}"
|
||||||
38 38 |
|
38 38 |
|
||||||
39 39 | x = "{a}".format(a=1)
|
39 39 | x = "{a}".format(a=1)
|
||||||
40 40 |
|
40 40 |
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue