boostorg / python

Boost.org python module
http://boostorg.github.io/python
Boost Software License 1.0
465 stars 201 forks source link

Emit complete type information in generated docstrings #417

Open jvansanten opened 1 year ago

jvansanten commented 1 year ago

PEP 484 type stubs are extremely convenient, both for static type checking with e.g. mypy and code completion with e.g. PyLance in VSCode or Jedi in vim. There are various ways to generate these for compiled extensions.

The stubgen that ships with mypy can generate stubs for compiled extensions, but knows nothing about types, so you would still have to complete them by hand. This is not exactly in the spirit of programatically-generated bindings. pybind11-stubgen does a similar thing, but parses the docstrings generated by pybind11 to get argument and return types. While pybind11 can be adapted to parse the signature format that boost-python already uses, the type information in the current generated docstrings is incomplete.

This PR introduces minimal changes such that the generated signatures can be used to create correct stubs. Specifically, these are:

Existing tests were extended to verify the expected docstring contents.

This is a roundabout way of achieving #277, and seemed the most straightforward route given that pybind11 also seems to still rely on external tooling to generate stubs. Along the way, it also fixes #426.

Comments welcome!