Skip to content

Commit 47c4ccf

Browse files
authored
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.
1 parent 74f64d3 commit 47c4ccf

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

crates/ruff_linter/src/rules/pylint/rules/unnecessary_dunder_call.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl DunderReplacement {
398398
"__or__" => Some(Self::Operator(
399399
"|",
400400
"Use `|` operator",
401-
OperatorPrecedence::BitXorOr,
401+
OperatorPrecedence::BitOr,
402402
)),
403403
"__rshift__" => Some(Self::Operator(
404404
">>",
@@ -418,7 +418,7 @@ impl DunderReplacement {
418418
"__xor__" => Some(Self::Operator(
419419
"^",
420420
"Use `^` operator",
421-
OperatorPrecedence::BitXorOr,
421+
OperatorPrecedence::BitXor,
422422
)),
423423

424424
"__radd__" => Some(Self::ROperator(
@@ -454,7 +454,7 @@ impl DunderReplacement {
454454
"__ror__" => Some(Self::ROperator(
455455
"|",
456456
"Use `|` operator",
457-
OperatorPrecedence::BitXorOr,
457+
OperatorPrecedence::BitOr,
458458
)),
459459
"__rrshift__" => Some(Self::ROperator(
460460
">>",
@@ -474,7 +474,7 @@ impl DunderReplacement {
474474
"__rxor__" => Some(Self::ROperator(
475475
"^",
476476
"Use `^` operator",
477-
OperatorPrecedence::BitXorOr,
477+
OperatorPrecedence::BitXor,
478478
)),
479479

480480
"__aiter__" => Some(Self::Builtin("aiter", "Use `aiter()` builtin")),

crates/ruff_python_ast/src/operator_precedence.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ pub enum OperatorPrecedence {
2828
/// Precedence of comparisons (`<`, `<=`, `>`, `>=`, `!=`, `==`),
2929
/// memberships (`in`, `not in`) and identity tests (`is`, `is not`).
3030
ComparisonsMembershipIdentity,
31-
/// Precedence of bitwise `|` and `^` operators.
32-
BitXorOr,
31+
/// Precedence of bitwise `|` operator.
32+
BitOr,
33+
/// Precedence of bitwise `^` operator.
34+
BitXor,
3335
/// Precedence of bitwise `&` operator.
3436
BitAnd,
3537
/// Precedence of left and right shift expressions (`<<`, `>>`).
@@ -159,7 +161,8 @@ impl From<Operator> for OperatorPrecedence {
159161
Operator::LShift | Operator::RShift => Self::LeftRightShift,
160162
// Bitwise operations: &, ^, |
161163
Operator::BitAnd => Self::BitAnd,
162-
Operator::BitXor | Operator::BitOr => Self::BitXorOr,
164+
Operator::BitXor => Self::BitXor,
165+
Operator::BitOr => Self::BitOr,
163166
// Exponentiation **
164167
Operator::Pow => Self::Exponent,
165168
}

0 commit comments

Comments
 (0)