Skip to content

Generator preferred quote style#20434

Merged
MichaReiser merged 17 commits intoastral-sh:mainfrom
ShaharNaveh:generator-quote-style
Sep 18, 2025
Merged

Generator preferred quote style#20434
MichaReiser merged 17 commits intoastral-sh:mainfrom
ShaharNaveh:generator-quote-style

Conversation

@ShaharNaveh
Copy link
Copy Markdown
Contributor

Closes #20432

Summary

Test Plan

@ShaharNaveh ShaharNaveh marked this pull request as ready for review September 16, 2025 11:09
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 16, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Sep 18, 2025
fn round_trip_with(
indentation: &Indentation,
line_ending: LineEnding,
quote: Option<Quote>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation now needs updating

}

#[test]
fn set_quote() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn set_quote() {
fn preferred_quote() {

indent: &'a Indentation,
/// The line ending to use.
line_ending: LineEnding,
/// Forced quote style to use.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment here is inaccurate. Setting quote doesn't force the generator to use the given quote everywhere. For example, setting Quote::Single for f'test{f"nested"}' doesn't change the quotes of the inner f-string. I also recommend adding a test showcasing this behavior

That's why I'd call this field preferred_quote. We should also extend the documentation to explain in more detail what it does (When None, the generator preserves the quote style when possible. When Some, the generator prefers the set quote style, disregarding the quote style used in the source)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a better name for sure


/// Force quote style for generated source code.
#[must_use]
pub fn with_quote(mut self, quote: Quote) -> Self {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as on the field documentation

Suggested change
pub fn with_quote(mut self, quote: Quote) -> Self {
pub fn with_preferred_quote(mut self, quote: Quote) -> Self {

Comment on lines +1408 to +1413
let flags = if let Some(quote) = self.quote {
flags.with_quote_style(quote)
} else {
*flags
};
self.p_str_repr(value, flags);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's clearer if we move the self.quote override to self.p_str_repr instead of "patching" the flags here.

@ShaharNaveh ShaharNaveh changed the title Generator quote style Generator preferred quote style Sep 18, 2025
Comment on lines +192 to +195
if let Some(preferred_quote) = self.preferred_quote {
flags = flags.with_quote_style(preferred_quote);
}

Copy link
Copy Markdown
Member

@MichaReiser MichaReiser Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of patching the flags here, can we do something like this:

let quote_style = self.preferred_quote.unwrap_or(flags.quote_style());
let escape = UnicodeEscape::with_preferred_quote(s, quote_style);

Do we need to add the same handling to:

  • unparse_interpolated_string_literal_element
  • p_bytes_repr
  • unparse_interpolated_string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice cleanup. will look into the other methods

/// - If [`Some`], the generator will always use the specified quote style,
/// ignoring the one found in the source.
#[must_use]
pub fn with_preferred_quote(mut self, quote: Option<Quote>) -> Self {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaReiser I've changed the signature to be able to take Option<Quote>, I think it makes more sense as it gives the ability to unset the preferred quote style & better aligns with the method's doc (IMO)

Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@MichaReiser MichaReiser merged commit 48ada2d into astral-sh:main Sep 18, 2025
35 checks passed
@ShaharNaveh ShaharNaveh deleted the generator-quote-style branch October 3, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't configure quote style for ruff_python_codegen::Generator

2 participants