Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,6 @@ async def c():

# Unicode escape
"\N{angle}AOB = {angle}°".format(angle=180)

# Raw string with \N{...}
r"\N{angle}AOB = {angle}°".format(angle=180)
6 changes: 5 additions & 1 deletion crates/ruff_linter/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ impl FStringConversion {
}

// Parse the format string.
let format_string = FormatString::from_str(contents)?;
let format_string = if raw {
FormatString::from_raw_str(contents)
} else {
FormatString::from_str(contents)
}?;

// If the format string contains only literal parts, it doesn't need to be converted.
if format_string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1378,10 +1378,29 @@ UP032 [*] Use f-string instead of `format` call
279 | # Unicode escape
280 | "\N{angle}AOB = {angle}°".format(angle=180)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
281 |
282 | # Raw string with \N{...}
|
help: Convert to f-string
277 | print(string)
278 |
279 | # Unicode escape
- "\N{angle}AOB = {angle}°".format(angle=180)
280 + f"\N{angle}AOB = {180}°"
281 |
282 | # Raw string with \N{...}
283 | r"\N{angle}AOB = {angle}°".format(angle=180)

UP032 [*] Use f-string instead of `format` call
--> UP032_0.py:283:1
|
282 | # Raw string with \N{...}
283 | r"\N{angle}AOB = {angle}°".format(angle=180)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Convert to f-string
280 | "\N{angle}AOB = {angle}°".format(angle=180)
281 |
282 | # Raw string with \N{...}
- r"\N{angle}AOB = {angle}°".format(angle=180)
283 + rf"\N{180}AOB = {180}°"
37 changes: 24 additions & 13 deletions crates/ruff_python_literal/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,14 @@ impl FormatString {
Ok((first_char, chars.as_str()))
}

fn parse_literal(text: &str) -> Result<(FormatPart, &str), FormatParseError> {
fn parse_literal(text: &str, is_raw: bool) -> Result<(FormatPart, &str), FormatParseError> {
let mut cur_text = text;
let mut result_string = String::new();
let mut pending_escape = false;
while !cur_text.is_empty() {
if pending_escape
// Raw strings: \N{...} is literal, not a Unicode escape
if !is_raw
&& pending_escape
&& let Some((unicode_string, remaining)) =
FormatString::parse_escaped_unicode_string(cur_text)
{
Expand Down Expand Up @@ -697,23 +699,14 @@ impl FormatString {
(&text[..end_idx], &text[end_idx..])
})
}
}

pub trait FromTemplate<'a>: Sized {
type Err;
fn from_str(s: &'a str) -> Result<Self, Self::Err>;
}

impl<'a> FromTemplate<'a> for FormatString {
type Err = FormatParseError;

fn from_str(text: &'a str) -> Result<Self, Self::Err> {
fn parse(text: &str, is_raw: bool) -> Result<Self, FormatParseError> {
let mut cur_text: &str = text;
let mut parts: Vec<FormatPart> = Vec::new();
while !cur_text.is_empty() {
// Try to parse both literals and bracketed format parts until we
// run out of text
cur_text = FormatString::parse_literal(cur_text)
cur_text = FormatString::parse_literal(cur_text, is_raw)
.or_else(|_| FormatString::parse_spec(cur_text, AllowPlaceholderNesting::Yes))
.map(|(part, new_text)| {
parts.push(part);
Expand All @@ -726,6 +719,24 @@ impl<'a> FromTemplate<'a> for FormatString {
}
}

pub trait FromTemplate<'a>: Sized {
type Err;
fn from_str(s: &'a str) -> Result<Self, Self::Err>;
fn from_raw_str(s: &'a str) -> Result<Self, Self::Err>;
}

impl<'a> FromTemplate<'a> for FormatString {
type Err = FormatParseError;

fn from_str(text: &'a str) -> Result<Self, Self::Err> {
FormatString::parse(text, false)
}

fn from_raw_str(text: &'a str) -> Result<Self, Self::Err> {
FormatString::parse(text, true)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down