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

Expected Line break to be put before logical operators, even when some parentheses are added #2738

Closed
nicolas-chan-42 opened this issue Dec 31, 2021 · 2 comments
Labels
F: linebreak How should we split up lines? 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

@nicolas-chan-42
Copy link

Black v21.12b0

Playground link

Options

--line-length=88
--safe

Input

# Long if-statement with 2 parentheses.
if (currency == "JPY" and 100_000 <= x <= 500_000) or (currency == "USD" and 1_000 <= x <= 5_000):
    print("Line breaks AFTER Logical operator `or`.")

Output

# Long if-statement with 2 parentheses.
if (currency == "JPY" and 100_000 <= x <= 500_000) or (
    currency == "USD" and 1_000 <= x <= 5_000
):
    print("Line breaks AFTER Logical operator `or`.")

Expected

The logical operator (or in this case) would be put in the front of the second line of conditionals after formatting.
For example,

# Long if-statement with 2 parentheses.
if (
    (currency == "JPY" and 100_000 <= x <= 500_000)
    or (currency == "USD" and 1_000 <= x <= 5_000)
):
    print("Line breaks AFTER Logical operator `or`.")
@nicolas-chan-42
Copy link
Author

The following are two examples to supplement my original example for a more comprehensive look of how Black currently handles long if-statements with parentheses added by the programmer to group related conditions.

Black v21.12b0

Playground link

Options

--line-length=88
--safe

Input

# Long if-statement with 2 parentheses, with some long names.
if (currency == "JPY" and 100_000 <= somelonglonglonglonglonglonglonglonglonglonglongname <= 500_000) or (currency == "USD" and 1_000 <= somelonglonglonglonglonglonglonglonglonglonglongname <= 5_000):
    pass

# When there are 3 groups of parentheses.
if (currency == "JPY" and 100_000 <= x <= 500_000) or (currency == "USD" and 1_000 <= x <= 5_000) or (currency == "EUR" and 1_000 <= x <= 5_000):
    pass

Output

# Long if-statement with 2 parentheses, with some long names.
if (
    currency == "JPY"
    and 100_000 <= somelonglonglonglonglonglonglonglonglonglonglongname <= 500_000
) or (
    currency == "USD"
    and 1_000 <= somelonglonglonglonglonglonglonglonglonglonglongname <= 5_000
):
    pass

# When there are 3 groups of parentheses.
if (
    (currency == "JPY" and 100_000 <= x <= 500_000)
    or (currency == "USD" and 1_000 <= x <= 5_000)
    or (currency == "EUR" and 1_000 <= x <= 5_000)
):
    pass

@nicolas-chan-42 nicolas-chan-42 changed the title Expected Line break to be put before logical operators, even with parentheses added Expected Line break to be put before logical operators, even when some parentheses are added Dec 31, 2021
@ichard26 ichard26 added F: linebreak How should we split up lines? F: parentheses Too many parentheses, not enough parentheses, and so on. T: style What do we want Blackened code to look like? labels Dec 31, 2021
@felix-hilden
Copy link
Collaborator

Hi, this is very similar to #2156 and other issues linked to it. I'll close this and let's continue discussion there!

@felix-hilden felix-hilden added the R: duplicate This issue or pull request already exists label Jan 2, 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? 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