-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify OperatorPrecedence
enums
#16071
Comments
I can take this on, I have a few questions though
|
This needs some good design. The fix results in #15919 weren't as nice as I'd want them to be. I found no easy way to avoid parenthesizing when unnecessary: 2 ** -2 # Intended result: 0.25
# ^^^^^^^ This doesn't need parentheses
-2 ** 2 # Intended result: 4
# ^^ This does The newest # This entire subscript resolves to the precedence of the subscript operator,
# while its slice resolves to that of the unary `-`.
lorem[-ipsum]
# `[]` binds more tightly than `-`,
# so a simple precedence comparison would result in the expected output for this case,
# where `-` is supposed to apply to `lorem` alone:
(-lorem)[ipsum] # Necessary
# On the other hand, the slice expression doesn't need parenthesizing.
# The comparison result alone isn't enough to avoid this:
lorem[(-ipsum)] # Unnecessary A more complex example: (a + 1)(
# ^^^^^^^ This needs extra parentheses, as binary `+` binds less tightly than `()`
a + 1,
# ^^^^^ This doesn't, as it is an argument
b := 0,
# ^^^^^^ This also doesn't; bare named expressions can be passed positionally.
c = (d := -1)
# ^^^^^^^^^ But this does.
) |
Just sent out a PR. I wrote most of the code before @InSyncWithFoo's comment. It doesn't attempt to address the problems with parenthesizing yet but I think may still be a good step towards unifying the many definitions of |
I can get started on the second PR for this issue soon. Should be interesting to see how nicely the other versions of In regards to the parenthesizing issue, I think that should either be a third PR on this issue or a separate issue -- would you agree @InSyncWithFoo? To be honest, I have no idea how to even approach the problem yet but it seems like a really cool learning opportunity, especially if I can be pointed in the right direction 🙂 |
@junhsonjb The parenthesizing problem is indeed big and deserves its own PR. That said, I also don't have a good idea of what the logic should be. |
Description
There are now 3
OperatorPrecedence
structs in ruffOperatorPrecedence
struct into ruff-python-ast and expose aprecedence()
method onExpr
(and `ExpressionRef) and ideally on each Expression node.OperatorPrecedence
structs with the one now defined in the AST crateIdeally, these would be two separate PRs
The text was updated successfully, but these errors were encountered: