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

Better multiline formatting for binary operators with function calls in operands #2023

Closed
rmorshea opened this issue Mar 6, 2021 · 2 comments
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?

Comments

@rmorshea
Copy link

rmorshea commented Mar 6, 2021

One things that's bugged me about black for a while is the way it handles the multiline formatting for binary operators when their operands are function calls. The case that most frequently occurs for me is in if statements. Consider the unformatted code below:

if some_function(an_argument) or some_other_function(another_argument, yet_another_argument):
    ...

Presently black will reformat this in the following way:

if some_function(an_argument) or some_other_function(
    another_argument, even_more_arguments
):
    ...

When I skim over the code above, I tend to read it a little like this:

if some_functi_________________________ther_function(
    another_argument, even_more_arguments
):
    ...

I end up passing over the content in the middle because:

  1. The arguments of the second function stands out and draw my attention
  2. This is a common reformatting pattern for other function calls that do not have operators on the same line

Further, even if you're someone who is able to read this correctly, this reformatting isn't "symetric" (for lack of a better word). Putting each operand of or into its own line would improve the aesthetics a great deal:

if (
    some_function(an_argument)
    or some_other_function(another_argument, yet_another_argument)
):
    ...

Yes, it takes up an extra line or two, but using more lines doesn't cost you anything, and I think the easier readability/aesthetic benefit is substantial. It's also worth noting that this form is what black adopts as soon as you add one more operator into the equation. For example, given the following unformatted code:

if some_function(an_argument) or some_other_function(another_argument, yet_another_argument) or the_last_thing:
    ...

black will reformat it into the more pleasing form:

if (
    some_function(an_argument)
    or some_other_function(another_argument, yet_another_argument)
    or the_last_thing
):
    ...

So beyond trying to reduce the number of lines in a file, it's not clear to me what the benefit of this form is:

if some_function(an_argument) or some_other_function(
    another_argument, yet_another_argument
):
    ...
@rmorshea rmorshea added the T: style What do we want Blackened code to look like? label Mar 6, 2021
@rmorshea
Copy link
Author

rmorshea commented Apr 1, 2021

I've realized the current behavior for this is rather inconsistent too. The following:

if my_really_long_function_name(the_first_argument, the_second_argument) or my_other_function(and_its_argument):
    ...

Get's formatted as:

if my_really_long_function_name(
    the_first_argument, the_second_argument
) or my_other_function(and_its_argument):
    ...

When this form happens to pass 88 characters:

if my_really_long_function_name(the_first_argument, the_second_argument) or my_other_function(
    and_its_argument
):
    ...

So adopting the proposed strategy for reformatting conditions described above would also help with the consistency too since there would only be one way to reformat in either scenario.

@JelleZijlstra JelleZijlstra added the F: linebreak How should we split up lines? label May 30, 2021
@felix-hilden
Copy link
Collaborator

I think this is essentially the same as #2156, so I'll close this in favor of it, thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

3 participants