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

Odd formatting choice for long conditions with comments in list/dict comprehensions #12280

Closed
goodspark opened this issue Jul 10, 2024 · 0 comments · Fixed by #12282
Closed
Labels
bug Something isn't working formatter Related to the formatter

Comments

@goodspark
Copy link

goodspark commented Jul 10, 2024

Previously used search terms: format for loop if comment

Code:

x = [1, 2, 3]
y = [
    a
    for a in x 
    if (
        # asdasd
        "askldaklsdnmklasmdlkasmdlkasmdlkasmdasd" != "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
        and "zxcm,.nzxclm,zxnckmnzxckmnzxczxc" != "zxcasdasdlmnasdlknaslkdnmlaskdm"
    )
]

Comparison of Black (previous) vs Ruff (new) outputs:

 y = [
     a
     for a in x
-    if (
+    if
+    (
         # asdasd
         "askldaklsdnmklasmdlkasmdlkasmdlkasmdasd"
         != "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
Ruff's output
x = [1, 2, 3]
y = [
    a
    for a in x
    if
    (
        # asdasd
        "askldaklsdnmklasmdlkasmdlkasmdlkasmdasd"
        != "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
        and "zxcm,.nzxclm,zxnckmnzxckmnzxczxc" != "zxcasdasdlmnasdlknaslkdnmlaskdm"
    )
]
Black's output
x = [1, 2, 3]
y = [
    a
    for a in x
    if (
        # asdasd
        "askldaklsdnmklasmdlkasmdlkasmdlkasmdasd"
        != "as,mdnaskldmlkasdmlaksdmlkasdlkasdm"
        and "zxcm,.nzxclm,zxnckmnzxckmnzxczxc" != "zxcasdasdlmnasdlknaslkdnmlaskdm"
    )
]

If I move the comment out of the inner parentheses of the loop condition, the outputs match:

    for a in x 
    # asdasd
    if (

Command: ruff format

Ruff settings: None for formatting, but target-python is given at the project level, if that matters.

[project]
target-python = "==3.10"

Python version: 3.10
Ruff version: 0.5.0
Black version: 24.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
2 participants