Proposal for code helper to fix the binding docstring to make code documentation and completion happy
Currently pybind11 generates the function signatures automagically and we use this feature a lot. The problem is that the automagically generates python signatures with type-hints and that these signatures may be invalid (in fact not narrow enough).
On way to partially tackle the problem is to change de c++ code to help pybind11 to properly produce the correct signatures. In some case this is minor changes on the c++ side, but in some others this may imply massive refactoring.
There is a second issue, when a function should have generic type hint signature (see PEP https://peps.python.org/pep-0695/) there is no way to add that in the automagicall process of pybind11's without changing pybind11 code and have that accepted mainstream.
The proposed alternative was fix manually the failing method signatures. The draw back of the approach was that in that case all the docstring autogenerated are disabled. So we have to write every signature for each overload of a method, even if previously they were correct). In addition it was suggested by @bakpaul to have that done not directly in the docstring but in the c++ code.
This PR is one possible implementation for such feature. Instead of the classical (which fails on signature generation):
p.add_method("addObject", addObjectKwargs, sofapython3::doc::sofa::core::Node::addObjectKwargs);
p.add_method("addObject", addObject, sofapython3::doc::sofa::core::Node::addObject);
p.add_method("addObject", addObjectGenericType, sofapython3::doc::sofa::core::Node::addObjectGenerictype);
Should be written this way (with custom signature generation only on the failing one)
def_method(p, "addObject")
.add_override(addObjectKwargs, sofapython3::doc::sofa::core::Node::addObjectKwargs)
.add_override(addObject, sofapython3::doc::sofa::core::Node::addObject)
.add_override("def addObject[T]() -> T", addObjectGenericType, sofapython3::doc::sofa::core::Node::addObjectGenerictype);
The complexity of the code is a result of how pybind11 is doing the magic of binding.