-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 collapses parenthesized if-expression #7066
Comments
Oh wow I called it a comprehension 🙈 lol. Thanks! |
Oh no prob :) It might also apply to comprehensions, I’m not sure, but I renamed to reflect the example. |
I don't fully understand Black's rules here (some examples -- e.g., the first vs. second case is odd and likely a bug given that they drop the comment entirely). The preview style is also quite different. It might be sufficient to expand the parent if the diff --git a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs
index 1b22fb568..dec9b7d5f 100644
--- a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs
+++ b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs
@@ -53,9 +53,13 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
let comments = f.context().comments().clone();
let inner = format_with(|f: &mut PyFormatter| {
- // We place `if test` and `else orelse` on a single line, so the `test` and `orelse` leading
- // comments go on the line before the `if` or `else` instead of directly ahead `test` or
- // `orelse`
+ if comments.has_leading(item) || comments.has_trailing(item) {
+ expand_parent().fmt(f)?;
+ } |
It would be nice to map this behavior to code in Black to better understand the rules. |
We decided to accept this as a deviation: #7082 |
Somewhat related: #6588
Line-length: 88
IMO Ruff's output is better. Reporting anyway.
Black Playground
Ruff Playground
The text was updated successfully, but these errors were encountered: