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:
Set __qualname__ on classes for Python >= 3.3 so that classes bound in scope() can be found again
Use fully-qualified type names for imported types in signatures
Emit signatures for property getters/setters
Ensure that slice and back_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.
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!