You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations");
252
255
static_assert(has_arg_annotations || !has_pos_only_args, "py::pos_only requires the use of argument annotations (for docstrings and aligning the annotations to the argument)");
253
-
static_assert(!(has_args && has_kw_only_args), "py::kw_only cannot be combined with a py::args argument");
256
+
257
+
static_assert(constexpr_sum(std::is_same<kw_only, Extra>::value...) <= 1, "py::kw_only may be specified only once");
258
+
static_assert(constexpr_sum(std::is_same<pos_only, Extra>::value...) <= 1, "py::pos_only may be specified only once");
0 commit comments