Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/test_kwargs_and_defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,39 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
"class_default_argument",
[](py::object a) { return py::repr(std::move(a)); },
"a"_a = py::module_::import("decimal").attr("Decimal"));

m.def(
"func_kw_only_0", [](int) {}, py::kw_only(), py::arg("i") = 0);
m.def(
"func_kw_only_1", [](int, int) {}, py::arg("a") = 0, py::kw_only(), py::arg("i") = 0);
m.def(
"func_kw_only_2",
[](int, int, int) {},
py::arg("a") = 0,
py::arg("b") = 0,
py::kw_only(),
py::arg("i") = 0);

// https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
// Originally reduced from tensorstore.Dim
struct init_kw_only_0 {};
py::class_<init_kw_only_0>(m, "init_kw_only_0")
.def(py::init([](int) { return init_kw_only_0(); }),
py::kw_only(), // test passes with this line commented out.
py::arg("i") = 0);

struct init_kw_only_1 {};
py::class_<init_kw_only_1>(m, "init_kw_only_1")
.def(py::init([](int, int) { return init_kw_only_1(); }),
py::arg("a") = 0,
py::kw_only(), // test passes with this line commented out.
py::arg("i") = 0);

struct init_kw_only_2 {};
py::class_<init_kw_only_2>(m, "init_kw_only_2")
.def(py::init([](int, int, int) { return init_kw_only_2(); }),
py::arg("a") = 0,
py::arg("b") = 0,
py::kw_only(), // test passes with this line commented out.
py::arg("i") = 0);
}
24 changes: 24 additions & 0 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,27 @@ def test_args_refcount():
assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)

assert m.class_default_argument() == "<class 'decimal.Decimal'>"


@pytest.mark.parametrize(
"func",
[
m.func_kw_only_0,
m.func_kw_only_1,
m.func_kw_only_2,
],
)
def test_func_kw_only(func):
func(i=1)


@pytest.mark.parametrize(
"init",
[
m.init_kw_only_0,
m.init_kw_only_1,
m.init_kw_only_2,
],
)
def test_init_kw_only(init):
init(i=1)