Add support for reformatting byte strings (#3176)

This commit is contained in:
Charlie Marsh 2023-02-23 11:50:24 -05:00 committed by GitHub
parent f967f344fc
commit 1e7233a8eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 90 additions and 59 deletions

View file

@ -652,7 +652,7 @@ fn format_constant(
write!(f, [text("False")])?;
}
}
Constant::Str(_) => write!(f, [string_literal(expr)])?,
Constant::Str(_) | Constant::Bytes(_) => write!(f, [string_literal(expr)])?,
_ => write!(f, [literal(Range::from_located(expr))])?,
}
Ok(())

View file

@ -37,6 +37,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
}
}
// Retain raw prefixes.
let mut is_raw = false;
if leading_quote.contains('r') {
is_raw = true;
@ -46,6 +47,11 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
f.write_element(FormatElement::StaticText { text: "R" })?;
}
// Normalize bytes literals to use b"...".
if leading_quote.contains('b') || leading_quote.contains('B') {
f.write_element(FormatElement::StaticText { text: "b" })?;
}
if trailing_quote.len() == 1 {
// Single-quoted string.
if dquotes == 0 || squotes > 0 {