Consistent Nested F-String with Python >= 3.12 #15836
-
Python 3.12 and newer allow one to nest F-String using the same quotation mark. Is there a way to get ruff to format your document to do this? E.g.
Perhaps I am missing an option if it exists. Ideally it would use what you’ve set in “quote-style”. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can achieve this with a echo "f'{'abc'[0]}'" > example.py
ruff format \
--no-cache \
--config 'target-version = "py312"' \
--config 'format.quote-style = "preserve"' \
--diff example.py Unfortunately, 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! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the information! I’ll subscribe to the issue and use these workarounds in the meantime. :-) |
Beta Was this translation helpful? Give feedback.
You can achieve this with a
target-version
of Python 3.12 or higher andquote-style = "preserve"
: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!