-
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
[ruff] Implement incorrectly-parenthesized-tuple-in-subscript
(RUF031
)
#12480
[ruff] Implement incorrectly-parenthesized-tuple-in-subscript
(RUF031
)
#12480
Conversation
I'm not super happy with the names or descriptions for this rule and its structs/module/function/etc., so if folks have better ideas I'm all ears. |
Thanks for your contribution. @AlexWaygood what's your take on this rule? I'm somewhat concerned of adding the rule to Ruff's rule set because it is a restriction lint rule, it forbids the use of specific syntax, and not a lint rule that improves the quality of the code overall. |
Hmm. I'm not sure I'd call this a "restriction" rule since it doesn't forbid the user from using any Python idioms that they'd otherwise be able to use. The recommendations the rule makes are purely cosmetic; the code's AST doesn't change at all if you follow the rule's recommendations. I'd call it a formatting rule, similar to many of our pycodestyle rules. This rule is quite opinionated, but I can definitely see that it's useful to enforce either one style or the other. The question is what style is preferable? The code becomes less noisy without the parens, but I'm not sure it's more readable -- it requires additional knowledge of Python's semantics to know that there's an implicit tuple formed by the comma there. So I think I'd support the rule being added, but I'd make it configurable (similar to e.g. our |
Great! A good opportunity to learn about rule-specific configurations. I'll make the changes. |
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF031 | 126 | 126 | 0 | 0 | 0 |
PLR6301 | 92 | 0 | 92 | 0 | 0 |
PYI052 | 38 | 0 | 38 | 0 | 0 |
D212 | 19 | 0 | 19 | 0 | 0 |
PLC2701 | 15 | 0 | 15 | 0 | 0 |
D200 | 13 | 0 | 13 | 0 | 0 |
ANN401 | 12 | 0 | 12 | 0 | 0 |
DOC201 | 9 | 0 | 9 | 0 | 0 |
TRY003 | 9 | 0 | 9 | 0 | 0 |
EM102 | 6 | 0 | 6 | 0 | 0 |
DOC501 | 4 | 0 | 4 | 0 | 0 |
D401 | 3 | 0 | 3 | 0 | 0 |
EM101 | 3 | 0 | 3 | 0 | 0 |
PLC0415 | 3 | 0 | 3 | 0 | 0 |
TCH002 | 3 | 0 | 3 | 0 | 0 |
ARG002 | 2 | 0 | 2 | 0 | 0 |
COM812 | 2 | 0 | 2 | 0 | 0 |
E501 | 2 | 0 | 2 | 0 | 0 |
ARG004 | 2 | 0 | 2 | 0 | 0 |
PLR2004 | 2 | 0 | 2 | 0 | 0 |
D107 | 2 | 0 | 2 | 0 | 0 |
PLR0904 | 2 | 0 | 2 | 0 | 0 |
PLC1901 | 2 | 0 | 2 | 0 | 0 |
RUF022 | 1 | 0 | 1 | 0 | 0 |
D102 | 1 | 0 | 1 | 0 | 0 |
CPY001 | 1 | 0 | 1 | 0 | 0 |
RUF012 | 1 | 0 | 1 | 0 | 0 |
FBT001 | 1 | 0 | 1 | 0 | 0 |
FBT002 | 1 | 0 | 1 | 0 | 0 |
SIM102 | 1 | 0 | 1 | 0 | 0 |
ANN204 | 1 | 0 | 1 | 0 | 0 |
E721 | 1 | 0 | 1 | 0 | 0 |
B905 | 1 | 0 | 1 | 0 | 0 |
DOC402 | 1 | 0 | 1 | 0 | 0 |
ERA001 | 1 | 0 | 1 | 0 | 0 |
TCH003 | 1 | 0 | 1 | 0 | 0 |
UP035 | 1 | 0 | 1 | 0 | 0 |
Any chance a reviewer could check whether the new They appear in the json schema but not in the output of If it matters, this is the first example of a "plugin" configuration being added to |
Ok, should be good to go! |
parentheses-in-tuple-slices
(RUF031
)bad-format-tuple-in-getitem
(RUF031
)
It doesn't restrict the use of any python idoms but it does restrict the allowed syntax. Any use of subscript > tuple is restricted.
Fair enough
I'm sorry to raise this so late. I'm concerned about this option because it limits the formatter's style guide options in the long term. Our formatter already removes unnecessary parentheses in some places, and I could see it removing more parentheses in the long term. That's why I would prefer not support the option and merge the rule without. |
I'm concerned that the implied definition of a "restriction" rule here is quite broad. Almost every rule that we have "restricts" you from doing something that the rule deems bad in terms of correctness, style or formatting :-) But this is getting off-topic.
Hmm, I see. To me it feels very unclear which style is preferable, which is why I think this makes sense as a configurable linter rule rather than something the formatter should have an opinion on. But if we think this is something the formatter might want to take an opinion on in the future, then I suppose I'm not sure that this is a linter rule we should have at all, then -- perhaps we should simply teach the formatter to have an opinion on this. |
It doesn't seem that we feel comfortable making that decision today. You explicitly state that you are unclear about what style you prefer. I wonder if the goal of the rule is to assert consistent formatting or if it is really about specifically restricting parenthesized tuples in subscriptions. What I hear is mainly that some people don't want to have parenthesized tuples in subscripts and less so that they want to enforce parentheses everywhere. Specifically restricting the use of parenthesized tuples also helps to give the rule a better name: |
Yep. That's why I don't think we should teach the formatter to have an opinion on this (ever), and why I would prefer a configurable linter rule ;-) Anyway, I've said my piece on this. For me, I can see it's useful to enforce a consistent style across a codebase, but I think the rule is too opinionated -- as either something to be incorporated in the formatter or a linter rule -- unless it can be made configurable. I think we're going round in circles now, so I'll bow out at this point :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlexWaygood and I discussed this offline and I now agree that this isn't something the formatter should be opinionated about. Therefore, the lint rule as is is fine :)
I would prefer to change the name: bad
isn't very telling. How about incorrectly-parenthesized-tuple-in-subscript
or incorrectly-parenthesized-tuple-in-getitem
?
For the setting, I think I would go with parenthesize-tuple-in-getitem
to match the rule name.
Great! I've made the requested changes. Let me know if there's anything else, and thanks for the discussion! |
bad-format-tuple-in-getitem
(RUF031
)incorrectly-parenthesized-tuple-in-getitem
(RUF031
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for working on this. I left a few smaller nits and we may want to change the wording of the rule (@AlexWaygood )
// In case we want to change the boolean setting for | ||
// this rule to an enum, this will make the code change | ||
// just a little simpler. | ||
#![allow(clippy::match_bool)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use an if...else
over a match instead of suppressing the clippy warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Sometimes in this kind of situation it can be more readable to introduce a custom enum and match on that instead, e.g.
enum ParenthesesPreference {
Keep,
Remove,
}
but here I'm not sure I really see the need; I think I'd just stick with bool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this has been marked as resolved, but the comment hasn't been addressed
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
Looks like you might have done a bad rebase there @dylwil3 -- the diff for this PR is now |
incorrectly-parenthesized-tuple-in-getitem
(RUF031
)incorrectly-parenthesized-tuple-in-subscript
(RUF031
)
Co-authored-by: Micha Reiser <[email protected]>
Co-authored-by: Micha Reiser <[email protected]>
858bf05
to
767ae99
Compare
@dylwil3, I fixed up your PR history for you. Before pushing any further updates to this PR, you'll now need to run the following commands locally in order to fetch your PR branch as it exists on GitHub (assuming your GitHub remote is called
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nearly there!
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_getitem.rs
Outdated
Show resolved
Hide resolved
// In case we want to change the boolean setting for | ||
// this rule to an enum, this will make the code change | ||
// just a little simpler. | ||
#![allow(clippy::match_bool)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this has been marked as resolved, but the comment hasn't been addressed
Thanks for your help cleaning up my mess 😄 Sorry for the last minute trouble and let me know if you find any other issues! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I'll apply my final suggestions and then merge
crates/ruff_linter/resources/test/fixtures/ruff/RUF031_prefer_parens.py
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/incorrectly_parenthesized_tuple_in_subscript.rs
Outdated
Show resolved
Hide resolved
ba50822
to
e165fa5
Compare
e165fa5
to
4ba055d
Compare
Thanks for the PR @dylwil3 and for your patience -- this was a great contribution! |
Many thanks to y'all, I'm thrilled this is now a thing ! |
Summary
Implements the new fixable lint rule RUF031 which checks for the use or omission of parentheses around tuples in subscripts, depending on the setting
lint.ruff.parenthesize-tuple-in-subscript
. By default, the use of parentheses is considered a violation.For example, if
d
is a dictionary thenbecomes
This closes #11990 and introduces configurations/options/settings for RUF rules.
Test Plan
Snapshot verified,
cargo test
passed, docs generated and checked that rule appeared.