From 35b16a8d5f220553aab5147a68cb06dc528d230e Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Thu, 10 Oct 2024 10:30:14 -0700 Subject: [PATCH 1/2] 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 9001d86b19..109c85bf00 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 9b2b1d8346..a6aaa23cd0 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) From 3cd11315da96219d9ce99f35894554999354d05d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:34:55 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- tests/test_class.cpp | 3 +-- tests/test_class.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 109c85bf00..d5ebb2fdb8 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -566,8 +566,7 @@ TEST_SUBMODULE(class_, m) { py::arg("x"), py::kw_only(), py::arg("y"), - py::is_method(py::none()) - ); + py::is_method(py::none())); m.attr("KwOnlyMethod") = parent_metaclass("MwOnlyMethod", py::make_tuple(), attributes); } diff --git a/tests/test_class.py b/tests/test_class.py index a6aaa23cd0..c47051bcf1 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -504,4 +504,4 @@ def test_pr4220_tripped_over_this(): def test_kw_only(): - assert (m.KwOnlyMethod().test("x", y="y") == 0) + assert m.KwOnlyMethod().test("x", y="y") == 0