Skip to content
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

Merged
merged 4 commits into from
Mar 7, 2020
Merged

Conversation

kngwyu
Copy link
Member

@kngwyu kngwyu commented Mar 4, 2020

Resolves #790
* does not mean accept_args and we should reject fn(10) for def fn(*, var=10).

@oconnor663
Copy link
Contributor

Thanks for the quick fix!

@@ -231,11 +231,15 @@ impl MethArgs {
[a.to_object(py), args.into(), kwargs.to_object(py)].to_object(py)
}

#[args("*", c = 10)]
Copy link
Member

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).

Copy link
Member Author

@kngwyu kngwyu Mar 5, 2020

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 🤔

Copy link
Member

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.

Copy link
Member Author

@kngwyu kngwyu Mar 5, 2020

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.

Copy link
Member

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)]

Copy link
Member

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.)

Copy link
Member Author

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.

Copy link
Member

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![*] ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kngwyu
Copy link
Member Author

kngwyu commented Mar 6, 2020

I pushed a change that raises a compile error for #[pyfunction(a, "*", b)].
For the problem of ambiguity, I opened another issue #794

Ok(())
}

fn add_nv_common(
Copy link
Member Author

@kngwyu kngwyu Mar 6, 2020

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 {
Copy link
Member Author

@kngwyu kngwyu Mar 6, 2020

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.

@kngwyu kngwyu requested a review from davidhewitt March 6, 2020 05:16
@davidhewitt
Copy link
Member

Will give this a full review tomorrow 👍

Copy link
Member

@davidhewitt davidhewitt left a 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.

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines +55 to +59
NestedMeta::Meta(syn::Meta::List(ref list)) => {
return Err(syn::Error::new_spanned(
list,
"List is not supported as argument",
));
Copy link
Member

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>
@kngwyu
Copy link
Member Author

kngwyu commented Mar 7, 2020

@davidhewitt
Thanks!

@kngwyu kngwyu merged commit 1a8ebc2 into PyO3:master Mar 7, 2020
@kngwyu kngwyu deleted the separator branch March 7, 2020 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"*" for keyword-only arguments allows signatures that it shouldn't
4 participants