Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: multiline conditionals wrap differently #7314

Closed
DanCardin opened this issue Sep 12, 2023 · 4 comments · Fixed by #7679
Closed

Formatter: multiline conditionals wrap differently #7314

DanCardin opened this issue Sep 12, 2023 · 4 comments · Fixed by #7679
Assignees
Labels
documentation Improvements or additions to documentation formatter Related to the formatter

Comments

@DanCardin
Copy link
Contributor

Black wants to split boolean operators across lines, where they're being forced (at least here due to the comment) onto multiple lines:

some_example_var = ""
if not some_example_var or (
    # A comment in the middle
    some_example_var
    and some_example_var not in some_example_var
):
    pass

whereas ruff wants to combine them onto one line:

some_example_var = ""
if not some_example_var or (
    # A comment in the middle
    some_example_var and some_example_var not in some_example_var
):
    pass

I dont really have a preference, it was just one of 3 total formatting diffs over 30k LOC (one being a documented intentional diff).

@charliermarsh charliermarsh added formatter Related to the formatter needs-decision Awaiting a decision from a maintainer labels Sep 12, 2023
@MichaReiser
Copy link
Member

MichaReiser commented Sep 13, 2023

Thanks for reporting this deviation. It isn't specific to bool expression but more generally that Black expands the commented node whereas Ruff does not (because associate the comment with the outer most vs inner most node):

Input

# A comment in the middle
some_example_var and some_example_var not in some_example_var

a = (
    # test
    a if b else c
)

a = x[
    #test
    a + b : b + c
]

a = (
    # A comment in the middle
    some_example_var and some_example_var not in some_example_var
)

Black

# A comment in the middle
some_example_var and some_example_var not in some_example_var

a = (
    # test
    a
    if b
    else c
)

a = x[
    # test
    a
    + b : b
    + c
]

a = (
    # A comment in the middle
    some_example_var
    and some_example_var not in some_example_var
)

Ruff

# A comment in the middle
some_example_var and some_example_var not in some_example_var

a = (
    # test
    a if b else c
)

a = x[
    # test
    a + b : b + c
]

a = (
    # A comment in the middle
    some_example_var and some_example_var not in some_example_var
)

I generally prefer Ruff's formatting because it avoids unnecessary line breaks. But eager to hear more opinions.

@MichaReiser MichaReiser added wontfix This will not be worked on and removed needs-decision Awaiting a decision from a maintainer labels Sep 13, 2023
@MichaReiser MichaReiser added this to the Formatter: Beta milestone Sep 13, 2023
@DanCardin
Copy link
Contributor Author

Ultimately i think the effect is very marginal either way, but I think perhaps I agree.

Particularly with the deviations regarding inline comments, it seems like this gives more control over where the comment "targets", as you can insert more comments to arrive at the same formatting decision otherwise, if it's important to the block of code.

Mostly posted so it could get added to the list of intentional deviations, if you were going to keep it.

@MichaReiser
Copy link
Member

Mostly posted so it could get added to the list of intentional deviations, if you were going to keep it.

Thank you for reporting it. It's extremely helpful for us to have a collection of all deviations because we should either:

  • Document them or
  • fix them.

So thanks again :)

@charliermarsh
Copy link
Member

We're going to mark this as an intentional deviation for now. Can be closed once it's documented.

@MichaReiser MichaReiser added documentation Improvements or additions to documentation and removed wontfix This will not be worked on labels Sep 27, 2023
@charliermarsh charliermarsh self-assigned this Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants