astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.71k stars 1.09k forks source link

F-String formatting in assignment positions #13813

Open MichaReiser opened 3 weeks ago

MichaReiser commented 3 weeks ago

The new f-string formatting in assignment-value positions seems inconsistent to me:

aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
    expression}moreeeeeeeeeeeeeeeee"

Gets formatted to:

aaaaaaaaaaaaaaaaaa = f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{
    expression
}moreeeeeeeeeeeeeeeee"

Which avoids parentheses but it doesn't feel like the ideal formatting and it doesn't match the formatting if the expression starts out "flat"

aaaaaaaaaaaaaaaaaa = (
    f"testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee{expression}moreeeeeeeeeeeeeeeee"
)
MichaReiser commented 3 weeks ago

Hmm, I might have to fix this for the implicit concatenated string formatting because it handles this correctly and this now results in instable formatting

MichaReiser commented 3 weeks ago

One challenge is that we don't want to inline the comments if the expression breaks:

a = f"test{
    expression
}flat" f"can be {
    joined
} togethereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" # inline

Should be formatted to

a = (
    f"test{expression}flatcan be {
        joined
    } togethereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
)   # inline

and not


```python
a = (
    f"test{expression}flatcan be {
        joined
    } togethereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"  # inline
)
dhruvmanila commented 3 weeks ago

Hmm, I might have to fix this for the implicit concatenated string formatting because it handles this correctly and this now results in instable formatting

@MichaReiser Just want to check-in whether you ended up fixing this with implicit concatenated string formatting?

MichaReiser commented 3 weeks ago

I fixed the instability but I didn't fix the formatting. I suggest waiting with this task until the implicit concatenated string formatting PR is merged or that you work on top of it. We'll otherwise end up with horrible merge conflicts