astral-sh/ruff

Bug: Formatting inconsistency with f strings

Closed this issue · 2 comments

I would assume that the second version of mystr would be formatted, but it doesn't

i = 2

# This gets formatted
sign = "+" if i > 0 else "-"
sign = "+" if i>0 else "-" # gets spaces around '>'

# This does not get formatted
mystr = f"HI {'+' if i > 0 else '-'}{i}"
mystr = f"HI {'+' if i>0 else '-'}{i}"

Ruff supports f-string formatting but only as a preview feature. We plan to release it early next year, once we figured out all edge cases :) You can give it a try by setting format.preview = true.

See #13371

Tried that in the meantime, you beat me to it. Thanks!