Separate BitXorOr into BitXor and BitOr precedence (#16844)

## Summary

This change follows up on the bug-fix requested in #16747 --
`ruff_python_ast::OperatorPrecedence` had an enum variant, `BitXorOr`,
which which gave the same precedence to the `|` and `^` operators. This
goes against [Python's documentation for operator
precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence),
so this PR changes the code so that it's correct.

This is part of the overall effort to unify redundant definitions of
`OperatorPrecedence` throughout the codebase (#16071)

## Test Plan

Because this is an internal change, I only ran existing tests to ensure
nothing was broken.
This commit is contained in:
Junhson Jean-Baptiste 2025-03-20 06:43:47 -04:00 committed by GitHub
parent 74f64d3f96
commit 47c4ccff5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 7 deletions

View file

@ -398,7 +398,7 @@ impl DunderReplacement {
"__or__" => Some(Self::Operator(
"|",
"Use `|` operator",
OperatorPrecedence::BitXorOr,
OperatorPrecedence::BitOr,
)),
"__rshift__" => Some(Self::Operator(
">>",
@ -418,7 +418,7 @@ impl DunderReplacement {
"__xor__" => Some(Self::Operator(
"^",
"Use `^` operator",
OperatorPrecedence::BitXorOr,
OperatorPrecedence::BitXor,
)),
"__radd__" => Some(Self::ROperator(
@ -454,7 +454,7 @@ impl DunderReplacement {
"__ror__" => Some(Self::ROperator(
"|",
"Use `|` operator",
OperatorPrecedence::BitXorOr,
OperatorPrecedence::BitOr,
)),
"__rrshift__" => Some(Self::ROperator(
">>",
@ -474,7 +474,7 @@ impl DunderReplacement {
"__rxor__" => Some(Self::ROperator(
"^",
"Use `^` operator",
OperatorPrecedence::BitXorOr,
OperatorPrecedence::BitXor,
)),
"__aiter__" => Some(Self::Builtin("aiter", "Use `aiter()` builtin")),

View file

@ -28,8 +28,10 @@ pub enum OperatorPrecedence {
/// Precedence of comparisons (`<`, `<=`, `>`, `>=`, `!=`, `==`),
/// memberships (`in`, `not in`) and identity tests (`is`, `is not`).
ComparisonsMembershipIdentity,
/// Precedence of bitwise `|` and `^` operators.
BitXorOr,
/// Precedence of bitwise `|` operator.
BitOr,
/// Precedence of bitwise `^` operator.
BitXor,
/// Precedence of bitwise `&` operator.
BitAnd,
/// Precedence of left and right shift expressions (`<<`, `>>`).
@ -159,7 +161,8 @@ impl From<Operator> for OperatorPrecedence {
Operator::LShift | Operator::RShift => Self::LeftRightShift,
// Bitwise operations: &, ^, |
Operator::BitAnd => Self::BitAnd,
Operator::BitXor | Operator::BitOr => Self::BitXorOr,
Operator::BitXor => Self::BitXor,
Operator::BitOr => Self::BitOr,
// Exponentiation **
Operator::Pow => Self::Exponent,
}