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

Question on binary operator linebreaking #587

Closed
jleclanche opened this issue Oct 25, 2018 · 3 comments
Closed

Question on binary operator linebreaking #587

jleclanche opened this issue Oct 25, 2018 · 3 comments
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?

Comments

@jleclanche
Copy link

Operating system: Linux
Python version: 3.7
Black version: Master

I have the following code:

def test_foo():
    espec = ...
    assert (
        espec.EncodingSpec("e:{A6D4CFE470214878,FD4466FC,n}") ==
        espec.EncodingSpec("e:{A6D4CFE470214878,FD4466FC,n}")
    )

This gets reformatted to the following:

def test_foo():
    espec = ...
    assert espec.EncodingSpec("e:{A6D4CFE470214878,FD4466FC,n}") == espec.EncodingSpec(
        "e:{A6D4CFE470214878,FD4466FC,n}"
    )

The latter is the simpler way to linebreak but is IMO a lot less readable (especially in the case of tests). Is there a case to be made to prefer the former style? I believe that's also how prettier prefers linebreaking as it's a lot more readable in general.

@cas--
Copy link
Contributor

cas-- commented Nov 8, 2018

For reference the line break should be before the operator like this:

    assert (
        espec.EncodingSpec("e:{A6D4CFE470214878,FD4466FC,n}")
        == espec.EncodingSpec("e:{A6D4CFE470214878,FD4466FC,n}")
    )

I have seen the same happen with if statements and personally would prefer this outcome:

if (
    very_long_long_long_long_func('argument') 
    or other_very_long_long_long_func('argument')
):
    pass

to current output:

if very_long_long_long_long_func("argument") or other_very_long_long_long_func(
    "argument"
):
    pass

Somewhat related to issue #571

@pradyunsg
Copy link
Contributor

pradyunsg commented Mar 22, 2019

I have a code snippet as follows:

        looks_like_pep = (
            file_path.startswith("pep-") and
            file_path.endswith((".txt", "rst")
        )

With line length 79, black re-formats this code to:

        looks_like_pep = file_path.startswith("pep-") and file_path.endswith(
            (".txt", "rst")
        )

Reducing the line length to 75 re-formats the code to:

        looks_like_pep = file_path.startswith(
            "pep-"
        ) and file_path.endswith((".txt", "rst"))

Both outputs are worse IMO than the input here.

ISTM, black more readily breaks around parenthesis than around a binary logical operator.

@JelleZijlstra JelleZijlstra added the T: style What do we want Blackened code to look like? label May 5, 2019
@JelleZijlstra JelleZijlstra added the F: linebreak How should we split up lines? label May 30, 2021
@felix-hilden
Copy link
Collaborator

I'll close this as a duplicate of #2156.

@felix-hilden felix-hilden added the R: duplicate This issue or pull request already exists label Feb 1, 2022
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? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

5 participants