mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Support keyword-only arguments
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).
This commit is contained in:
committed by
Wenzel Jakob
parent
03f9e4a8ec
commit
be0d804523
@@ -94,6 +94,30 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
|
||||
// m.def("bad_args6", [](py::args, py::args) {});
|
||||
// m.def("bad_args7", [](py::kwargs, py::kwargs) {});
|
||||
|
||||
// test_keyword_only_args
|
||||
m.def("kwonly_all", [](int i, int j) { return py::make_tuple(i, j); },
|
||||
py::args_kw_only(), py::arg("i"), py::arg("j"));
|
||||
m.def("kwonly_some", [](int i, int j, int k) { return py::make_tuple(i, j, k); },
|
||||
py::arg(), py::args_kw_only(), py::arg("j"), py::arg("k"));
|
||||
m.def("kwonly_with_defaults", [](int i, int j, int k, int z) { return py::make_tuple(i, j, k, z); },
|
||||
py::arg() = 3, "j"_a = 4, py::args_kw_only(), "k"_a = 5, "z"_a);
|
||||
m.def("kwonly_mixed", [](int i, int j) { return py::make_tuple(i, j); },
|
||||
"i"_a, py::args_kw_only(), "j"_a);
|
||||
m.def("kwonly_plus_more", [](int i, int j, int k, py::kwargs kwargs) {
|
||||
return py::make_tuple(i, j, k, kwargs); },
|
||||
py::arg() /* positional */, py::arg("j") = -1 /* both */, py::args_kw_only(), py::arg("k") /* kw-only */);
|
||||
|
||||
m.def("register_invalid_kwonly", [](py::module m) {
|
||||
m.def("bad_kwonly", [](int i, int j) { return py::make_tuple(i, j); },
|
||||
py::args_kw_only(), py::arg() /* invalid unnamed argument */, "j"_a);
|
||||
});
|
||||
|
||||
// These should fail to compile:
|
||||
// argument annotations are required when using args_kw_only
|
||||
// m.def("bad_kwonly1", [](int) {}, py::args_kw_only());
|
||||
// can't specify both `py::args_kw_only` and a `py::args` argument
|
||||
// m.def("bad_kwonly2", [](int i, py::args) {}, py::args_kw_only(), "i"_a);
|
||||
|
||||
// test_function_signatures (along with most of the above)
|
||||
struct KWClass { void foo(int, float) {} };
|
||||
py::class_<KWClass>(m, "KWClass")
|
||||
|
||||
Reference in New Issue
Block a user