From 72663ce85cd4a7386d27190130d22c4fc1e8133d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 4 May 2019 21:22:59 +0200 Subject: [PATCH] documentation/python: functions actually *can* take args/kwargs directly. --- documentation/python.py | 15 ++++++++++----- .../pybind_signatures/pybind_signatures.cpp | 5 ++++- .../pybind_signatures/pybind_signatures.html | 4 ++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/documentation/python.py b/documentation/python.py index 772158be..6b749314 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -465,11 +465,16 @@ def extract_function_doc(state: State, parent, path: List[str], function) -> Lis param.default = default if type or default: out.has_complex_params = True - # *args / **kwargs are shown in the signature only for - # overloaded functions and we are expanding those - assert name not in ['*args', '**kwargs'] - - param.kind = 'POSITIONAL_ONLY' if positional_only else 'POSITIONAL_OR_KEYWORD' + # *args / **kwargs can still appear in the parsed signatures if + # the function accepts py::args / py::kwargs directly + if name == '*args': + param.name = 'args' + param.kind = 'VAR_POSITIONAL' + elif name == '**kwargs': + param.name = 'kwargs' + param.kind = 'VAR_KEYWORD' + else: + param.kind = 'POSITIONAL_ONLY' if positional_only else 'POSITIONAL_OR_KEYWORD' out.params += [param] diff --git a/documentation/test_python/pybind_signatures/pybind_signatures.cpp b/documentation/test_python/pybind_signatures/pybind_signatures.cpp index 4cb39638..ea145b27 100644 --- a/documentation/test_python/pybind_signatures/pybind_signatures.cpp +++ b/documentation/test_python/pybind_signatures/pybind_signatures.cpp @@ -33,6 +33,8 @@ struct MyClass { private: float _foo = 0.0f; }; +void duck(py::args, py::kwargs) {} + PYBIND11_MODULE(pybind_signatures, m) { m.doc() = "pybind11 function signature extraction"; @@ -43,7 +45,8 @@ PYBIND11_MODULE(pybind_signatures, m) { .def("taking_a_list_returning_a_tuple", &takingAListReturningATuple, "Takes a list, returns a tuple") .def("crazy_signature", &crazySignature, "Function that failed to get parsed") .def("overloaded", static_cast(&overloaded), "Overloaded for ints") - .def("overloaded", static_cast(&overloaded), "Overloaded for floats"); + .def("overloaded", static_cast(&overloaded), "Overloaded for floats") + .def("duck", &duck, "A function taking args/kwargs directly"); py::class_(m, "MyClass", "My fun class!") .def_static("static_function", &MyClass::staticFunction, "Static method with positional-only args") diff --git a/documentation/test_python/pybind_signatures/pybind_signatures.html b/documentation/test_python/pybind_signatures/pybind_signatures.html index 82ed966d..b314bfcf 100644 --- a/documentation/test_python/pybind_signatures/pybind_signatures.html +++ b/documentation/test_python/pybind_signatures/pybind_signatures.html @@ -49,6 +49,10 @@ def crazy_signature(…)
Function that failed to get parsed
+
+ def duck(*args, **kwargs) +
+
A function taking args/kwargs directly
def overloaded(arg0: int, /) -> str
-- 2.30.2