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

Formatter can insert mysterious extraneous parentheses in with statements #14001

Open
lpulley opened this issue Oct 30, 2024 · 12 comments · Fixed by #14005
Open

Formatter can insert mysterious extraneous parentheses in with statements #14001

lpulley opened this issue Oct 30, 2024 · 12 comments · Fixed by #14005
Assignees
Labels
bug Something isn't working formatter Related to the formatter
Milestone

Comments

@lpulley
Copy link

lpulley commented Oct 30, 2024

My ruff --version is 0.7.1.

I've encountered a case where ruff format adds nesting parentheses for seemingly no reason:

with (
    open(
        "some/path.txt",
        "rb",
    )
    if True
    else open("other/path.txt")
    # Bar
):
    pass
➜ ruff format --diff up034.py
--- up034.py
+++ up034.py
@@ -1,10 +1,12 @@
 with (
-    open(
-        "some/path.txt",
-        "rb",
+    (
+        open(
+            "some/path.txt",
+            "rb",
+        )
+        if True
+        else open("other/path.txt")
     )
-    if True
-    else open("other/path.txt")
     # Bar
 ):
     pass

1 file would be reformatted

i.e. it wants this:

with (
    (
        open(
            "some/path.txt",
            "rb",
        )
        if True
        else open("other/path.txt")
    )
    # Bar
):
    pass

UP034 complains about this, for good reason.

If the # Bar comment is removed, the formatter makes no changes. More curiously, if I keep # Bar but add as _, the formatter makes no changes. (I also checked that Black does not do this.)

I've resolved the issue in the meantime by moving the # Bar comment to a different line, but this seems like a formatter bug.

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Oct 30, 2024
@MichaReiser MichaReiser self-assigned this Oct 30, 2024
@MichaReiser
Copy link
Member

Thanks for reporting this. Agree, this looks silly but I'm a bit scared from fixing it... with item formatting is complicated.

@lpulley
Copy link
Author

lpulley commented Nov 4, 2024

Hmm... with Ruff 0.7.2, the example I gave above is still problematic; it still wants to add extra ( ) and fail UP034.

@MichaReiser
Copy link
Member

Hmm... with Ruff 0.7.2, the example I gave above is still problematic; it still wants to add extra ( ) and fail UP034.

Yeah, we can't fix it before promoting the new style to stable. You would have to use format.preview = true for now

@lpulley
Copy link
Author

lpulley commented Nov 4, 2024 via email

@MichaReiser
Copy link
Member

MichaReiser commented Nov 4, 2024

You can subscribe to #13371 to get notified when the preview style gets stabilized.

@lpulley
Copy link
Author

lpulley commented Jan 9, 2025

I'm trying out 0.9.0 and still seeing this behavior. Did this make it into the 2025 style?

@MichaReiser
Copy link
Member

Hmmm, no. That's on me.

@lpulley
Copy link
Author

lpulley commented Jan 9, 2025

Not the end of the world; just wanted to make sure I wasn't missing anything obvious. Thanks.

@MichaReiser
Copy link
Member

Thanks for being so understanding and sorry that I missed that one last preview style

@cj81499
Copy link

cj81499 commented Jan 9, 2025

Hey @MichaReiser, I'm one of @lpulley's coworkers. Will this make it into 0.9.1? Or will we be waiting till the 2026 style?

fwiw, we've been super happy with ruff and this will not change that :) Thanks for helping to build such an awesome piece of software!

@MichaReiser
Copy link
Member

Thanks for the kind words.

We might want to pull it into 0.10, considering that changes should be super rare. I don't feel comfortable shipping it as part of a patch release because it would violate our versioning policy

@MichaReiser MichaReiser reopened this Jan 10, 2025
@MichaReiser MichaReiser added this to the v0.10 milestone Jan 10, 2025
@cj81499
Copy link

cj81499 commented Jan 10, 2025

Sounds good to me, thank you!

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
Development

Successfully merging a pull request may close this issue.

3 participants