-
Notifications
You must be signed in to change notification settings - Fork 760
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
Fix the interpretation of vararg separator #792
Conversation
Thanks for the quick fix! |
tests/test_methods.rs
Outdated
@@ -231,11 +231,15 @@ impl MethArgs { | |||
[a.to_object(py), args.into(), kwargs.to_object(py)].to_object(py) | |||
} | |||
|
|||
#[args("*", c = 10)] |
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.
Should you also add a test for args(a, "*", c=10)
? (or whatever the correct syntax is for a required argument, I'm not sure).
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.
Now this allows args(a, "*", c=10)
, args(a, b, "*", c=10)
, and args("*", c=10)
, which seems confusing.
So it may be good to restrict the syntax 🤔
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 don't think it's a good idea to restrict things, and in fact we should also add support for positional-only arguments (in a separate change, IMO). The full spec for a python function definition can be found here: https://docs.python.org/3/tutorial/controlflow.html#special-parameters
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
----------- ---------- ----------
| | |
| Positional or keyword |
| - Keyword only
-- Positional only
That doesn't include *args
and **kwargs
.
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.
Yeah, but our proc-macro can do somewhat more flexible things like
#[args(a, "*", d=10)]
fn get_pos_arg_kw_sep(&self, a: i32, b: i32, c: i32) -> PyResult<i32>
, which are really confusing.
And it looks like now we allow #[args("*", d)]
, which looks like a bug.
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.
If we're working on this, should we also change to accept *
instead of "*"
? e.g. #[args(a, *, d=10)]
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.
(Maybe we should note these down each as separate bug 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.
syn doesn't accept it.
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 could be wrong, can't it parse it as syn::Token![*]
?
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 pushed a change that raises a compile error for |
Ok(()) | ||
} | ||
|
||
fn add_nv_common( |
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 separated this function for refactoring.
The logic is not changed except *, **, name=lit
is inhibited(the old code does
if self.has_varargs {
// No check here
...
} else {
self.kw_arg_is_ok(item)?;
...
}
).
@@ -406,15 +406,6 @@ fn impl_call(_cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream { | |||
quote! { _slf.#fname(#(#names),*) } | |||
} | |||
|
|||
/// Converts a bool to "true" or "false" | |||
fn bool_to_ident(condition: bool) -> syn::Ident { |
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 removed this function since just quote! { #bool_value }
works.
Will give this a full review tomorrow 👍 |
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. One nitpick about the changelog. Feel free to re-word what I suggest as you like.
NestedMeta::Meta(syn::Meta::List(ref list)) => { | ||
return Err(syn::Error::new_spanned( | ||
list, | ||
"List is not supported as argument", | ||
)); |
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.
Nice :)
Co-Authored-By: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
@davidhewitt |
Resolves #790
*
does not meanaccept_args
and we should rejectfn(10)
fordef fn(*, var=10)
.