-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[ruff
] Implement post-init-default (RUF033
)
#13192
Conversation
|
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 for reviewing a draft PR... couldn't resist! Looks great.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
CodSpeed Performance ReportMerging #13192 will improve performances by 5.89%Comparing Summary
Benchmarks breakdown
|
RUF033
)ruff
] Implement post-init-default (RUF033
)
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! I pushed a couple of simplifications, but this was overall an excellent PR
} | ||
|
||
/// RUF033 | ||
pub(crate) fn post_init_default(checker: &mut Checker, function_def: &ast::StmtFunctionDef) { |
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 made this run on StmtFunctionDef
nodes rather than StmtClassDef
nodes, as this allows us to access checker.semantic().current_scope()
to see what other symbols have been bound in the class. This enables us to see much more simply and more accurately whether it's safe to add a new InitVar
field to the class body in the fix
let initvar_edit = Edit::insertion( | ||
content.into_owned(), | ||
locator.line_start(post_init_def.start()), | ||
); |
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.
Rather than iterate through all statements in the class body until we get the first non-docstring statement, I changed it so that we just insert the InitVar
symbol in the line before the __post_init__
definition. It seems much simpler and less error-prone as we already know that we're past the docstring in this case
Summary
Implements the post-init-default (
RUF033
) rule, which reports a diagnostic upon any__post_init__
method with argument default. Has a sometimes available unsafe autofix. For example,Closes #13128
Test Plan
cargo nextest run