Skip to content

Support keyword-only arguments (rebased)#2100

Merged
wjakob merged 2 commits into
pybind:masterfrom
skoslowski:kwonly_args
Apr 26, 2020
Merged

Support keyword-only arguments (rebased)#2100
wjakob merged 2 commits into
pybind:masterfrom
skoslowski:kwonly_args

Conversation

@skoslowski

Copy link
Copy Markdown
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

TheButlah commented Feb 4, 2020

Copy link
Copy Markdown

+1 for this feature

@cpuhrsch

Copy link
Copy Markdown

I'd love this as well!

@cpuhrsch

cpuhrsch commented Mar 3, 2020

Copy link
Copy Markdown

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

@cpuhrsch

Copy link
Copy Markdown

@gst - are there any updates to this?

@cpuhrsch

cpuhrsch commented Apr 2, 2020

Copy link
Copy Markdown

Pinging the creator of this PR @skoslowski

@skoslowski

skoslowski commented Apr 2, 2020

Copy link
Copy Markdown
Contributor Author

@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

cpuhrsch commented Apr 3, 2020

Copy link
Copy Markdown

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

izdeby commented Apr 9, 2020

Copy link
Copy Markdown

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

@gst

gst commented Apr 10, 2020

Copy link
Copy Markdown

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
Copy Markdown
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

gst commented Apr 11, 2020

Copy link
Copy Markdown

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
Copy Markdown
Contributor Author

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

@wjakob

wjakob commented Apr 26, 2020

Copy link
Copy Markdown
Member

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
Copy Markdown
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