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

Incorrect line breaking with expressions that use parenthesis #2508

Closed
bruceleban opened this issue Sep 25, 2021 · 1 comment
Closed

Incorrect line breaking with expressions that use parenthesis #2508

bruceleban opened this issue Sep 25, 2021 · 1 comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?

Comments

@bruceleban
Copy link

Describe the bug

Black does not correctly break lines in some cases. In the first two of these examples below it incorrectly puts foo( at the end of a line instead of breaking at the or binary operator as it should. In the other three cases, where I've added a third or clause, it breaks correctly.

This code

if (
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    or foo(1, 2, 3, 4, 5, 6)
):
    pass

is incorrectly formatted as

if foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) or foo(
    1, 2, 3, 4, 5, 6
):
    pass

To Reproduce

Using https://black.vercel.app/ enter this code:

# These first two are incorrectly formatted.
if (
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    or foo(1, 2, 3, 4, 5, 6)
):
    pass
if (
    foo(1, 2, 3, 4, 5, 6)
    or foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
):
    pass
# These next three are correctly formatted.
if (
    foo(1)
    or foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    or foo(1, 2, 3, 4, 5, 6)
):
    pass
if (
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    or foo(1, 2, 3, 4, 5, 6)
    or foo(1)
):
    pass
if (
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    or foo(1)
    or foo(1, 2, 3, 4, 5, 6)
):
    pass
# These next two examples show it is not simply a line length issue.
# It removes the outer parentheses because it all fits on one line (as documented).
if foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5) or foo(1) or foo(1, 2, 3, 4, 5, 6):
    pass
# This case is three characters longer and no longer fits on one line
# so it breaks it -- in this case correctly.
if (
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6)
    or foo(1)
    or foo(1, 2, 3, 4, 5, 6)
):
    pass

Expected behavior

All of these should follow PEP 8 and break before the or operator.

Environment (please complete the following information):

Does this bug also happen on main?

Yes. main == @06ccb8 at the time I submitted this.

@bruceleban bruceleban added the T: bug Something isn't working label Sep 25, 2021
@felix-hilden
Copy link
Collaborator

Hi! This is known and discussed in length in #1811 and #2156, so I'll close this as a duplicate.

@felix-hilden felix-hilden added R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like? and removed T: bug Something isn't working labels Sep 25, 2021
@ichard26 ichard26 added the F: parentheses Too many parentheses, not enough parentheses, and so on. label Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. 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

3 participants