-
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
Add convenience methods for iterating over all parameter nodes in a function #11174
Add convenience methods for iterating over all parameter nodes in a function #11174
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.
I like this a lot. I did not review all code changes. But we should have done this a long time ago :D
if parameters | ||
.posonlyargs | ||
.iter() | ||
.chain(¶meters.args) | ||
.chain(¶meters.kwonlyargs) | ||
.count() | ||
> 1 |
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 advantage of defining your own iterator type (vs using chaining) is that you could implement ExactSizeIterator
and you could then call .len()
here.
But I think the better solution here is to add a method to parameters
to get this length
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.
But I think the better solution here is to add a method to
parameters
to get this length
Currently the rule is doing something a little odd: it's checking how many nonvariadic parameters there are in this function, but paying no attention to how many variadic parameters there are. That makes no sense to me based on what the rule is supposed to be checking: I think it makes just as much sense to emit an error if an @property
-decorated function has *args
or **kwargs
parameters, as that's just as invalid.
I think maybe I'll just revert all changes to this file for now, and fix the bug (at least, I think it's a bug) in a separate PR, with added tests.
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.
Followup PR at #11200
3792179
to
8ace18c
Compare
2010e4d
to
0323a78
Compare
Summary
A pretty common pattern in our linter rules is the need to iterate over all parameters in a function. That ends up looking pretty verbose currently -- something like this:
It's also pretty easy to get this pattern subtly incorrect: for example, several of our rules currently incorrectly do
parameters.args.iter().chain(¶meters.posonlyargs)
, but positional-only parameters always precede positional-or-keyword parameters in Python.This PR adds two convenience methods to the
ast::Parameters
struct inruff_python_ast/src/nodes.rs
, to make iterating over all parameters more ergonomic.iter_non_variadic_params()
iterates over just the non-variadic parameters (posonlyargs
,args
andkwonlyargs
), anditer_all_params()
iterates over all parameters in a function, including any variadic paramerers (vararg
andkwarg
). Using these convenience methods in our linter rules significantly reduces duplication.Test Plan
cargo test
. One parser snapshot changes slightly, but I think it's for the better: the syntax errors are now reported in the order that they appear in the source code. Previously, they were reported out of order.