Skip to content

Feature request: Allow Configurable Quote Styling for f-Strings in Python 3.12+ #14118

Open
@matthewlloyd

Description

I would like to request an option in Ruff formatter to configure quote styling for f-strings in Python 3.12 and later - specifically, the ability to maintain consistent use of double quotes throughout f-strings, even when nesting quotes.

Background

With the introduction of Python 3.12, it's now possible to nest double quotes within double-quoted f-strings without needing to alternate between single and double quotes. This enhancement allows for more consistent and uniform code styling.

Currently, Ruff formatter's preview mode reverts nested double quotes to single quotes inside double-quoted f-strings, following the style used in earlier Python versions. While this approach ensures backward compatibility, it may not be ideal for codebases that have migrated to Python 3.12 and prefer consistent quote usage.

Benefits

  • Reduced cognitive load: Consistent quotes within f-strings simplify reading and writing code, reducing the mental overhead associated with switching between quote types.
  • Improved readability: Uniform quote usage enhances the clarity of f-strings, especially in longer or more intricate strings with nested quotes.
  • Seamless copying and pasting: Expressions within f-strings can be copied and pasted without needing to adjust quote styles, streamlining code modifications.

Proposal

Introduce a configuration option in Ruff formatter that allows developers to:

  • Opt-in to consistent quote styling for f-strings in Python 3.12 and later.
  • Choose their preferred quote style (e.g., always use double quotes).

This flexibility would enable developers to take full advantage of the new string handling capabilities in Python 3.12 while adhering to their codebase's stylistic preferences.

This configuration option could be a sibling of the existing option, quote-style, which allows developers to choose between double or single quote characters: https://docs.astral.sh/ruff/settings/#format_quote-style

Alternative Solution

As an alternative to adding a new configuration option, Ruff formatter could automatically switch to the new f-string quote formatting when the target Python version is set to 3.12 or higher. This would involve:

  • Default Behavior Change for Python 3.12+: When the target Python version is 3.12 or above, Ruff formatter would default to using consistent quotes throughout f-strings, leveraging the enhanced syntax support.
  • Backward Compatibility: For projects targeting earlier Python versions, the formatter would maintain the existing behavior of alternating quotes to ensure compatibility.
  • Simplified Configuration: This approach eliminates the need for additional configuration options, making it easier for developers to adopt the new formatting style without extra setup.

Rationale

  • Alignment with Python Evolution: Adopting the new formatting by default for Python 3.12+ aligns Ruff formatter with the latest Python language features and future direction.
  • Temporary Compatibility Concerns: While changing quotes might affect syntax highlighting in some tools, major IDEs like VS Code and PyCharm already support the new syntax. Any remaining tooling issues are likely to be resolved over time.
  • Future-Proofing: Considering that pre-3.12 versions of Python will reach end-of-life after October 2027, updating the default behavior now helps future-proof codebases and reduces the need for later adjustments.

Additional context

I understand that changing the default behavior may have implications for existing workflows. However, making this adjustment based on the target Python version provides a balanced approach that benefits developers who are ready to adopt Python 3.12 features while preserving the experience for those who are not.

Thank you for considering this feature request.

References

For further discussion, see the comments here: #11056 (comment)

The pull request which changed the f-string quote style from consistent to alternating in the formatter's preview mode is here: #13860

Metadata

Assignees

No one assigned

    Labels

    formatterRelated to the formatterstyleHow should formatted code look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions