mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 03:49:28 +00:00
Force parentheses for power operations in unary expressions (#7955)
## Summary E.g., given `-10**100`, reformat as `-(10**100)`. Black special cases this (https://github.com/psf/black/pull/909) and it's currently a deviation. Closes https://github.com/astral-sh/ruff/issues/7951.
This commit is contained in:
parent
b6e75e58c9
commit
3d03e75a9d
2 changed files with 10 additions and 12 deletions
|
@ -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::*;
|
||||
|
||||
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue