Improve pow operator spacing (#2970)

Ensure that we add spaces to expressions like `foo.bar() ** 2`.
This commit is contained in:
Charlie Marsh 2023-02-16 15:17:32 -05:00 committed by GitHub
parent 1c01ec21cb
commit 5157f584ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 4 deletions

View file

@ -718,8 +718,7 @@ fn format_bin_op(
right: &Expr,
) -> FormatResult<()> {
// https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-breaks-binary-operators
let is_simple =
matches!(op, Operator::Pow) && (is_simple_power(left) && is_simple_power(right));
let is_simple = matches!(op, Operator::Pow) && is_simple_power(left) && is_simple_power(right);
write!(f, [left.format()])?;
if !is_simple {
@ -788,7 +787,7 @@ fn format_lambda(
body: &Expr,
) -> FormatResult<()> {
write!(f, [text("lambda")])?;
if !args.args.is_empty() {
if !args.args.is_empty() || args.kwarg.is_some() || args.vararg.is_some() {
write!(f, [space()])?;
write!(f, [args.format()])?;
}

View file

@ -81,7 +81,7 @@ pub fn is_simple_power(expr: &Expr) -> bool {
}
ExprKind::Constant { .. } => true,
ExprKind::Name { .. } => true,
ExprKind::Attribute { .. } => true,
ExprKind::Attribute { value, .. } => is_simple_power(value),
_ => false,
}
}