mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-20 06:49:25 +00:00
Provide better type hints for a variety of generic types (#4259)
* Provide better type hints for a variety of generic types * Makes better documentation * tuple, dict, list, set, function * Move to py::typing * style: pre-commit fixes * Update copyright line with correct year and actual author. The author information was copy-pasted from the git log output. --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -398,3 +398,32 @@ before they are used as a parameter or return type of a function:
|
||||
pyFoo.def(py::init<const ns::Bar&>());
|
||||
pyBar.def(py::init<const ns::Foo&>());
|
||||
}
|
||||
|
||||
Setting inner type hints in docstrings
|
||||
======================================
|
||||
|
||||
When you use pybind11 wrappers for ``list``, ``dict``, and other generic python
|
||||
types, the docstring will just display the generic type. You can convey the
|
||||
inner types in the docstring by using a special 'typed' version of the generic
|
||||
type.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
PYBIND11_MODULE(example, m) {
|
||||
m.def("pass_list_of_str", [](py::typing::List<py::str> arg) {
|
||||
// arg can be used just like py::list
|
||||
));
|
||||
}
|
||||
|
||||
The resulting docstring will be ``pass_list_of_str(arg0: list[str]) -> None``.
|
||||
|
||||
The following special types are available in ``pybind11/typing.h``:
|
||||
|
||||
* ``py::Tuple<Args...>``
|
||||
* ``py::Dict<K, V>``
|
||||
* ``py::List<V>``
|
||||
* ``py::Set<V>``
|
||||
* ``py::Callable<Signature>``
|
||||
|
||||
.. warning:: Just like in python, these are merely hints. They don't actually
|
||||
enforce the types of their contents at runtime or compile time.
|
||||
|
||||
Reference in New Issue
Block a user