Skip to content

Conversation

skoslowski
Copy link
Contributor

This is a duplicate of #1196 rebased to current master resolving the merge conflicts on the existing PR.

(Fixes #1194)

This adds support for a py::args_kw_only() annotation that can be
specified between py::arg annotations to indicate that any following
arguments are keyword-only. This allows you to write:

m.def("f", [](int a, int b) { /* ... */ },
      py::arg("a"), py::args_kw_only(), py::arg("b"));

and have it work like Python 3's:

def f(a, *, b):
    # ...

with respect to how a and b arguments are accepted (that is, a can
be positional or by keyword; b can only be specified by keyword).

@TheButlah
Copy link

TheButlah commented Feb 4, 2020

+1 for this feature

@cpuhrsch
Copy link

I'd love this as well!

@cpuhrsch
Copy link

cpuhrsch commented Mar 3, 2020

Ping: Are you planning on merging this soon? :)

@cpuhrsch
Copy link

@gst - are there any updates to this?

@cpuhrsch
Copy link

cpuhrsch commented Apr 2, 2020

Pinging the creator of this PR @skoslowski

@skoslowski
Copy link
Contributor Author

skoslowski commented Apr 2, 2020

@cpuhrsch, I am not a maintainer of pybind11, so there is nothing I can do. This PR only exists to help get the original code merged.

I have been wanting this for a while now and it has dragged on for too long. So, for now, I have fallen back to using a private fork with this patch included.

@cpuhrsch
Copy link

cpuhrsch commented Apr 3, 2020

Mentioning @wjakob since this PR was approved a while ago and seems very useful in allowing pybind11 to further cover the python function-calling paradigm.

@izdeby
Copy link

izdeby commented Apr 9, 2020

This would be a great addition to pybind. Is there anyway this can be merged, @gst?

@gst
Copy link

gst commented Apr 10, 2020

I dont have merge privilege here otherwise I'd have already merged ;)

@wjakob is the last person having done a commit 10 days ago.. ping ?

@jagerman
Copy link
Member

I like this addition, but some minor bikeshedding: Thoughts on changing the tag to something shorter, perhaps py::kwonly{}? The name feels a little verbose to me currently:

m.def("f", [](int a, int b) { /* ... */ },
        py::arg("a"), py::args_kw_only(), py::arg("b"));

vs:

m.def("f", [](int a, int b) { /* ... */ },
        py::arg("a"), py::kwonly(), py::arg("b"));

@gst
Copy link

gst commented Apr 11, 2020

agree with you py::kwonly looks better.

jagerman and others added 2 commits April 14, 2020 13:02
This adds support for a `py::args_kw_only()` annotation that can be
specified between `py::arg` annotations to indicate that any following
arguments are keyword-only.  This allows you to write:

    m.def("f", [](int a, int b) { /* ... */ },
          py::arg("a"), py::args_kw_only(), py::arg("b"));

and have it work like Python 3's:

    def f(a, *, b):
        # ...

with respect to how `a` and `b` arguments are accepted (that is, `a` can
be positional or by keyword; `b` can only be specified by keyword).
@skoslowski
Copy link
Contributor Author

I am also in favor of kwonly(). If there are no objections, I'll amend the original commit.

@wjakob
Copy link
Member

wjakob commented Apr 26, 2020

This is a nice feature, and the patch looks fantastic. Apologies that it's taken me a while to get to this. Many thanks to @jagerman and @skoslowski.

@wjakob wjakob merged commit a86ac53 into pybind:master Apr 26, 2020
@henryiii
Copy link
Collaborator

Wow, didn't know this went in!!!

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.

Indicating that a function takes keyword-only arguments
8 participants