Emit complete type information in generated docstrings #417
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 bypybind11
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:
__qualname__
on classes for Python >= 3.3 so that classes bound inscope()
can be found againslice
andback_reference<T>
argument and return types are correctly annotated.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!