diff --git a/crates/ruff_python_formatter/src/expression/string.rs b/crates/ruff_python_formatter/src/expression/string.rs index 0fef24af91cf48..dd0deed2de3fa3 100644 --- a/crates/ruff_python_formatter/src/expression/string.rs +++ b/crates/ruff_python_formatter/src/expression/string.rs @@ -443,7 +443,9 @@ impl StringPart { let quotes = match quoting { Quoting::Preserve => self.quotes, Quoting::CanChange => { - if self.prefix.is_raw_string() { + if preferred_style == QuoteStyle::Preserve { + self.quotes + } else if self.prefix.is_raw_string() { choose_quotes_raw(raw_content, self.quotes, preferred_style) } else { choose_quotes(raw_content, self.quotes, preferred_style) @@ -722,7 +724,7 @@ fn choose_quotes(input: &str, quotes: StringQuotes, preferred_style: QuoteStyle) QuoteStyle::Single } } - QuoteStyle::Double => { + QuoteStyle::Double | QuoteStyle::Preserve => { if double_quotes > single_quotes { QuoteStyle::Single } else { @@ -774,8 +776,8 @@ impl Format> for StringQuotes { let quotes = match (self.style, self.triple) { (QuoteStyle::Single, false) => "'", (QuoteStyle::Single, true) => "'''", - (QuoteStyle::Double, false) => "\"", - (QuoteStyle::Double, true) => "\"\"\"", + (QuoteStyle::Double | QuoteStyle::Preserve, false) => "\"", + (QuoteStyle::Double | QuoteStyle::Preserve, true) => "\"\"\"", }; token(quotes).fmt(f) diff --git a/crates/ruff_python_formatter/src/options.rs b/crates/ruff_python_formatter/src/options.rs index b39c7b6ff79c64..f0ddfc8909d7d9 100644 --- a/crates/ruff_python_formatter/src/options.rs +++ b/crates/ruff_python_formatter/src/options.rs @@ -190,6 +190,7 @@ pub enum QuoteStyle { Single, #[default] Double, + Preserve, } impl QuoteStyle { @@ -197,6 +198,7 @@ impl QuoteStyle { match self { QuoteStyle::Single => '\'', QuoteStyle::Double => '"', + QuoteStyle::Preserve => '"', // not used } } @@ -205,6 +207,7 @@ impl QuoteStyle { match self { QuoteStyle::Single => QuoteStyle::Double, QuoteStyle::Double => QuoteStyle::Single, + QuoteStyle::Preserve => QuoteStyle::Preserve, } } } @@ -228,6 +231,7 @@ impl FromStr for QuoteStyle { match s { "\"" | "double" | "Double" => Ok(Self::Double), "'" | "single" | "Single" => Ok(Self::Single), + "preserve" | "Preserve" => Ok(Self::Preserve), // TODO: replace this error with a diagnostic _ => Err("Value not supported for QuoteStyle"), } diff --git a/pyproject.toml b/pyproject.toml index 3d9facb8c96921..80dce697878f13 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,16 +50,14 @@ python-source = "python" strip = true exclude = [ "crates/ruff_linter/resources/test/fixtures/**/*", - "crates/ruff_linter/src/rules/*/snapshots/**/*" -] -include = [ - "rust-toolchain.toml" + "crates/ruff_linter/src/rules/*/snapshots/**/*", ] +include = ["rust-toolchain.toml"] [tool.ruff] extend-exclude = [ "crates/ruff_linter/resources/", - "crates/ruff_python_formatter/resources/" + "crates/ruff_python_formatter/resources/", ] [tool.black] @@ -71,8 +69,8 @@ force-exclude = ''' ''' [tool.rooster] -major_labels = [] # Ruff never uses the major version number -minor_labels = ["breaking"] # Bump the minor version on breaking changes +major_labels = [] # Ruff never uses the major version number +minor_labels = ["breaking"] # Bump the minor version on breaking changes changelog_ignore_labels = ["internal"] @@ -91,7 +89,7 @@ changelog_sections.__unknown__ = "Other changes" changelog_contributors = false version_files = [ - "README.md", + "README.md", "docs/integrations.md", "crates/flake8_to_ruff/Cargo.toml", "crates/ruff_cli/Cargo.toml", diff --git a/ruff.schema.json b/ruff.schema.json index c4acb767cd5859..325384665220a7 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2418,7 +2418,8 @@ "type": "string", "enum": [ "single", - "double" + "double", + "preserve" ] }, "RelativeImportsOrder": {