mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Stabilize quote-style preserve
(#9922)
This commit is contained in:
parent
6dc1b21917
commit
4946a1876f
7 changed files with 37 additions and 33 deletions
|
@ -166,12 +166,6 @@ impl Configuration {
|
|||
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
|
||||
};
|
||||
|
||||
if quote_style == QuoteStyle::Preserve && !format_preview.is_enabled() {
|
||||
return Err(anyhow!(
|
||||
"'quote-style = preserve' is a preview only feature. Run with '--preview' to enable it."
|
||||
));
|
||||
}
|
||||
|
||||
let formatter = FormatterSettings {
|
||||
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
|
||||
extension: self.extension.clone().unwrap_or_default(),
|
||||
|
|
|
@ -2942,28 +2942,28 @@ pub struct FormatOptions {
|
|||
)]
|
||||
pub indent_style: Option<IndentStyle>,
|
||||
|
||||
/// Configures the preferred quote character for strings. Valid options are:
|
||||
///
|
||||
/// Configures the preferred quote character for strings. The recommended options are
|
||||
/// * `double` (default): Use double quotes `"`
|
||||
/// * `single`: Use single quotes `'`
|
||||
/// * `preserve` (preview only): Keeps the existing quote character. We don't recommend using this option except for projects
|
||||
/// that already use a mixture of single and double quotes and can't migrate to using double or single quotes.
|
||||
///
|
||||
/// In compliance with [PEP 8](https://peps.python.org/pep-0008/) and [PEP 257](https://peps.python.org/pep-0257/),
|
||||
/// Ruff prefers double quotes for multiline strings and docstrings, regardless of the
|
||||
/// configured quote style.
|
||||
/// Ruff prefers double quotes for triple quoted strings and docstrings even when using `quote-style = "single"`.
|
||||
///
|
||||
/// Ruff may also deviate from using the configured quotes if doing so requires
|
||||
/// escaping quote characters within the string. For example, given:
|
||||
/// Ruff deviates from using the configured quotes if doing so prevents the need for
|
||||
/// escaping quote characters inside the string:
|
||||
///
|
||||
/// ```python
|
||||
/// a = "a string without any quotes"
|
||||
/// b = "It's monday morning"
|
||||
/// ```
|
||||
///
|
||||
/// Ruff will change `a` to use single quotes when using `quote-style = "single"`. However,
|
||||
/// `b` remains unchanged, as converting to single quotes requires escaping the inner `'`,
|
||||
/// which leads to less readable code: `'It\'s monday morning'`. This does not apply when using `preserve`.
|
||||
/// Ruff will change the quotes of the string assigned to `a` to single quotes when using `quote-style = "single"`.
|
||||
/// However, ruff uses double quotes for he string assigned to `b` because using single quotes would require escaping the `'`,
|
||||
/// which leads to the less readable code: `'It\'s monday morning'`.
|
||||
///
|
||||
/// In addition, Ruff supports the quote style `preserve` for projects that already use
|
||||
/// a mixture of single and double quotes and can't migrate to the `double` or `single` style.
|
||||
/// The quote style `preserve` leaves the quotes of all strings unchanged.
|
||||
#[option(
|
||||
default = r#"double"#,
|
||||
value_type = r#""double" | "single" | "preserve""#,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue