Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for code helper to fix the binding docstring to make code documentation and completion happy #461

Open
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

damienmarchal
Copy link
Contributor

@damienmarchal damienmarchal commented Oct 2, 2024

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.

The name was missing.


Signed-off-by: Damien Marchal <[email protected]>
I suspect this code to have been integrated in the SofaPython3 by mistake and to have never
worked. By looking at it it may be an experiment to allow syntax like this

import Sofa.Components
Sofa.Components.MechanicalObject()  ...
…nows about them.

The problem:
Depending on the definition order of the binded classes, there may have
incorrect types if Base is useing BaseData... but BaseData is only binded after Base.

The PR propose a solution for that using a decidcated "forward" registration patter.
…nows about them.

The problem:
Depending on the definition order of the binded classes, there may have
incorrect types if Base is useing BaseData... but BaseData is only binded after Base.

The PR propose a solution for that using a decidcated "forward" registration patter.
…nows about them.

The problem:
Depending on the definition order of the binded classes, there may have
incorrect types if Base is useing BaseData... but BaseData is only binded after Base.

The PR propose a solution for that using a decidcated "forward" registration patter.
I suspect this code to have been integrated in the SofaPython3 by mistake and to have never
worked. By looking at it it may be an experiment to allow syntax like this

import Sofa.Components
Sofa.Components.MechanicalObject()  ...
…definition' into pr-typehint-example-how-to-fix-definition
It should be completed if ones want more feature than just class declaration
It should be completed if ones want more feature than just class declaration
# Conflicts:
#	bindings/Sofa/src/SofaPython3/Sofa/Components/Submodule_Components.h
#	bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseClass.h
#	bindings/Sofa/src/SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant