-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add rule to detect unnecessary class properties #21535
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
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF069 | 4 | 4 | 0 | 0 | 0 |
ntBre
left a comment
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.
Thanks! This looks good to me overall, just some minor nits and a suggestion about the rule name and code.
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
| use crate::{FixAvailability, Violation}; | ||
|
|
||
| /// ## What it does | ||
| /// Detects unnecessary `@property` methods. |
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.
"Unnecessary" doesn't feel like exactly the right description to me. I think something like property-without-return for the rule name, along the lines of the issue title, might make more sense.
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.
The initial name was something like that, but I decided it's no longer accurate after the inclusion of raise and yield, would you still want me to revert it to the old name?
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.
Ah, I see what you mean, but I think it's still okay for the name and diagnostic message only to mention return. @amyreese had a good point that return will probably be the most common fix when the diagnostic fires too, so it seems good to emphasize that option.
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
32c13f2 to
1a71829
Compare
ntBre
left a comment
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.
Thank you, this looks great!
I think we just need to bump the preview_since version and update the rule name/docs and this is good to go.
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/ruff/rules/unnecessary_property.rs
Outdated
Show resolved
Hide resolved
| use crate::{FixAvailability, Violation}; | ||
|
|
||
| /// ## What it does | ||
| /// Detects unnecessary `@property` methods. |
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.
Ah, I see what you mean, but I think it's still okay for the name and diagnostic message only to mention return. @amyreese had a good point that return will probably be the most common fix when the diagnostic fires too, so it seems good to emphasize that option.
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
| PropertyWithoutReturn { | ||
| name: name.to_string(), | ||
| }, | ||
| function_def.range(), |
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.
Sorry to add one more change needed. We should pick a smaller range for the diagnostic, rather than throwing a yellow squiggle on an entire function in an editor, as changing the diagnostic range later will potentially be a breaking change.
I think I'd suggest just marking the @property decorator itself, since that's the part that's unfulfilled by the body of the function.
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.
In some cases, we highlight the range of the function's name. You can see other usages by searching for references to the Identifier trait
ruff/crates/ruff_python_ast/src/identifier.rs
Lines 18 to 21 in 9ae698f
| pub trait Identifier { | |
| /// Return the [`TextRange`] of the identifier in the given AST node. | |
| fn identifier(&self) -> TextRange; | |
| } |
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.
Thank you @amyreese & @MichaReiser for the suggestions.
I agree with highlighting only one line instead of the entire function block.
I ended up highlighting only the function name (@MichaReiser suggestion), just because it's a personal preference of mine. LMK if you want me to change that to highlight something else instead
amyreese
left a comment
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.
One last nit, but otherwise looks good to me!
crates/ruff_linter/src/rules/ruff/rules/property_without_return.rs
Outdated
Show resolved
Hide resolved
…n.rs Co-authored-by: Amethyst Reese <amethyst@n7.gg>
|
Thanks this looks good. I just noticed that you recoded the rule from RUF066 to RUF069 before. Can you tell me why? It seems to me that RUF066 is still available. |
|
Oh, never mind. It's because there are other in-flight PRs using that code. I recoded it back to RUF066, because I want to avoid holes and your PR is going to land first :) |
Summary
Fixes #21403
Test Plan
Added tests