Skip to content

Consistent Nested F-String with Python >= 3.12 #15836

Closed Answered by ntBre
TheSkyentist asked this question in Q&A
Discussion options

You must be logged in to vote

You can achieve this with a target-version of Python 3.12 or higher and quote-style = "preserve":

echo "f'{'abc'[0]}'" > example.py

ruff format \
    --no-cache \
    --config 'target-version = "py312"' \
    --config 'format.quote-style = "preserve"' \
    --diff example.py

Unfortunately, preserve has the downside of not changing any quotes, but otherwise the formatter currently prefers alternating quotes for f-strings unless the outer quotes are tripled.

There's also an open feature request for this to be added as an option at #14118, so we're still keeping it in mind!

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@ntBre
Comment options

Answer selected by TheSkyentist
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
formatter Related to the formatter
2 participants