diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py index 1ea6d78e15..b77a52689d 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py @@ -206,6 +206,11 @@ if ( for user_id in set(target_user_ids) - {u.user_id for u in updates}: updates.append(UserPresenceState.default(user_id)) +# If either operator is parenthesized, use non-simple formatting. +# See: https://github.com/astral-sh/ruff/issues/7318. +10**(2) +10**2 + # Keeps parenthesized left hand sides ( log(self.price / self.strike) diff --git a/crates/ruff_python_formatter/src/expression/binary_like.rs b/crates/ruff_python_formatter/src/expression/binary_like.rs index a63fb3a21c..55e55c8037 100644 --- a/crates/ruff_python_formatter/src/expression/binary_like.rs +++ b/crates/ruff_python_formatter/src/expression/binary_like.rs @@ -466,8 +466,11 @@ impl Format> for BinaryLike<'_> { } } -const fn is_simple_power_expression(left: &Expr, right: &Expr) -> bool { - is_simple_power_operand(left) && is_simple_power_operand(right) +fn is_simple_power_expression(left: &Expr, right: &Expr, source: &str) -> bool { + is_simple_power_operand(left) + && is_simple_power_operand(right) + && !is_expression_parenthesized(left.into(), source) + && !is_expression_parenthesized(right.into(), source) } /// Return `true` if an [`Expr`] adheres to [Black's definition](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-breaks-binary-operators) @@ -661,6 +664,7 @@ impl Format> for FlatBinaryExpressionSlice<'_> { && is_simple_power_expression( left.last_operand().expression(), right.first_operand().expression(), + f.context().source(), ); if let Some(leading) = left.first_operand().leading_binary_comments() { diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap index 2e8975b248..849e9974cb 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap @@ -212,6 +212,11 @@ if ( for user_id in set(target_user_ids) - {u.user_id for u in updates}: updates.append(UserPresenceState.default(user_id)) +# If either operator is parenthesized, use non-simple formatting. +# See: https://github.com/astral-sh/ruff/issues/7318. +10**(2) +10**2 + # Keeps parenthesized left hand sides ( log(self.price / self.strike) @@ -657,6 +662,11 @@ if ( for user_id in set(target_user_ids) - {u.user_id for u in updates}: updates.append(UserPresenceState.default(user_id)) +# If either operator is parenthesized, use non-simple formatting. +# See: https://github.com/astral-sh/ruff/issues/7318. +10 ** (2) +10**2 + # Keeps parenthesized left hand sides ( log(self.price / self.strike)