-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always prefer double quotes for docstrings and triple-quoted srings #7680
Merged
+84
−68
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,32 +323,40 @@ impl StringPart { | |
self, | ||
quoting: Quoting, | ||
locator: &'a Locator, | ||
quote_style: QuoteStyle, | ||
configured_style: QuoteStyle, | ||
) -> NormalizedString<'a> { | ||
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings and triple-quoted | ||
// strings. (We assume docstrings are always triple-quoted.) | ||
let preferred_style = if self.quotes.triple { | ||
QuoteStyle::Double | ||
} else { | ||
configured_style | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic could go at multiple different levels in this call stack, but this felt simplest. |
||
|
||
let raw_content = locator.slice(self.content_range); | ||
|
||
let preferred_quotes = match quoting { | ||
let quotes = match quoting { | ||
Quoting::Preserve => self.quotes, | ||
Quoting::CanChange => { | ||
if self.prefix.is_raw_string() { | ||
preferred_quotes_raw(raw_content, self.quotes, quote_style) | ||
choose_quotes_raw(raw_content, self.quotes, preferred_style) | ||
} else { | ||
preferred_quotes(raw_content, self.quotes, quote_style) | ||
choose_quotes(raw_content, self.quotes, preferred_style) | ||
} | ||
} | ||
}; | ||
|
||
let normalized = normalize_string( | ||
locator.slice(self.content_range), | ||
preferred_quotes, | ||
quotes, | ||
self.prefix.is_raw_string(), | ||
); | ||
|
||
NormalizedString { | ||
prefix: self.prefix, | ||
content_range: self.content_range, | ||
text: normalized, | ||
quotes: preferred_quotes, | ||
quotes, | ||
} | ||
} | ||
} | ||
|
@@ -460,16 +468,17 @@ impl Format<PyFormatContext<'_>> for StringPrefix { | |
} | ||
} | ||
|
||
/// Detects the preferred quotes for raw string `input`. | ||
/// The configured quote style is preferred unless `input` contains unescaped quotes of the | ||
/// configured style. For example, `r"foo"` is preferred over `r'foo'` if the configured | ||
/// quote style is double quotes. | ||
fn preferred_quotes_raw( | ||
/// Choose the appropriate quote style for a raw string. | ||
/// | ||
/// The preferred quote style is chosen unless the string contains unescaped quotes of the | ||
/// preferred style. For example, `r"foo"` is chosen over `r'foo'` if the preferred quote | ||
/// style is double quotes. | ||
fn choose_quotes_raw( | ||
input: &str, | ||
quotes: StringQuotes, | ||
configured_style: QuoteStyle, | ||
preferred_style: QuoteStyle, | ||
) -> StringQuotes { | ||
let configured_quote_char = configured_style.as_char(); | ||
let preferred_quote_char = preferred_style.as_char(); | ||
let mut chars = input.chars().peekable(); | ||
let contains_unescaped_configured_quotes = loop { | ||
match chars.next() { | ||
|
@@ -478,7 +487,7 @@ fn preferred_quotes_raw( | |
chars.next(); | ||
} | ||
// `"` or `'` | ||
Some(c) if c == configured_quote_char => { | ||
Some(c) if c == preferred_quote_char => { | ||
if !quotes.triple { | ||
break true; | ||
} | ||
|
@@ -487,13 +496,13 @@ fn preferred_quotes_raw( | |
// We can't turn `r'''\""'''` into `r"""\"""""`, this would confuse the parser | ||
// about where the closing triple quotes start | ||
None => break true, | ||
Some(next) if *next == configured_quote_char => { | ||
Some(next) if *next == preferred_quote_char => { | ||
// `""` or `''` | ||
chars.next(); | ||
|
||
// We can't turn `r'''""'''` into `r""""""""`, nor can we have | ||
// `"""` or `'''` respectively inside the string | ||
if chars.peek().is_none() || chars.peek() == Some(&configured_quote_char) { | ||
if chars.peek().is_none() || chars.peek() == Some(&preferred_quote_char) { | ||
break true; | ||
} | ||
} | ||
|
@@ -510,41 +519,42 @@ fn preferred_quotes_raw( | |
style: if contains_unescaped_configured_quotes { | ||
quotes.style | ||
} else { | ||
configured_style | ||
preferred_style | ||
}, | ||
} | ||
} | ||
|
||
/// Detects the preferred quotes for `input`. | ||
/// * single quoted strings: The preferred quote style is the one that requires less escape sequences. | ||
/// * triple quoted strings: Use double quotes except the string contains a sequence of `"""`. | ||
fn preferred_quotes( | ||
input: &str, | ||
quotes: StringQuotes, | ||
configured_style: QuoteStyle, | ||
) -> StringQuotes { | ||
let preferred_style = if quotes.triple { | ||
/// Choose the appropriate quote style for a string. | ||
/// | ||
/// For single quoted strings, the preferred quote style is used, unless the alternative quote style | ||
/// would require fewer escapes. | ||
/// | ||
/// For triple quoted strings, the preferred quote style is always used, unless the string contains | ||
/// a triplet of the quote character (e.g., if double quotes are preferred, double quotes will be | ||
/// used unless the string contains `"""`). | ||
fn choose_quotes(input: &str, quotes: StringQuotes, preferred_style: QuoteStyle) -> StringQuotes { | ||
let style = if quotes.triple { | ||
// True if the string contains a triple quote sequence of the configured quote style. | ||
let mut uses_triple_quotes = false; | ||
let mut chars = input.chars().peekable(); | ||
|
||
while let Some(c) = chars.next() { | ||
let configured_quote_char = configured_style.as_char(); | ||
let preferred_quote_char = preferred_style.as_char(); | ||
match c { | ||
'\\' => { | ||
if matches!(chars.peek(), Some('"' | '\\')) { | ||
chars.next(); | ||
} | ||
} | ||
// `"` or `'` | ||
c if c == configured_quote_char => { | ||
c if c == preferred_quote_char => { | ||
match chars.peek().copied() { | ||
Some(c) if c == configured_quote_char => { | ||
Some(c) if c == preferred_quote_char => { | ||
// `""` or `''` | ||
chars.next(); | ||
|
||
match chars.peek().copied() { | ||
Some(c) if c == configured_quote_char => { | ||
Some(c) if c == preferred_quote_char => { | ||
// `"""` or `'''` | ||
chars.next(); | ||
uses_triple_quotes = true; | ||
|
@@ -579,7 +589,7 @@ fn preferred_quotes( | |
// Keep the existing quote style. | ||
quotes.style | ||
} else { | ||
configured_style | ||
preferred_style | ||
} | ||
} else { | ||
let mut single_quotes = 0u32; | ||
|
@@ -599,7 +609,7 @@ fn preferred_quotes( | |
} | ||
} | ||
|
||
match configured_style { | ||
match preferred_style { | ||
QuoteStyle::Single => { | ||
if single_quotes > double_quotes { | ||
QuoteStyle::Double | ||
|
@@ -619,7 +629,7 @@ fn preferred_quotes( | |
|
||
StringQuotes { | ||
triple: quotes.triple, | ||
style: preferred_style, | ||
style, | ||
} | ||
} | ||
|
||
|
@@ -668,7 +678,7 @@ impl Format<PyFormatContext<'_>> for StringQuotes { | |
} | ||
|
||
/// Adds the necessary quote escapes and removes unnecessary escape sequences when quoting `input` | ||
/// with the provided `style`. | ||
/// with the provided [`StringQuotes`] style. | ||
/// | ||
/// Returns the normalized string and whether it contains new lines. | ||
fn normalize_string(input: &str, quotes: StringQuotes, is_raw: bool) -> Cow<str> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: The variable here is called
preferred_style
but it is calledconfigured_style
inpeferred_quotes_raw
andpreferred_quotes
.I would be okay calling it
configured_style
or maybedefault_style
to make it clear it's what we would default to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I went back and forth on these a few times 😂