-
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
Introduce an Arguments
AST node for function calls and class definitions
#6259
Conversation
args, | ||
keywords, | ||
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.
I tended to favor destructuring to minimize code changes (i.e., we didn't have to change this method at all, only the let
match here).
decorator.expression.range(), | ||
diagnostic.set_fix(Fix::automatic(Edit::deletion( | ||
arguments.start(), | ||
arguments.end(), |
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.
This is an improvement (delete from left to right parens).
diagnostic.set_fix(Fix::automatic(Edit::deletion( | ||
arguments.start(), | ||
arguments.end(), | ||
))); |
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.
This is way simpler (delete from left to right paren).
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
e628cb0
to
0f87e9b
Compare
(I am going to leverage these in the formatter in a separate change, to fix the trailing comment bug we have today.) |
0f87e9b
to
6bd209c
Compare
crates/ruff/src/rules/flake8_builtins/rules/builtin_attribute_shadowing.rs
Outdated
Show resolved
Hide resolved
6bd209c
to
09fa5b0
Compare
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.
skimmed through it
## Summary Similar to #6259, this PR adds a `TypeParams` node to the AST, to capture the list of type parameters with their surrounding brackets. If a statement lacks type parameters, the `type_params` field will be `None`.
## Summary This PR leverages the `Arguments` AST node introduced in #6259 in the formatter, which ensures that we correctly handle trailing comments in calls, like: ```python f( 1, # comment ) pass ``` (Previously, this was treated as a leading comment on `pass`.) This also allows us to unify the argument handling across calls and class definitions. ## Test Plan A bunch of new fixture tests, plus improved Black compatibility.
pub fn bases(&self) -> impl Iterator<Item = &Expr> { | ||
self.arguments | ||
.as_ref() | ||
.map(|arguments| &arguments.args) | ||
.into_iter() | ||
.flatten() | ||
} |
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.
Nit: We could return a slice
here by using
pub fn bases(&self) -> impl Iterator<Item = &Expr> { | |
self.arguments | |
.as_ref() | |
.map(|arguments| &arguments.args) | |
.into_iter() | |
.flatten() | |
} | |
pub fn bases(&self) -> &[Expr] { | |
match &self.arguments { | |
Some(arguments) => &arguments.args, | |
None => &[], | |
} | |
} |
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.
Good call.
@@ -2951,9 +3003,9 @@ mod size_assertions { | |||
use super::*; | |||
use static_assertions::assert_eq_size; | |||
|
|||
assert_eq_size!(Stmt, [u8; 168]); | |||
assert_eq_size!(Stmt, [u8; 176]); |
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.
👀
Slices are strictly more flexible, since you can always convert to an iterator, etc., but not the other way around. Suggested in #6259 (comment).
Summary
This PR adds a new
Arguments
AST node, which we can use for function calls and class definitions.The
Arguments
node spans from the left (open) to right (close) parentheses inclusive.In the case of classes, the
Arguments
is an option, to differentiate between:In this PR, we don't really leverage this change (except that a few rules get much simpler, since we don't need to lex to find the start and end ranges of the parentheses, e.g.,
crates/ruff/src/rules/pyupgrade/rules/lru_cache_without_parameters.rs
,crates/ruff/src/rules/pyupgrade/rules/unnecessary_class_parentheses.rs
).In future PRs, this will be especially helpful for the formatter, since we can track comments enclosed on the node itself.
Test Plan
cargo test