[internal] Use ruff_python_ast::OperatorPrecedence
in Parser (ruff_python_parser
)
#16747
+81
−157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This change continues to resolve #16071 (and continues the work started in #16162). Specifically, this PR changes the code in the parser so that it uses the
OperatorPrecedence
struct fromruff_python_ast
instead of its own version. This is part of an effort to get rid of the redundant definitions ofOperatorPrecedence
throughout the codebase.Note that this PR only makes this change for
ruff_python_parser
-- we still want to make a similar change for the formatter (namely theOperatorPrecedence
defined in the expression part of the formatter, the pattern one is different). I separated the work to keep the PRs small and easily reviewable.Test Plan
Because this is an internal change, I didn't add any additional tests. Existing tests do pass.
It's worth noting however, that this change did have a slight impact on snapshot results. The removed defintion of
OperatorPrecedence
had separate enum variantsBitOr
andBitXor
. This differs from the newer version ofOperatorPrecedence
which keeps both operators on the same level withBitXorOr
. As expected, this resulted in a slightly different snapshot. After inspecting both the old and new versions, however, I think that the new one is acceptable. Simply put, the old snapshot prioritized aBitXor
operation over aBitOr
and the new snapshot does not.The snapshot only had to be updated for the following expression:
The old snapshot resolved to a precedence of:
which ultimately boils down to an expression of the form
A | B
where A =1
and B =((2 & 3) ^ ((4 + (5 @ 6)) << (7 // 8)) >> 9)
.Meanwhile, the new snapshot resolved to a precedence of:
which boils down to an expression of the form
A ^ B
where A =(1 | (2 & 3))
and B =(((4 + (5 @ 6)) << (7 // 8)) >> 9)
.And since the new
OperatorPrecedence
states that^
and|
have the same precedence,BitXorOr
, both of the resulting expressions have equal precedence. It was based on this logic that I approved the new snapshot.All other tests passed.