Skip to content
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

Add f-string consistent quotes formatting option #16385

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

matthewlloyd
Copy link
Contributor

Introduces a new formatter option f-string-consistent-quotes that leverages Python 3.12's PEP 701 to use consistent quotes inside f-string expressions rather than alternating quote styles for compatibility.

When enabled and targeting Python 3.12+, the formatter will use the same quote style (following the quote-style setting) inside f-string expressions as in the outer f-string. This produces more consistent and readable code.

When disabled (default) or targeting Python versions below 3.12, the formatter will continue to alternate quotes for compatibility.

Implements: #14118

Introduces a new formatter option `f-string-consistent-quotes` that leverages Python 3.12's PEP 701 to use consistent quotes inside f-string expressions rather than alternating quote styles for compatibility.

When enabled and targeting Python 3.12+, the formatter will use the same quote style (following the `quote-style` setting) inside f-string expressions as in the outer f-string. This produces more consistent and readable code.

When disabled (default) or targeting Python versions below 3.12, the formatter will continue to alternate quotes for compatibility.

🤖 Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

Thanks for working on this. I don't feel like I've enough information/conviction yet that we should introduce this option. That's why I prefer to wait with this change a little longer.

@MichaReiser MichaReiser added configuration Related to settings and configuration formatter Related to the formatter needs-decision Awaiting a decision from a maintainer labels Feb 26, 2025
@matthewlloyd
Copy link
Contributor Author

Thanks for considering this PR. I'd like to clarify a few points that might help with the decision:

  1. This change introduces an opt-in configuration option that doesn't alter existing behavior by default. Users who prefer the current approach can continue using it unchanged.

  2. The feature aligns with Ruff's existing philosophy of giving users control over code formatting. Just as quote-style lets users choose their preferred quotes globally, this option extends that same control to quotes inside f-strings. As it is, Ruff allows users to choose their quotes, yet arbitrarily dictates them in nested f-string expressions.

  3. Since Python 3.12 specifically introduced this capability with PEP 701, it seems logical to provide a way for users to take advantage of it. The implementation only applies when targeting Python 3.12+, maintaining backward compatibility. Older versions of Python will one day be obsolete and alternating quotes will become vestigial and unnecessary, so this will likely need to be done eventually anyway.

  4. Six users have already expressed interest in this feature in the initial issue (Feature request: Allow Configurable Quote Styling for f-Strings in Python 3.12+ #14118), suggesting there's demand for this level of formatting control.

What additional information would be helpful to move this forward? I'm happy to address any specific concerns about the implementation or rationale.

@MichaReiser
Copy link
Member

MichaReiser commented Feb 26, 2025

It's mainly that I'm not sure that there's enough demand to justify an extra option. We don't have as high a bar as black on options but we still maintain a fairly high bar on new options because each option introduces the risk that people now start discussing options instead of formatting decisions. That's why I want to wait a little longer to see if there's more demand for the option introduced in this PR

@matthewlloyd
Copy link
Contributor Author

Thanks for the feedback. I understand your concern about demand, but I'd like to respectfully point out that you accepted and implemented the original feature request to make quote styles alternating (#11056) even though it had been requested by and had support from only a single user. This feature has been requested by six users already.

The original change to enforce alternating quotes made in 2024 was made to cater to a narrow use case - backporting to Python versions released before 2022 - while this configuration option will benefit all current users of Python 3.12 (released two years ago already, in 2023) and all future versions. Given that Python's trajectory is clearly moving toward consistent quote handling in f-strings with PEP 701, it seems reasonable to provide users the option to follow this modern approach.

Additionally, this is a non-default configuration option that requires explicit opt-in, so it doesn't impact existing users or add complexity to the default experience. It simply provides flexibility for those who want to leverage Python 3.12's improved f-string capabilities.

@tlauli
Copy link

tlauli commented Feb 26, 2025

I would very much like to see this merged. I am currently postponing upgrading ruff to ^0.9 in my repo, because I do not like the alternating style. I was surprised that the option for consistent quotes was not already present when I saw the default was to alternate quotes.

It seems weird to not be able to opt in to something, that in my opinion should be the default behavior after python 3.11 end of life. I do not believe we should be restricted by limitations of old versions forever, and allowing the transition to happen sooner on an opt in basis will only make it smoother.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration formatter Related to the formatter needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants