Skip to content

Commit c102973

Browse files
committed
add pos_only test
1 parent 16e99de commit c102973

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

test-data/pybind11_fixtures/src/main.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,82 @@ void bind_demo(py::module& m) {
265265
// Module-level attributes
266266
m.attr("PI") = std::acos(-1);
267267
m.attr("__version__") = "0.0.1";
268+
269+
// test_keyword_only_args
270+
m.def(
271+
"kw_only_all",
272+
[](int i, int j) { return py::make_tuple(i, j); },
273+
py::kw_only(),
274+
py::arg("i"),
275+
py::arg("j"));
276+
m.def(
277+
"kw_only_some",
278+
[](int i, int j, int k) { return py::make_tuple(i, j, k); },
279+
py::arg(),
280+
py::kw_only(),
281+
py::arg("j"),
282+
py::arg("k"));
283+
m.def(
284+
"kw_only_with_defaults",
285+
[](int i, int j, int k, int z) { return py::make_tuple(i, j, k, z); },
286+
py::arg() = 3,
287+
"j"_a = 4,
288+
py::kw_only(),
289+
"k"_a = 5,
290+
"z"_a);
291+
m.def(
292+
"kw_only_mixed",
293+
[](int i, int j) { return py::make_tuple(i, j); },
294+
"i"_a,
295+
py::kw_only(),
296+
"j"_a);
297+
m.def(
298+
"kw_only_plus_more",
299+
[](int i, int j, int k, const py::kwargs &kwargs) {
300+
return py::make_tuple(i, j, k, kwargs);
301+
},
302+
py::arg() /* positional */,
303+
py::arg("j") = -1 /* both */,
304+
py::kw_only(),
305+
py::arg("k") /* kw-only */);
306+
307+
m.def("register_invalid_kw_only", [](py::module_ m) {
308+
m.def(
309+
"bad_kw_only",
310+
[](int i, int j) { return py::make_tuple(i, j); },
311+
py::kw_only(),
312+
py::arg() /* invalid unnamed argument */,
313+
"j"_a);
314+
});
315+
316+
// test_positional_only_args
317+
m.def(
318+
"pos_only_all",
319+
[](int i, int j) { return py::make_tuple(i, j); },
320+
py::arg("i"),
321+
py::arg("j"),
322+
py::pos_only());
323+
m.def(
324+
"pos_only_mix",
325+
[](int i, int j) { return py::make_tuple(i, j); },
326+
py::arg("i"),
327+
py::pos_only(),
328+
py::arg("j"));
329+
m.def(
330+
"pos_kw_only_mix",
331+
[](int i, int j, int k) { return py::make_tuple(i, j, k); },
332+
py::arg("i"),
333+
py::pos_only(),
334+
py::arg("j"),
335+
py::kw_only(),
336+
py::arg("k"));
337+
m.def(
338+
"pos_only_def_mix",
339+
[](int i, int j, int k) { return py::make_tuple(i, j, k); },
340+
py::arg("i"),
341+
py::arg("j") = 2,
342+
py::pos_only(),
343+
py::arg("k") = 3);
268344
}
269345

270346
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)