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

Force parentheses for power operations in unary expressions #7955

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_unary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_python_ast::UnaryOp;

use crate::comments::{trailing_comments, SourceComment};
use crate::expression::parentheses::{
is_expression_parenthesized, NeedsParentheses, OptionalParentheses,
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
};
use crate::prelude::*;

Expand Down Expand Up @@ -57,7 +57,14 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
space().fmt(f)?;
}

operand.format().fmt(f)
if operand
.as_bin_op_expr()
.is_some_and(|bin_op| bin_op.op.is_pow())
{
operand.format().with_options(Parentheses::Always).fmt(f)
} else {
operand.format().fmt(f)
}
}

fn fmt_dangling_comments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,6 @@ last_call()
```diff
--- Black
+++ Ruff
@@ -31,7 +31,7 @@
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
-+(really ** -(confusing ** ~(operator**-precedence)))
++really ** -confusing ** ~operator**-precedence
flags & ~select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
@@ -115,7 +115,7 @@
arg,
another,
Expand Down Expand Up @@ -322,7 +313,7 @@ not great
-1
~int and not v1 ^ 123 + v2 | True
(~int) and (not ((v1 ^ (123 + v2)) | True))
+really ** -confusing ** ~operator**-precedence
+(really ** -(confusing ** ~(operator**-precedence)))
flags & ~select.EPOLLIN and waiters.write_task is not None
lambda arg: None
lambda a=True: a
Expand Down
Loading