Stabilize quote-style preserve (#9922)

This commit is contained in:
Micha Reiser 2024-02-12 10:30:07 +01:00 committed by GitHub
parent 6dc1b21917
commit 4946a1876f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 37 additions and 33 deletions

View file

@ -248,6 +248,12 @@ pub enum QuoteStyle {
Preserve,
}
impl QuoteStyle {
pub const fn is_preserve(self) -> bool {
matches!(self, QuoteStyle::Preserve)
}
}
impl fmt::Display for QuoteStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {

View file

@ -50,11 +50,13 @@ impl Format<PyFormatContext<'_>> for FormatStringLiteral<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let locator = f.context().locator();
let quote_style = if self.layout.is_docstring() {
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings
let quote_style = f.options().quote_style();
let quote_style = if self.layout.is_docstring() && !quote_style.is_preserve() {
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings,
// except when using quote-style=preserve
QuoteStyle::Double
} else {
f.options().quote_style()
quote_style
};
let normalized = StringPart::from_source(self.value.range(), &locator).normalize(

View file

@ -306,6 +306,7 @@ impl StringPart {
normalize_hex: bool,
) -> NormalizedString<'a> {
// Per PEP 8, always prefer double quotes for triple-quoted strings.
// Except when using quote-style-preserve.
let preferred_style = if self.quotes.triple {
// ... unless we're formatting a code snippet inside a docstring,
// then we specifically want to invert our quote style to avoid
@ -354,6 +355,8 @@ impl StringPart {
// if it doesn't have perfect alignment with PEP8.
if let Some(quote) = parent_docstring_quote_char {
QuoteStyle::from(quote.invert())
} else if configured_style.is_preserve() {
QuoteStyle::Preserve
} else {
QuoteStyle::Double
}

View file

@ -263,21 +263,21 @@ rb"rb double"
rb'br single'
rb"br double"
"""single triple"""
'''single triple'''
"""double triple"""
r"""r single triple"""
r'''r single triple'''
r"""r double triple"""
f"""f single triple"""
f'''f single triple'''
f"""f double triple"""
rf"""fr single triple"""
rf'''fr single triple'''
rf"""fr double triple"""
rf"""rf single triple"""
rf'''rf single triple'''
rf"""rf double triple"""
b"""b single triple"""
b'''b single triple'''
b"""b double triple"""
rb"""rb single triple"""
rb'''rb single triple'''
rb"""rb double triple"""
rb"""br single triple"""
rb'''br single triple'''
rb"""br double triple"""
'single1' 'single2'
@ -287,7 +287,7 @@ rb"""br double triple"""
def docstring_single_triple():
"""single triple"""
'''single triple'''
def docstring_double_triple():
@ -299,7 +299,7 @@ def docstring_double():
def docstring_single():
"single"
'single'
```
@ -308,8 +308,7 @@ def docstring_single():
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
-'single' # this string is treated as a docstring
+"single" # this string is treated as a docstring
'single' # this string is treated as a docstring
+
"double"
r'r single'