-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
New rule suggestion: Force parentheses in A or B and C
#8721
Comments
I like it. |
Would it be possible for this rule to also lint against the use of compound EDIT: Actually, this might be better as a different rule due to the AST differences. |
(I'm having a go at implementing this.) How should this rule interact with the My suggestion is that we do the following -- what do we think?
|
Hmm, considering the
I really like what Prettier is doing in the JS ecosystem by normalizing parentheses for all code. Unfortunately, our formatter can't do this because Python supports operator overloading, and the overloads may not adhere to the standard operator-semantic (making it unsafe to add or remove parentheses). It would be nice to have a lint rule that establishes a good standard around parenthesizing binary expressions, but that significantly increases this issue's scope. |
I agree with keeping the scope narrow for now. The reason why I think |
I would prefer to keep
I don't think Ruff should flag any of these. |
Alright, those are pretty persuasive. I'll leave |
That might be a bit off-topic, but I felt like I need to say it, as you might be teaching others how to read a code like this without parentheses: |
When I see code like
A or B and C
, I never know immediately whether it means(A or B) and C
orA or (B and C)
. (The answer is the latter;and
has higher precedence thanor
.) I would suggest adding a lint rule that triggers if anand
operation contains an unparenthesizedor
, or if anor
contains an unparenthesizedand
.I considered implementing this in flake8-bugbear or Black, but this rule can't be implemented on the AST, so it's hard to do in flake8-bugbear. It could be done in Black, but I would like to be able to see violations of this rule explicitly listed so I can check for possible bugs, and that's easier to do in a linter.
The text was updated successfully, but these errors were encountered: