From 35b16a8d5f220553aab5147a68cb06dc528d230e Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Thu, 10 Oct 2024 10:30:14 -0700 Subject: [PATCH] Demonstrate that using the method of defining a class recommended on https://github.com/pybind/pybind11/issues/1193 does not allow for passing kwargs. --- tests/test_class.cpp | 16 ++++++++++++++++ tests/test_class.py | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 9001d86b1..109c85bf0 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -554,6 +554,22 @@ TEST_SUBMODULE(class_, m) { }); test_class::pr4220_tripped_over_this::bind_empty0(m); + + py::object parent_metaclass = py::reinterpret_borrow((PyObject *) &PyType_Type); + py::dict attributes; + + attributes["test"] = py::cpp_function( + [](py::object self [[maybe_unused]], py::object x, py::object y) { + py::print(x, y); + return 0; + }, + py::arg("x"), + py::kw_only(), + py::arg("y"), + py::is_method(py::none()) + ); + + m.attr("KwOnlyMethod") = parent_metaclass("MwOnlyMethod", py::make_tuple(), attributes); } template diff --git a/tests/test_class.py b/tests/test_class.py index 9b2b1d834..a6aaa23cd 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -501,3 +501,7 @@ def test_pr4220_tripped_over_this(): m.Empty0().get_msg() == "This is really only meant to exercise successful compilation." ) + + +def test_kw_only(): + assert (m.KwOnlyMethod().test("x", y="y") == 0)