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

Split pybind declarations and definitions #6869

Merged
merged 46 commits into from
Aug 14, 2024
Merged

Conversation

timohl
Copy link
Contributor

@timohl timohl commented Jul 14, 2024

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes C++ Types in Python Documentation #6867
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

As explained in #6867 there are several instances where C++ types are shown in the Python binding documentation.
Some of those instances can be solved by splitting the py::_class and py::enum_ declarations from the method/function definitions using .def(...).
This ensures, that all types are properly declared before usage.

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

Split modules:

  • camera
  • core
  • data (no changes needed)
  • geometry
  • io
  • ml
  • pipelines
  • t
  • utility
  • visualization

Style+Testing:

  • Applied style checker
  • Run testing
  • Add changelog entry

Copy link

update-docs bot commented Jul 14, 2024

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@timohl timohl mentioned this pull request Jul 14, 2024
3 tasks
@timohl
Copy link
Contributor Author

timohl commented Jul 14, 2024

All except for o3d.data is done now!
I don't think splitting o3d.data is necessary, since nothing else depends on it, and it just contains a lot of small classes.

This removes 80 C++ types from python binding docs, if I see correctly.
Here you can see the before vs after diff of the output of pybind11-stubgen open3d:
The 11 additions are just changing object addresses, so they can be subtracted from the deletions.

Sorry for the huge number of changes required for this, but it is basically just a lot of movement of code.
In order to make review easier, here is a summary of how I split modules up into declarations and definitions, with examples from o3d.camera:

Declarations contain py::module, py::class_ and py::enum_ declarations, as well as the value declarations of an enum:

py::module m_camera = m.def_submodule("camera");
py::class_<PinholeCameraIntrinsic> pinhole_intr(
        m_camera, "PinholeCameraIntrinsic", "<docs...>");
py::enum_<PinholeCameraIntrinsicParameters> pinhole_intr_params(
            m_camera, "PinholeCameraIntrinsicParameters", py::arithmetic(),
            "PinholeCameraIntrinsicParameters");
pinhole_intr_params
            .value("PrimeSenseDefault",
                   PinholeCameraIntrinsicParameters::PrimeSenseDefault,
                   "Default camera intrinsic parameter for PrimeSense.")
...

Definitions refer to modules and classes using .attr() and add method and function definitions to them:

auto m_camera = static_cast<py::module>(m.attr("camera"));
// open3d.camera.PinholeCameraIntrinsic
auto pinhole_intr = static_cast<py::class_<PinholeCameraIntrinsic>>(
        m_camera.attr("PinholeCameraIntrinsic"));
py::detail::bind_default_constructor<PinholeCameraIntrinsic>(pinhole_intr);
py::detail::bind_copy_functions<PinholeCameraIntrinsic>(pinhole_intr);
pinhole_intr
        .def(py::init<int, int, const Eigen::Matrix3d>(), "width"_a,
             "height"_a, "intrinsic_matrix"_a)

Next steps for me will be running style checks and unit tests.
Please let me know if you have any questions for review, if I missed anything, or anything else I should do?
I would like to further improve the documentation and typing stubs generated by pybind11-stubgen after this.

@timohl
Copy link
Contributor Author

timohl commented Jul 14, 2024

Applied style checker, run tests on my machine (no extra flags though like CUDA, ...), and added entry to changelog.
Codacy Static Code Analysis fails with two new issues, however looking into it, they existed before but just got shifted.

Please tell me if any action is required on my part or any open questions come up.

@timohl timohl marked this pull request as ready for review July 14, 2024 21:28
@timohl
Copy link
Contributor Author

timohl commented Jul 17, 2024

Failed checks:

All other checks were successful.

@ssheorey
Copy link
Member

Thanks @timohl for this huge effort! This looks like a pretty useful update. I'll check it out. Can you paste some before and after screenshots of how the docs look better after this PR, and how a code editor is able to better do auto-complete with pybind11-stubgen (e.g. VS code or your favourite editor).

@ssheorey
Copy link
Member

PS: Also please consider contributing the script for pybind11-stubgen, so that other users can get auto-complete out of the box for Open3D.

@ssheorey ssheorey requested review from ssheorey and benjaminum and removed request for benjaminum July 25, 2024 22:55
@timohl
Copy link
Contributor Author

timohl commented Jul 26, 2024

Here how this changes vscode type checking ability in three cases:

  • No generated typing stubs (No stubs)
  • Generated typing stubs from main (old stubs)
  • Generated typing stubs from this pull request (new stubs)

Typing stubs where generated using pybind11-stubgen open3d -o typings.

Desc. Image
No stubs:
The hover box provides no documentation.
Types are not recognized.
no_typings
Old stubs:
The hover box provides documentation and partial typing.
Return type is ....
Notice how get_center() at the bottom is gray,
since the type checker does not know the type of oriented_bb
old_typings
New stubs:
The hover box provides documentation and full typing.
Return type is properly recognized.
get_center() is yellow,
since the type checker knows the type of oriented_bb.
new_typings

@timohl timohl mentioned this pull request Jul 26, 2024
9 tasks
@ssheorey ssheorey added this to the v0.19 milestone Aug 14, 2024
Copy link
Member

@ssheorey ssheorey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @timohl Looks good.

Next step: Package the generated stubs with Open3D.

@ssheorey ssheorey merged commit 48ccf2a into isl-org:main Aug 14, 2024
29 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

C++ Types in Python Documentation
2 participants